1def delete(t, k):2 if t is None: return t3 if k < t.key:4 t.left = delete(t.left, k)5 elif k > t.key:6 t.right = delete(t.right, k)7 elif t.left is None: return t.right8 elif t.right is None: return t.left9 else:10 s = t.right11 while s.left: s = s.left12 t.key = s.key13 t.right = delete(t.right, s.key)14 return t5030702040608065[0]20[1]30[2]40[3]50[4]60[5]65[6]70[7]80
Node blocks: one key each, standing above its place in sorted orderRow: the in-order traversal, read left to rightToken: the key a walk is looking forA binary search tree on a board, each node above its key's place in sorted order, so the row in front is the in-order traversal: a successor is the next cell, a predecessor the one before. Deleting a leaf drops it; a node with one child is replaced by that child; a node with two children takes its successor's key, and the old successor is then deleted by one of the first two cases.

Sorted order

  1. 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

Deleting a node

Cost

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.