Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3c11a76

Browse filesBrowse files
authored
Merge pull request AllAlgorithms#42 from anish03/lca
Added python implementation for lowest common ancestor
2 parents 7698ca1 + 05e4a1c commit 3c11a76
Copy full SHA for 3c11a76

File tree

Expand file treeCollapse file tree

1 file changed

+104
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+104
-0
lines changed

‎trees/lowest_common_ancestor.py

Copy file name to clipboard
+104Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
class Node:
2+
def __init__(self,data):
3+
self.data = data
4+
self.left = None
5+
self.right = None
6+
7+
class BST:
8+
def __init__(self):
9+
self.root = None
10+
11+
def getRoot(self):
12+
return self.root
13+
14+
def add(self,data):
15+
if not self.root:
16+
self.root = Node(data)
17+
else:
18+
self._add(data,self.root)
19+
20+
def _add(self,data,node):
21+
if data <= node.data:
22+
if node.left:
23+
self._add(data,node.left)
24+
else:
25+
node.left = Node(data)
26+
else:
27+
if node.right:
28+
self._add(data,node.right)
29+
else:
30+
node.right = Node(data)
31+
32+
def find(self,data):
33+
if self.root:
34+
self._find(data,self.root)
35+
else:
36+
return None
37+
38+
def _find(data,node):
39+
if data == node.data:
40+
return node
41+
elif node.left and data <= node.data:
42+
return self._find(data,node.left)
43+
elif node.right and data > node.data:
44+
return self._find(data,node.right)
45+
46+
def inorder(self,node):
47+
if node:
48+
self.inorder(node.left)
49+
print node.data
50+
self.inorder(node.right)
51+
52+
def get_height(self,node):
53+
if not node:
54+
return 0
55+
else:
56+
i = max(self.get_height(node.left),self.get_height(node.right)) + 1
57+
return i
58+
59+
def find_path(self,root,path,k):
60+
if not root:
61+
return False
62+
63+
path.append(root.data)
64+
65+
if root.data == k:
66+
return True
67+
68+
if (root.left and self.find_path(root.left,path,k) or root.right and self.find_path(root.right,path,k)):
69+
return True
70+
71+
path.pop()
72+
return False
73+
74+
def Driver(self,root,n1,n2):
75+
path1,path2 = [],[]
76+
77+
if (not self.find_path(root,path1,n1) or not self.find_path(root,path2,n2)):
78+
return -1
79+
80+
i = 0
81+
while i < len(path1) and i < len(path2):
82+
if path1[i] != path2[i]:
83+
break
84+
i += 1
85+
return path1[i-1]
86+
87+
def main():
88+
89+
B = BST()
90+
91+
B.add(8)
92+
B.add(3)
93+
B.add(10)
94+
B.add(1)
95+
B.add(6)
96+
B.add(14)
97+
B.add(4)
98+
B.add(7)
99+
B.add(13)
100+
101+
print B.Driver(B.root,1,6)
102+
103+
if __name__ == '__main__':
104+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.