Sub topic 8
Successor and predecessor
This chapter assumes you can read the Node, insert, minimum, and in-order traversal code from earlier chapters. It also assumes you are comfortable with recursion and Big-O notation.
What they are
The successor of a key is the next larger key in the tree. The predecessor is the next smaller key. In the sorted output of an in-order traversal, they are the neighbors of the key on either side.
You can find both without running a full traversal. The ordering property tells you where to look.
The two cases for successor
Take a node x that holds the key you start from.
- If
xhas a right subtree, the successor is the minimum of that subtree. Every key there is larger thanx, and the leftmost one is the smallest of them. - If
xhas no right subtree, the successor is the lowest ancestor for whichxlies in the left subtree. Going down from the root, this is the last node where you turned left.
Our nodes have no parent pointers, so the code searches down from the root and remembers the last left turn along the way. Predecessor mirrors this: swap left and right, and use the maximum.
Code
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert(node, key):
if node is None:
return Node(key)
if key < node.key:
node.left = insert(node.left, key)
elif key > node.key:
node.right = insert(node.right, key)
return node
def minimum(node):
while node.left is not None:
node = node.left
return node
def maximum(node):
while node.right is not None:
node = node.right
return node
def successor(root, key):
node, candidate = root, None
while node is not None and node.key != key:
if key < node.key:
candidate = node # last left turn
node = node.left
else:
node = node.right
if node is None:
return None # key is not in the tree
if node.right is not None:
return minimum(node.right)
return candidate
def predecessor(root, key):
node, candidate = root, None
while node is not None and node.key != key:
if key > node.key:
candidate = node # last right turn
node = node.right
else:
node = node.left
if node is None:
return None
if node.left is not None:
return maximum(node.left)
return candidate
root = None
for k in [50, 30, 70, 20, 40, 60, 80]:
root = insert(root, k)
print(successor(root, 50).key) # 60
print(successor(root, 40).key) # 50
print(successor(root, 80)) # None
print(predecessor(root, 50).key) # 40
print(predecessor(root, 60).key) # 50Trace successor(root, 40). The search goes from 50 to 30 to 40. It turns left only at 50, so candidate is 50. The node 40 has no right child, so the answer is 50.
Cost
Each call walks one path from the root down, and then possibly continues down through a subtree. The path never visits a level twice, so the cost is O(h), where h is the tree height. This is much cheaper than the O(n) cost of traversing the whole tree.
Where you will use them
- Deletion. A node with two children is replaced by its successor, which is the minimum of its right subtree. That is case 1 above.
- Ordered iteration. Calling
successorrepeatedly walks the keys in sorted order, one at a time. - Neighbor queries. "What is the smallest key above 45?" is the same walk. It also works when the key is absent, if you drop the equality check and return
candidateonce you reachNone.
Exercise
In the tree built from [50, 30, 70, 20, 40, 60, 80], what are the successor and predecessor of 70? Which case applies to each?
Show answer
The successor is 80, because 70 has a right subtree and its minimum is 80 (case 1). The predecessor is 60, because 70 has a left subtree and its maximum is 60.
Exercise
Why does successor remember only the last left turn, not every left turn?
Show answer
Each left turn goes to a node with a smaller key than the previous left-turn node. The last one is the lowest ancestor whose left subtree contains the starting node, and that is the smallest key larger than the starting key.