Skip to content

Commit f997aa0

Browse files
committed
fixed doctests to be less excessive
1 parent c85a338 commit f997aa0

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

Diff for: data_structures/binary_tree/lowest_common_ancestor.py

+12-20
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ def swap(a: int, b: int) -> tuple[int, int]:
1111
Return a tuple (b, a) when given two integers a and b
1212
>>> swap(2,3)
1313
(3, 2)
14-
>>> swap(3,4)
15-
(4, 3)
16-
>>> swap(67, 12)
17-
(12, 67)
14+
>>> swap(3,-4)
15+
(-4, 3)
1816
"""
1917
a ^= b
2018
b ^= a
@@ -35,6 +33,13 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
3533
>>> parent[2]
3634
[0, 0, 0, 0, 0, 0, 0]
3735
36+
>>> max_node = 1
37+
>>> parent = [[0, 0]] + [[0] * 2 for _ in range(19)]
38+
>>> parent = create_sparse(max_node, parent)
39+
>>> parent[0]
40+
[0, 0]
41+
>>> parent[1]
42+
[0, 0]
3843
"""
3944
j = 1
4045
while (1 << j) < max_node:
@@ -52,21 +57,16 @@ def lowest_common_ancestor(
5257
Return the lowest common ancestor between u and v
5358
5459
>>> 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)]
60+
>>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + \
61+
[[0] * 7 for _ in range(17)]
5662
>>> lowest_common_ancestor(4, 5, level, parent)
5763
2
58-
>>> lowest_common_ancestor(3, 6, level, parent)
59-
3
6064
>>> lowest_common_ancestor(4, 6, level, parent)
6165
1
62-
>>> lowest_common_ancestor(5, 6, level, parent)
63-
1
6466
>>> lowest_common_ancestor(2, 3, level, parent)
6567
1
6668
>>> lowest_common_ancestor(6, 6, level, parent)
6769
6
68-
>>> lowest_common_ancestor(1, 3, level, parent)
69-
1
7070
"""
7171
# u must be deeper in the tree than v
7272
if level[u] < level[v]:
@@ -86,8 +86,6 @@ def lowest_common_ancestor(
8686
return parent[0][u]
8787

8888

89-
90-
9189
# runs a breadth first search from root node of the tree
9290
def breadth_first_search(
9391
level: list[int],
@@ -108,12 +106,8 @@ def breadth_first_search(
108106
[-1, 0, 1, 1, 2, 2, 2]
109107
>>> parent[0]
110108
[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]
115109
116-
# Edge case: graph with one node
110+
117111
>>> level = [-1] * 2
118112
>>> parent = [[0] * 2 for _ in range(20)]
119113
>>> graph = {1: []}
@@ -122,8 +116,6 @@ def breadth_first_search(
122116
[-1, 0]
123117
>>> parent[0]
124118
[0, 0]
125-
>>> parent[1]
126-
[0, 0]
127119
"""
128120
level[root] = 0
129121
q: Queue[int] = Queue(maxsize=max_node)

0 commit comments

Comments
 (0)