Skip to content

Commit a728cc9

Browse files
Added/Improved doctests for lowest_common_ancestor.py (#12673)
* added doctests to functions in lowest_common_ancestor.py * fixed doctests to be less excessive * Update lowest_common_ancestor.py * Update lowest_common_ancestor.py * Update lowest_common_ancestor.py * Update lowest_common_ancestor.py * Update lowest_common_ancestor.py * Update lowest_common_ancestor.py --------- Co-authored-by: Maxim Smolskiy <[email protected]>
1 parent b720f24 commit a728cc9

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

data_structures/binary_tree/lowest_common_ancestor.py

+54
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ def swap(a: int, b: int) -> tuple[int, int]:
1515
(4, 3)
1616
>>> swap(67, 12)
1717
(12, 67)
18+
>>> swap(3,-4)
19+
(-4, 3)
1820
"""
1921
a ^= b
2022
b ^= a
@@ -25,6 +27,23 @@ def swap(a: int, b: int) -> tuple[int, int]:
2527
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
2628
"""
2729
creating sparse table which saves each nodes 2^i-th parent
30+
>>> max_node = 6
31+
>>> parent = [[0, 0, 1, 1, 2, 2, 3]] + [[0] * 7 for _ in range(19)]
32+
>>> parent = create_sparse(max_node=max_node, parent=parent)
33+
>>> parent[0]
34+
[0, 0, 1, 1, 2, 2, 3]
35+
>>> parent[1]
36+
[0, 0, 0, 0, 1, 1, 1]
37+
>>> parent[2]
38+
[0, 0, 0, 0, 0, 0, 0]
39+
40+
>>> max_node = 1
41+
>>> parent = [[0, 0]] + [[0] * 2 for _ in range(19)]
42+
>>> parent = create_sparse(max_node=max_node, parent=parent)
43+
>>> parent[0]
44+
[0, 0]
45+
>>> parent[1]
46+
[0, 0]
2847
"""
2948
j = 1
3049
while (1 << j) < max_node:
@@ -38,6 +57,21 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
3857
def lowest_common_ancestor(
3958
u: int, v: int, level: list[int], parent: list[list[int]]
4059
) -> int:
60+
"""
61+
Return the lowest common ancestor between u and v
62+
63+
>>> level = [-1, 0, 1, 1, 2, 2, 2]
64+
>>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + \
65+
[[0] * 7 for _ in range(17)]
66+
>>> lowest_common_ancestor(u=4, v=5, level=level, parent=parent)
67+
2
68+
>>> lowest_common_ancestor(u=4, v=6, level=level, parent=parent)
69+
1
70+
>>> lowest_common_ancestor(u=2, v=3, level=level, parent=parent)
71+
1
72+
>>> lowest_common_ancestor(u=6, v=6, level=level, parent=parent)
73+
6
74+
"""
4175
# u must be deeper in the tree than v
4276
if level[u] < level[v]:
4377
u, v = swap(u, v)
@@ -68,6 +102,26 @@ def breadth_first_search(
68102
sets every nodes direct parent
69103
parent of root node is set to 0
70104
calculates depth of each node from root node
105+
>>> level = [-1] * 7
106+
>>> parent = [[0] * 7 for _ in range(20)]
107+
>>> graph = {1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [], 6: []}
108+
>>> level, parent = breadth_first_search(
109+
... level=level, parent=parent, max_node=6, graph=graph, root=1)
110+
>>> level
111+
[-1, 0, 1, 1, 2, 2, 2]
112+
>>> parent[0]
113+
[0, 0, 1, 1, 2, 2, 3]
114+
115+
116+
>>> level = [-1] * 2
117+
>>> parent = [[0] * 2 for _ in range(20)]
118+
>>> graph = {1: []}
119+
>>> level, parent = breadth_first_search(
120+
... level=level, parent=parent, max_node=1, graph=graph, root=1)
121+
>>> level
122+
[-1, 0]
123+
>>> parent[0]
124+
[0, 0]
71125
"""
72126
level[root] = 0
73127
q: Queue[int] = Queue(maxsize=max_node)

0 commit comments

Comments
 (0)