Sorted order
This binary search tree holds 8 keys. Every key in a node's left subtree is smaller, every key in its right subtree is bigger. Each node stands above its place in sorted order, so the row in front reads 20 to 80.
Successor and predecessor
The successor of a key is the next one in sorted order. 50 has a right subtree, so the answer is in there: its smallest key. Step right to 70, then go left until you can't: 60.
40 has no right subtree, so its successor is above it. Climb while you come up from a right child: 40 hangs right of 30. Then 30 hangs left of 50, so stop there: 50 is next.
The predecessor is the key just before, so swap left and right. 50 has a left subtree: step left to 30, then go right until you can't: 40. With no left subtree you would climb instead.
Deleting a node
To delete 20, search for it: left at 50, left at 30. Node 20 has no children, so line 7 returns its right link, which is None. 30's left link becomes None and the leaf just drops off.
Now delete 30. It has one child, 40, on its right. Line 7 returns that child, so 50's left link points straight at 40, which rises into 30's place. 40 is still smaller than 50: the order holds.
Delete 50, the root. It has two children, but only one node can take its place. Lines 10 and 11 pick its successor: right to 70, then left as far as possible: 60. It has no left child.
Line 12 copies 60 into the root. Everything on the left is still smaller than 60, and everything on the right is at least 60, so the order holds. For a moment 60 is in the tree twice.
Line 13 deletes 60 from the right subtree. That old node has no left child, so line 7 returns its right child: 65 rises under 70. The row now reads 40, 60, 65, 70, 80: still sorted.
Cost
Each operation here walked one path, down or up, so it costs O(h), where h is the tree's height: now 2. A balanced tree keeps h near log₂ n; one built from sorted keys is a line, with h = n − 1.
What happens
;; step 1/10 · bst-rule
insert order: 50, 30, 70, 20, 40, 60, 80, 65
in-order: 20, 30, 40, 50, 60, 65, 70, 80
height: 3 (longest root-to-leaf path, in links)
the program this figure runs (python3, as-is):
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None
def insert(t, k):
if t is None: return Node(k)
if k < t.key: t.left = insert(t.left, k)
else: t.right = insert(t.right, k)
return t
def delete(t, k):
if t is None: return t
if k < t.key:
t.left = delete(t.left, k)
elif k > t.key:
t.right = delete(t.right, k)
elif t.left is None: return t.right
elif t.right is None: return t.left
else:
s = t.right
while s.left: s = s.left
t.key = s.key
t.right = delete(t.right, s.key)
return t
def successor(t, k): # smallest key bigger than k
s = None
while t:
if k < t.key: s, t = t, t.left
else: t = t.right
return s
def predecessor(t, k): # biggest key smaller than k
p = None
while t:
if k > t.key: p, t = t, t.right
else: t = t.left
return p
def inorder(t):
return inorder(t.left) + [t.key] + inorder(t.right) if t else []
root = None
for k in [50, 30, 70, 20, 40, 60, 80, 65]:
root = insert(root, k)
print(inorder(root))
print(successor(root, 50).key, successor(root, 40).key, predecessor(root, 50).key)
for k in [20, 30, 50]:
root = delete(root, k)
print(inorder(root))
prints:
[20, 30, 40, 50, 60, 65, 70, 80]
60 50 40
[30, 40, 50, 60, 65, 70, 80]
[40, 50, 60, 65, 70, 80]
[40, 60, 65, 70, 80]
Each node is drawn above its key's place in sorted order, so the row in front is the in-order traversal. When the root takes the key 60 it moves to 60's column; the tree's links are what really changed. CLRS and MIT 6.006 find a successor with parent links (right then far left, or climb). The Python here has no parent links: it walks down from the root and keeps the last node where it turned left, which is the same node.