Skip to content

Commit c85a338

Browse files
committed
added doctests to functions in lowest_common_ancestor.py
1 parent 0c8cf8e commit c85a338

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

Diff for: data_structures/binary_tree/lowest_common_ancestor.py

+56
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ def swap(a: int, b: int) -> tuple[int, int]:
2525
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
2626
"""
2727
creating sparse table which saves each nodes 2^i-th parent
28+
>>> max_node = 6
29+
>>> parent = [[0, 0, 1, 1, 2, 2, 3]] + [[0] * 7 for _ in range(19)]
30+
>>> parent = create_sparse(max_node, parent)
31+
>>> parent[0]
32+
[0, 0, 1, 1, 2, 2, 3]
33+
>>> parent[1]
34+
[0, 0, 0, 0, 1, 1, 1]
35+
>>> parent[2]
36+
[0, 0, 0, 0, 0, 0, 0]
37+
2838
"""
2939
j = 1
3040
while (1 << j) < max_node:
@@ -38,6 +48,26 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
3848
def lowest_common_ancestor(
3949
u: int, v: int, level: list[int], parent: list[list[int]]
4050
) -> int:
51+
"""
52+
Return the lowest common ancestor between u and v
53+
54+
>>> level = [-1, 0, 1, 1, 2, 2, 2]
55+
>>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + [[0] * 7 for _ in range(17)]
56+
>>> lowest_common_ancestor(4, 5, level, parent)
57+
2
58+
>>> lowest_common_ancestor(3, 6, level, parent)
59+
3
60+
>>> lowest_common_ancestor(4, 6, level, parent)
61+
1
62+
>>> lowest_common_ancestor(5, 6, level, parent)
63+
1
64+
>>> lowest_common_ancestor(2, 3, level, parent)
65+
1
66+
>>> lowest_common_ancestor(6, 6, level, parent)
67+
6
68+
>>> lowest_common_ancestor(1, 3, level, parent)
69+
1
70+
"""
4171
# u must be deeper in the tree than v
4272
if level[u] < level[v]:
4373
u, v = swap(u, v)
@@ -56,6 +86,8 @@ def lowest_common_ancestor(
5686
return parent[0][u]
5787

5888

89+
90+
5991
# runs a breadth first search from root node of the tree
6092
def breadth_first_search(
6193
level: list[int],
@@ -68,6 +100,30 @@ def breadth_first_search(
68100
sets every nodes direct parent
69101
parent of root node is set to 0
70102
calculates depth of each node from root node
103+
>>> level = [-1] * 7
104+
>>> parent = [[0] * 7 for _ in range(20)]
105+
>>> graph = {1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [], 6: []}
106+
>>> level, parent = breadth_first_search(level, parent, 6, graph, 1)
107+
>>> level
108+
[-1, 0, 1, 1, 2, 2, 2]
109+
>>> parent[0]
110+
[0, 0, 1, 1, 2, 2, 3]
111+
>>> parent[1]
112+
[0, 0, 0, 0, 0, 0, 0]
113+
>>> parent[2]
114+
[0, 0, 0, 0, 0, 0, 0]
115+
116+
# Edge case: graph with one node
117+
>>> level = [-1] * 2
118+
>>> parent = [[0] * 2 for _ in range(20)]
119+
>>> graph = {1: []}
120+
>>> level, parent = breadth_first_search(level, parent, 1, graph, 1)
121+
>>> level
122+
[-1, 0]
123+
>>> parent[0]
124+
[0, 0]
125+
>>> parent[1]
126+
[0, 0]
71127
"""
72128
level[root] = 0
73129
q: Queue[int] = Queue(maxsize=max_node)

0 commit comments

Comments
 (0)