Skip to content

Commit 0cd031a

Browse files
modify doctest
1 parent c8ac042 commit 0cd031a

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

data_structures/binary_tree/lowest_common_ancestor.py

+26-27
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,24 @@ def swap(a: int, b: int) -> tuple[int, int]:
2323

2424

2525
def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
26-
"""
26+
r"""
2727
Create a sparse table that saves each node's 2^i-th parent.
28-
29-
The given ``parent`` table should have the direct parent of each node in row 0.
30-
This function fills in:
31-
28+
29+
The given ``parent`` table should have the direct parent of each node
30+
in row 0. This function fills in:
31+
3232
parent[j][i] = parent[j - 1][parent[j - 1][i]]
33-
33+
3434
for each j where 2^j is less than max_node.
35-
35+
3636
For example, consider a small tree where:
3737
- Node 1 is the root (its parent is 0),
3838
- Nodes 2 and 3 have parent 1.
39-
39+
4040
We set up the parent table for only two levels (row 0 and row 1)
4141
for max_node = 3. (Note that in practice the table has many rows.)
42-
43-
>>> parent0 = [0, 0, 1, 1] # 0 is unused; node1's parent=0, nodes 2 and 3's parent=1.
42+
43+
>>> parent0 = [0, 0, 1, 1]
4444
>>> parent1 = [0, 0, 0, 0]
4545
>>> parent = [parent0, parent1]
4646
>>> sparse = create_sparse(3, parent)
@@ -59,18 +59,17 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]:
5959
def lowest_common_ancestor(
6060
u: int, v: int, level: list[int], parent: list[list[int]]
6161
) -> int:
62-
"""
62+
r"""
6363
Return the lowest common ancestor (LCA) of nodes u and v in a tree.
64-
65-
The lists ``level`` and ``parent`` must be precomputed. ``level[i]`` is the depth
66-
of node i, and ``parent`` is a sparse table where parent[0][i] is the direct parent
67-
of node i.
68-
64+
65+
The lists ``level`` and ``parent`` must be precomputed.
66+
6967
>>> # Consider a simple tree:
7068
>>> # 1
7169
>>> # / \\
7270
>>> # 2 3
73-
>>> # With levels: level[1]=0, level[2]=1, level[3]=1 and parent[0]=[0, 0, 1, 1]
71+
>>> # With levels: level[1]=0, level[2]=1, level[3]=1 and
72+
>>> # parent[0]=[0, 0, 1, 1]
7473
>>> level = [-1, 0, 1, 1] # index 0 is dummy
7574
>>> parent = [[0, 0, 1, 1]] + [[0, 0, 0, 0] for _ in range(19)]
7675
>>> lowest_common_ancestor(2, 3, level, parent)
@@ -104,20 +103,20 @@ def breadth_first_search(
104103
graph: dict[int, list[int]],
105104
root: int = 1,
106105
) -> tuple[list[int], list[list[int]]]:
107-
"""
106+
r"""
108107
Run a breadth-first search (BFS) from the root node of the tree.
109-
108+
110109
This sets each node's direct parent (stored in parent[0]) and calculates the
111110
depth (level) of each node from the root.
112-
111+
113112
>>> # Consider a simple tree:
114113
>>> # 1
115114
>>> # / \\
116115
>>> # 2 3
117116
>>> graph = {1: [2, 3], 2: [], 3: []}
118117
>>> level = [-1] * 4 # index 0 is unused; nodes 1 to 3.
119118
>>> parent = [[0] * 4 for _ in range(20)]
120-
>>> new_level, new_parent = breadth_first_search(level, parent, 3, graph, root=1)
119+
>>> new_level, new_parent=breadth_first_search(level,parent,3,graph,root=1)
121120
>>> new_level[1:4]
122121
[0, 1, 1]
123122
>>> new_parent[0][1:4]
@@ -137,30 +136,30 @@ def breadth_first_search(
137136

138137

139138
def main() -> None:
140-
"""
139+
r"""
141140
Run a BFS to set node depths and parents in a sample tree, then create the
142141
sparse table and compute several lowest common ancestors.
143-
142+
144143
The sample tree used is:
145-
144+
146145
1
147146
/ | \
148147
2 3 4
149148
/ / \\ \\
150149
5 6 7 8
151150
/ \\ | / \\
152151
9 10 11 12 13
153-
152+
154153
The expected lowest common ancestors are:
155154
- LCA(1, 3) --> 1
156155
- LCA(5, 6) --> 1
157156
- LCA(7, 11) --> 3
158157
- LCA(6, 7) --> 3
159158
- LCA(4, 12) --> 4
160159
- LCA(8, 8) --> 8
161-
160+
162161
To test main() without it printing to the console, we capture the output.
163-
162+
164163
>>> import sys
165164
>>> from io import StringIO
166165
>>> backup = sys.stdout

0 commit comments

Comments
 (0)