-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh46Successor.py
More file actions
69 lines (51 loc) · 1.91 KB
/
Ch46Successor.py
File metadata and controls
69 lines (51 loc) · 1.91 KB
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
# Successor: Write an algorithm to find the "next" node (i.e. in-order successor) of a given node in a binary search tree. You may assume that each node has a link to its parent.
from Ch42MinTree import produceTree, TreeNode, traverse
from binarytree import setup, pprint, Node
def createParents(root):
if getattr(root, 'parent', None) == None:
root.parent = None
if root.left:
root.left.parent = root
createParents(root.left)
if root.right:
root.right.parent = root
createParents(root.right)
def findParent(node, parent):
if parent == None:
print('reached end no parent')
return None
if parent.data > node.data:
print('parent greater than node, returning parent:', parent.data)
return parent
print("continuing search for a parent")
return findParent(node, parent.parent)
def findSuccessor(node, successor=None):
print('looking for successor to:', node.data, '\n', 'current successor: ', getattr(successor, 'data', None))
if successor == None:
if node.right != None:
print('successor set to below right')
successor = node.right
else:
print('searching for parent')
return findParent(node, node.parent)
if successor != None and successor.left:
print('traverse down left')
return findSuccessor(node, successor.left)
else:
print('found successor', successor.data)
return successor
if __name__ == "__main__":
root = produceTree(list(range(1,30)))
createParents(root)
# traverse(root)
# print(findSuccessor(root.right).data)
setup(
node_init_func=lambda v: TreeNode(v),
node_class=TreeNode,
null_value=None,
value_attr='data',
left_attr='left',
right_attr='right'
)
pprint(root)
print(findSuccessor(root.left.left.right.left))