Algorithm Search⌘K

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 TT be a binary search tree with the strict binary search tree property, correct parent pointers, nn nodes, and a map MM from its keys to their values. Let kk be a key. For a node uu write sub(u)\mathrm{sub}(u) for the nodes of its subtree.

Claim. TREE-DELETE(T,k)(T, k) stops. If kk is not a key of TT, it returns false and changes nothing. Else it returns true, and afterwards the tree has the BST property, correct parent pointers, n−1n - 1 nodes, and represents the map MM without kk.

Termination. The search of line L1 and the walk of line L4 each move one level down per step, so each takes at most h+1h + 1 steps. The rest has no loops.

Not found. By the correctness of BST search, line L1 returns NIL exactly when kk is not a key, and then line L2 returns false before any change.

Lemma 1 (splice). Let zz be a node with at most one child, and let cc be that child or NIL. Lines L7 to L11 give a tree with correct parent pointers and the nodes of the old tree except zz, each with its key and value. Each pair (u,w)(u, w) where ww is in the left (right) subtree of uu in the new tree is a pair of the old tree with ww in the left (right) subtree of uu. In particular, if the old tree has the BST property, so does the new one.

Proof. Lines L9 to L11 make the parent of zz (or the root field) point to cc, and line L8 sets c.pc.p to the old parent of zz. No other link changes, so all other parent pointers stay correct, and the nodes that stay reachable are all nodes except zz. For the pairs, take a node uu and a node ww in its left subtree after the change (the right side is the mirror case). If uu is not an ancestor of zz in the old tree, its subtree did not change. If uu is an ancestor of zz, its subtree lost only zz, and the nodes of sub(c)\mathrm{sub}(c) moved up one level but stay on the same side of uu as zz was, because sub(c)⊆sub(z)\mathrm{sub}(c) \subseteq \mathrm{sub}(z). So ww was in the left subtree of uu in the old tree too. □\square

Case of at most one child. If zz 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 kk. The map loses exactly kk.

Case of two children. Let z0z_0 be the node of kk, with left subtree AA and right subtree BB, both non-empty. Line L4 sets yy to the node of BB with the smallest key, by the correctness of TREE-MINIMUM. Then $y.left = $ NIL, else the left child of yy would be a node of BB with a smaller key.

yy is the successor. Let a=a(z0)a = a(z_0) and b=b(z0)b = b(z_0) be the interval bounds of z0z_0 given by its ancestors, so that the keys of TT in (a,b)(a, b) are exactly the keys of sub(z0)\mathrm{sub}(z_0). Each key of TT larger than kk and smaller than y.keyy.key would be in (a,b)(a, b), because a<ka < k and y.key<by.key < b, so it would be in sub(z0)\mathrm{sub}(z_0), and larger than kk, so in BB, and smaller than the minimum of BB, which is impossible. So no key of TT lies strictly between kk and y.keyy.key.

The copy keeps the order at z0z_0. After line L5, z0z_0 holds y.keyy.key. Each key of AA is less than k<y.keyk < y.key. Each key of BB other than y.keyy.key is greater than y.keyy.key. For each ancestor uu of z0z_0, the key y.keyy.key was already in sub(z0)\mathrm{sub}(z_0), on the same side of uu, so the relation at uu still holds. Nodes inside AA and BB do not change. So, before the splice, each ancestor-descendant pair of nodes that does not involve the node yy satisfies the strict order condition. (The pair (z0,y)(z_0, y) does not: both now hold y.keyy.key.)

The splice. Line L6 sets z=yz = y, which has no left child. By Lemma 1, after lines L7 to L11 the node yy 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 yy. 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 kk: the value of y.keyy.key is now at z0z_0 (line L5), and the key kk is no longer anywhere, because the only node that held it now holds y.keyy.key.

Count. Line L12 subtracts 1 from T.nT.n, and exactly one node left the tree. ■\blacksquare

Back to Binary search tree deletion