From c85a338c0e48bb5547e0bbe93a5c6f2d03ad5b84 Mon Sep 17 00:00:00 2001 From: NidhaNureen Date: Mon, 14 Apr 2025 19:12:23 +1200 Subject: [PATCH 1/2] added doctests to functions in lowest_common_ancestor.py --- .../binary_tree/lowest_common_ancestor.py | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/data_structures/binary_tree/lowest_common_ancestor.py b/data_structures/binary_tree/lowest_common_ancestor.py index 651037703b95..9b59389456b7 100644 --- a/data_structures/binary_tree/lowest_common_ancestor.py +++ b/data_structures/binary_tree/lowest_common_ancestor.py @@ -25,6 +25,16 @@ def swap(a: int, b: int) -> tuple[int, int]: def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: """ creating sparse table which saves each nodes 2^i-th parent + >>> max_node = 6 + >>> parent = [[0, 0, 1, 1, 2, 2, 3]] + [[0] * 7 for _ in range(19)] + >>> parent = create_sparse(max_node, parent) + >>> parent[0] + [0, 0, 1, 1, 2, 2, 3] + >>> parent[1] + [0, 0, 0, 0, 1, 1, 1] + >>> parent[2] + [0, 0, 0, 0, 0, 0, 0] + """ j = 1 while (1 << j) < max_node: @@ -38,6 +48,26 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: def lowest_common_ancestor( u: int, v: int, level: list[int], parent: list[list[int]] ) -> int: + """ + Return the lowest common ancestor between u and v + + >>> level = [-1, 0, 1, 1, 2, 2, 2] + >>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + [[0] * 7 for _ in range(17)] + >>> lowest_common_ancestor(4, 5, level, parent) + 2 + >>> lowest_common_ancestor(3, 6, level, parent) + 3 + >>> lowest_common_ancestor(4, 6, level, parent) + 1 + >>> lowest_common_ancestor(5, 6, level, parent) + 1 + >>> lowest_common_ancestor(2, 3, level, parent) + 1 + >>> lowest_common_ancestor(6, 6, level, parent) + 6 + >>> lowest_common_ancestor(1, 3, level, parent) + 1 + """ # u must be deeper in the tree than v if level[u] < level[v]: u, v = swap(u, v) @@ -56,6 +86,8 @@ def lowest_common_ancestor( return parent[0][u] + + # runs a breadth first search from root node of the tree def breadth_first_search( level: list[int], @@ -68,6 +100,30 @@ def breadth_first_search( sets every nodes direct parent parent of root node is set to 0 calculates depth of each node from root node + >>> level = [-1] * 7 + >>> parent = [[0] * 7 for _ in range(20)] + >>> graph = {1: [2, 3], 2: [4, 5], 3: [6], 4: [], 5: [], 6: []} + >>> level, parent = breadth_first_search(level, parent, 6, graph, 1) + >>> level + [-1, 0, 1, 1, 2, 2, 2] + >>> parent[0] + [0, 0, 1, 1, 2, 2, 3] + >>> parent[1] + [0, 0, 0, 0, 0, 0, 0] + >>> parent[2] + [0, 0, 0, 0, 0, 0, 0] + + # Edge case: graph with one node + >>> level = [-1] * 2 + >>> parent = [[0] * 2 for _ in range(20)] + >>> graph = {1: []} + >>> level, parent = breadth_first_search(level, parent, 1, graph, 1) + >>> level + [-1, 0] + >>> parent[0] + [0, 0] + >>> parent[1] + [0, 0] """ level[root] = 0 q: Queue[int] = Queue(maxsize=max_node) From f997aa0fb7ec42269750fa6e6b334cfeb4194e8f Mon Sep 17 00:00:00 2001 From: NidhaNureen Date: Thu, 17 Apr 2025 00:51:27 +1200 Subject: [PATCH 2/2] fixed doctests to be less excessive --- .../binary_tree/lowest_common_ancestor.py | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/data_structures/binary_tree/lowest_common_ancestor.py b/data_structures/binary_tree/lowest_common_ancestor.py index 9b59389456b7..a2a14d04f035 100644 --- a/data_structures/binary_tree/lowest_common_ancestor.py +++ b/data_structures/binary_tree/lowest_common_ancestor.py @@ -11,10 +11,8 @@ def swap(a: int, b: int) -> tuple[int, int]: Return a tuple (b, a) when given two integers a and b >>> swap(2,3) (3, 2) - >>> swap(3,4) - (4, 3) - >>> swap(67, 12) - (12, 67) + >>> swap(3,-4) + (-4, 3) """ a ^= b b ^= a @@ -35,6 +33,13 @@ def create_sparse(max_node: int, parent: list[list[int]]) -> list[list[int]]: >>> parent[2] [0, 0, 0, 0, 0, 0, 0] + >>> max_node = 1 + >>> parent = [[0, 0]] + [[0] * 2 for _ in range(19)] + >>> parent = create_sparse(max_node, parent) + >>> parent[0] + [0, 0] + >>> parent[1] + [0, 0] """ j = 1 while (1 << j) < max_node: @@ -52,21 +57,16 @@ def lowest_common_ancestor( Return the lowest common ancestor between u and v >>> level = [-1, 0, 1, 1, 2, 2, 2] - >>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + [[0] * 7 for _ in range(17)] + >>> parent = [[0, 0, 1, 1, 2, 2, 3],[0, 0, 0, 0, 1, 1, 1]] + \ + [[0] * 7 for _ in range(17)] >>> lowest_common_ancestor(4, 5, level, parent) 2 - >>> lowest_common_ancestor(3, 6, level, parent) - 3 >>> lowest_common_ancestor(4, 6, level, parent) 1 - >>> lowest_common_ancestor(5, 6, level, parent) - 1 >>> lowest_common_ancestor(2, 3, level, parent) 1 >>> lowest_common_ancestor(6, 6, level, parent) 6 - >>> lowest_common_ancestor(1, 3, level, parent) - 1 """ # u must be deeper in the tree than v if level[u] < level[v]: @@ -86,8 +86,6 @@ def lowest_common_ancestor( return parent[0][u] - - # runs a breadth first search from root node of the tree def breadth_first_search( level: list[int], @@ -108,12 +106,8 @@ def breadth_first_search( [-1, 0, 1, 1, 2, 2, 2] >>> parent[0] [0, 0, 1, 1, 2, 2, 3] - >>> parent[1] - [0, 0, 0, 0, 0, 0, 0] - >>> parent[2] - [0, 0, 0, 0, 0, 0, 0] - # Edge case: graph with one node + >>> level = [-1] * 2 >>> parent = [[0] * 2 for _ in range(20)] >>> graph = {1: []} @@ -122,8 +116,6 @@ def breadth_first_search( [-1, 0] >>> parent[0] [0, 0] - >>> parent[1] - [0, 0] """ level[root] = 0 q: Queue[int] = Queue(maxsize=max_node)