MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / main

Function main

data_structures/binary_tree/lowest_common_ancestor.py:139–167  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

137
138
139def main() -> None:
140 max_node = 13
141 # initializing with 0
142 parent = [[0 for _ in range(max_node + 10)] for _ in range(20)]
143 # initializing with -1 which means every node is unvisited
144 level = [-1 for _ in range(max_node + 10)]
145 graph: dict[int, list[int]] = {
146 1: [2, 3, 4],
147 2: [5],
148 3: [6, 7],
149 4: [8],
150 5: [9, 10],
151 6: [11],
152 7: [],
153 8: [12, 13],
154 9: [],
155 10: [],
156 11: [],
157 12: [],
158 13: [],
159 }
160 level, parent = breadth_first_search(level, parent, max_node, graph, 1)
161 parent = create_sparse(max_node, parent)
162 print("LCA of node 1 and 3 is: ", lowest_common_ancestor(1, 3, level, parent))
163 print("LCA of node 5 and 6 is: ", lowest_common_ancestor(5, 6, level, parent))
164 print("LCA of node 7 and 11 is: ", lowest_common_ancestor(7, 11, level, parent))
165 print("LCA of node 6 and 7 is: ", lowest_common_ancestor(6, 7, level, parent))
166 print("LCA of node 4 and 12 is: ", lowest_common_ancestor(4, 12, level, parent))
167 print("LCA of node 8 and 8 is: ", lowest_common_ancestor(8, 8, level, parent))
168
169
170if __name__ == "__main__":

Callers 1

Calls 3

create_sparseFunction · 0.85
lowest_common_ancestorFunction · 0.85
breadth_first_searchFunction · 0.70

Tested by

no test coverage detected