-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_least_common_parent.py
92 lines (79 loc) · 2.89 KB
/
find_least_common_parent.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from python_bst import *
import os,sys,random
import numpy as np
def preorderTravese(preorderlist,node):
if node is None:
return
nodevalue(preorderlist,node)
preorderTravese(preorderlist,node.left)
preorderTravese(preorderlist,node.right)
def createMatrix(root):
height = getHeight(0,root);
_grid_shape = (height,height)
adjmatrix = np.zeros(_grid_shape);
print 'height' + str(height)
return adjmatrix
def createList(adjList,node):
if node.left is not None:
adjList.append((node.key,node.left.key))
adjList.append(createList(adjList,node.left))
if node.right is not None:
adjList.append((node.key,node.right.key))
adjList.append(createList(adjList,node.right))
return adjList
def getHeight(height,node):
height1=0;
height2=0;
if node.left is not None:
height1 = getHeight(height,node.left)
if node.right is not None:
height2 = getHeight(height,node.right)
height += height1+1 if (height1>=height2) else height2+1;
return height;
def nodevalue(preorderlist,node):
preorderlist.append(node.key)
def allAncestors(root,node):
ancestorsList = list()
while node is not None and node.parent is not None:
ancestorsList.append(node.parent)
node = node.parent
return ancestorsList
def findLeastCommonParentineff(root,node1Ancestors,node2Ancestors):
if type(node1Ancestors) is list and type(node2Ancestors) is list:
for node1ancestoritem in node1Ancestors:
for node2ancestoritem in node2Ancestors:
if node1ancestoritem.key == node2ancestoritem.key:
return node1ancestoritem
else:
return root
def findLeastCommonParent(root,node1,node2):
#adjmatrix = createMatrix(root)
adjList = list()
adjList = createList(adjList,root)
print adjList
def findin(tree):
if tree is not None:
preorderlist=list()
print 'root:' + str(tree.root.key)
nodekey1 = raw_input('enter the two nodes to check: ')
nodekey2 = raw_input()
node1 = tree.find(int(nodekey1))
node2 = tree.find(int(nodekey2))
if(node1 is not None and node2 is not None):
print 'all is well'
node1Ancestors = allAncestors(tree.root,node1)
node2Ancestors = allAncestors(tree.root,node2)
leastCommonParentineff = findLeastCommonParentineff(tree.root,node1Ancestors,node2Ancestors)
findLeastCommonParent(tree.root,node1,node2)
print 'least common parent ' + str(leastCommonParentineff.key)
else:
print 'invalid nodekey1 or nodekey2 value'
else:
print 'please input valid tree'
def runTest(args=None,BSTtype=BST):
tree = BSTtype()
with open('./bst_input.txt') as f:
args = f.read().split()
tree = test(args)
findin(tree)
if __name__ == '__main__': runTest()