-
Notifications
You must be signed in to change notification settings - Fork 113
/
lowest_common_ancestor.py
55 lines (42 loc) · 1.48 KB
/
lowest_common_ancestor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
""" Lowest Common Ancestor in Binary Search Tree
"""
class Node:
""" Node contains actual data and address to left and right node """
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinarySearchTree:
""" Binary Search Tree Implementation """
def __init__(self):
self.root = None
def insert(self, data):
""" inserts new integer data into the tree at the position, so that
the binary search tree property is maintained.
"""
def _insert(root, data):
""" recursive internal method which works on node level """
if root is None:
root = Node(data)
elif data <= root.data:
root.left = _insert(root.left, data)
else:
root.right = _insert(root.right, data)
return root
self.root = _insert(self.root, data)
def lowest_common_ancestor(root, a, b):
""" lowest common ancestor in BST """
if root.data > max(a, b):
return lowest_common_ancestor(root.left, a, b)
if root.data < min(a, b):
return lowest_common_ancestor(root.right, a, b)
return root
def main():
""" operational function """
bst = BinarySearchTree()
for elem in [9, 5, 3, 10, 6, 1, 7, 4]:
bst.insert(elem)
lca_node = lowest_common_ancestor(bst.root, 5, 4)
print("lowest_common_ancestor for 5 and 4:", lca_node.data)
if __name__ == "__main__":
main()