Proofs: Binary search tree deletion
These are the human proofs of Binary search tree deletion, at full length.
BST deletion removes exactly the key and keeps the BST property full
This proof shows: correctness, termination.
Let be a binary search tree with the strict binary search tree property, correct parent pointers, nodes, and a map from its keys to their values. Let be a key. For a node write for the nodes of its subtree.
Claim. TREE-DELETE stops. If is not a key of , it returns false and changes nothing. Else it returns true, and afterwards the tree has the BST property, correct parent pointers, nodes, and represents the map without .
Termination. The search of line L1 and the walk of line L4 each move one level down per step, so each takes at most steps. The rest has no loops.
Not found. By the correctness of BST search, line L1 returns NIL exactly when is not a key, and then line L2 returns false before any change.
Lemma 1 (splice). Let be a node with at most one child, and let be that child or NIL. Lines L7 to L11 give a tree with correct parent pointers and the nodes of the old tree except , each with its key and value. Each pair where is in the left (right) subtree of in the new tree is a pair of the old tree with in the left (right) subtree of . In particular, if the old tree has the BST property, so does the new one.
Proof. Lines L9 to L11 make the parent of (or the root field) point to , and line L8 sets to the old parent of . No other link changes, so all other parent pointers stay correct, and the nodes that stay reachable are all nodes except . For the pairs, take a node and a node in its left subtree after the change (the right side is the mirror case). If is not an ancestor of in the old tree, its subtree did not change. If is an ancestor of , its subtree lost only , and the nodes of moved up one level but stay on the same side of as was, because . So was in the left subtree of in the old tree too.
Case of at most one child. If has at most one child at line L3, lines L4 to L6 are skipped, and Lemma 1 shows that the tree loses exactly the node of . The map loses exactly .
Case of two children. Let be the node of , with left subtree and right subtree , both non-empty. Line L4 sets to the node of with the smallest key, by the correctness of TREE-MINIMUM. Then $y.left = $ NIL, else the left child of would be a node of with a smaller key.
is the successor. Let and be the interval bounds of given by its ancestors, so that the keys of in are exactly the keys of . Each key of larger than and smaller than would be in , because and , so it would be in , and larger than , so in , and smaller than the minimum of , which is impossible. So no key of lies strictly between and .
The copy keeps the order at . After line L5, holds . Each key of is less than . Each key of other than is greater than . For each ancestor of , the key was already in , on the same side of , so the relation at still holds. Nodes inside and do not change. So, before the splice, each ancestor-descendant pair of nodes that does not involve the node satisfies the strict order condition. (The pair does not: both now hold .)
The splice. Line L6 sets , which has no left child. By Lemma 1, after lines L7 to L11 the node is gone, and each pair of the new tree is a pair of the tree before the splice, on the same side, that does not involve . So each such pair satisfies the strict condition, and the new tree has the BST property with distinct keys. The key set is the old one without : the value of is now at (line L5), and the key is no longer anywhere, because the only node that held it now holds .
Count. Line L12 subtracts 1 from , and exactly one node left the tree.