-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbst_inorder_successor.py
58 lines (47 loc) · 1.55 KB
/
bst_inorder_successor.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
#!/usr/bin/python
# Date: 2020-10-22
#
# Description:
# Write an algorithm to find the next node(i.e., inorder successor) of a given
# node in a binary search tree. We can assume that each node has link to its
# parent.
#
# Approach:
# - If given node has a right subtree then take smallest from right subtree
# - Otherwise keep on checking parent node, break when first greater than given
# is found
#
# Complexity:
# O(h) time, h is height of tree
class Node:
def __init__(self, data, parent=None):
self.data = data
self.left = None
self.right = None
self.parent = parent
def get_smallest(node):
while node.left:
node = node.left
return node.data
def get_inorder_successor_no_right_child(node):
parent = node.parent
while parent and parent.data < node.data:
parent = parent.parent
return parent.data if parent else None
def get_inorder_successor(node):
if node.right is None:
return get_inorder_successor_no_right_child(node)
return get_smallest(node.right)
def main():
root = Node(10)
root.left = Node(5, root)
root.right = Node(20, root)
root.left.left = Node(3, root.left)
root.left.left.left = Node(2, root.left.left)
root.left.left.right = Node(4, root.left.left)
root.right.right = Node(25, root.right)
assert get_inorder_successor(root) == 20 # (10)
assert get_inorder_successor(root.left) == 10 # (5)
assert get_inorder_successor(root.right) == 25 # (20)
assert get_inorder_successor(root.left.left.right) == 5 # (4)
main()