Proofs: Red-black tree deletion
These are the human proofs of Red-black tree deletion, at full length.
Deletion keeps the left-leaning red-black rules full
This proof shows: correctness.
The rules are (L1) search order, (L2) no red right child, (L3) no red node with a red left child, (L4) the same number of black nodes on every path from the root to nil, and (L5) a black root. For a node , write for the number of black nodes on a path from down to nil, counting itself, with ; it is well defined when (L4) holds in the subtree of . Call a subtree valid when (L1) to (L4) hold in it; its root may be red. In a valid subtree, a red node has two black children: the left one by (L3), the right one by (L2).
Lemma A (the steps). A rotation of a red link, where the child moves up, takes the color of the old top, and the old top becomes red, keeps (L1), keeps (L4) in the subtree, and keeps of the top. A color flip at a node whose two children both have the color opposite to its own keeps (L1), (L4) and of the top.
Proof. A rotation keeps the in-order sequence of the keys. For ROTATE-LEFT with red, let be the color of and , , the subtrees , , . Before, a path from the top to , or meets the node of color and, for and , the red . After, it meets the node of color and, for and , the red . Red nodes add nothing, so each path has the same count. ROTATE-RIGHT is the mirror image. A flip at a black node with two red children makes the node red and the children black: a path from the top meets one black node before (the top) and one after (a child). The other flip is the reverse. Neither step changes keys.
A call REMOVE has the precondition that , that (L1), (L3), (L4) hold in the subtree of and (L2) holds at every node except possibly at , and that one of these holds:
- (P1) is red, with two black children;
- (P2) is black, is red and is black;
- (P3) is black, is black, is red, and .
In the 2-3 view, (P1) and (P2) say that is not a 2-node, so a key can leave it. The postcondition is that the result is valid, with keys and , and that is black if was black when the call began. DELETE-MIN has the same precondition with (P1) or (P2), with the least key, and the same postcondition.
Lemma 2 (FIX-UP in deletion). If both children of are valid with the same , and if is red only when both children are black, then FIX-UP returns a valid subtree with the same keys and . If is black and is black, the result is itself, black.
Proof. If is red, its children are black and no test is true. If is black: with two black children nothing changes; with a red left child and a black right child, line L23 does not apply, since the left child is valid and red, so its left child is black; with a black left child and a red right child, line L22 rotates left and gives a black top with a red left child whose two children are black, as in Lemma 1; with two red children, line L24 flips and gives a red top with two black children.
Lemma 3 (MOVE-RED-LEFT). Let be red with valid black children and , and let be black. After MOVE-RED-LEFT, the subtree has the same keys and , (L1) and (L4) hold, and either (a) the top is , black, with two red children, each with two black children, or (b) the top is red, its right child is black and valid, and its left child is the old , black, with the red left child (whose children are black) and a black right child.
Proof. Line L5 flips at , a red node with two black children: is black, and and are red. The children of are black ( by assumption, by (L2)), and is black by (L2). If is black, the result is (a). Else let , red, with two black children. Line L6 rotates right at : takes the red color of and becomes its red right child, whose left child is the old , black. Then it rotates left at : moves up with the black color of , becomes its red left child, and the right child of is the old , black. Then it flips at , a black node with two red children: is red, and and are black. Now has the red left child and a black right child, and has two black children. Every step is one of Lemma A.
Lemma 4 (MOVE-RED-RIGHT). Let be red with valid black children and , and let be black. After MOVE-RED-RIGHT, the subtree has the same keys and , (L1) and (L4) hold, and either (a) the top is , black, with two red children, each with two black children, or (b) the top is red with two black children; is valid, and is the old , black, with a black left child and the red right child , whose children are black; and .
Proof. Line L7 flips at : is black, and and are red. The children of are black ( by assumption, by (L2)), and is black by (L2). If is black, the result is (a). Else is red, with black children. Line L8 rotates right at : moves up with the black color of , becomes its red right child, and the left child of is the old , black. Then it flips at , a black node with two red children: is red, is black, and is black. The node keeps the red right child . Since was the left child of , .
Lemma 5 (REMOVE and DELETE-MIN). Under the precondition, REMOVE and DELETE-MIN meet the postcondition.
Proof. By induction on the size. In every case below, the recursive call meets the precondition, so by induction it returns a valid subtree with the same as the child it replaces; then Lemma 2 applies to the last line, and we check its two conditions and the color of the result.
Case (lines L9 to L11). Since (P3) needs , this case has (P1) or (P2). The key is in , which is not nil.
- (P2): is red with black children, so line L10 does nothing and the call on has (P1). Line L11 replaces ; the right child of is black and unchanged. By Lemma 2, the result is , black.
- (P1) and red: line L10 does nothing. The call on the black has (P2) and returns a black node. At FIX-UP, is red with two black children, so Lemma 2 applies.
- (P1) and black: is not nil, since . Line L10 calls MOVE-RED-LEFT. In Lemma 3 (a), the call on the red left child has (P1), and at FIX-UP the top is black. In Lemma 3 (b), is less than the key of the old , which is the left child of the new top, so the recursion goes there, with (P2), and returns a black node; at FIX-UP the top is red with two black children.
Case (lines L12 to L16).
- (P2): line L12 rotates right at . The new top is the old , black, with the black left child (by (L3)) and the red right child , whose children are black. Now , line L13 does not apply ( nil), line L14 does nothing ( is red), and line L16 calls REMOVE on the red , which has (P1). At FIX-UP, is black with a black left child, so Lemma 2 gives a black result.
- (P3): line L12 does nothing, line L13 does not apply ( nil), and line L14 does nothing ( is red). The red right child has two black children, by (L3) and (L2) below . If , line L15 calls DELETE-MIN on it, and else line L16 calls REMOVE on it; both calls have (P1). At FIX-UP, is black with a black left child, and the result is black.
- (P1): line L12 does nothing. If and is nil, then and is black, so is nil: is a red leaf, and line L13 returns nil, with . Else is not nil, and neither is , by (L4). If is red, line L14 does nothing, and the call at line L15 or L16 on the black has (P2) and returns a black node; at FIX-UP, is red with two black children. If is black, line L14 calls MOVE-RED-RIGHT. In Lemma 4 (a), the call on the red right child has (P1), and the top is black at FIX-UP. In Lemma 4 (b), the new top has , so line L15 does not apply, and line L16 calls REMOVE on the old , which is black, with a black left child and a red right child, and : (P3). It returns a black node, so at the FIX-UP of , is red with two black children.
When line L15 runs, becomes the least key of . Every key of is less than , and every other key of is greater than , so after DELETE-MIN removes from , (L1) holds and the key is gone.
DELETE-MIN. If is nil (line L18), then (P2) is impossible, so is red, and as above is a red leaf and the result is nil. Else lines L19 to L21 run the case above, with DELETE-MIN in place of REMOVE.
In every case the keys are , and is kept, by Lemma A, by induction and by Lemma 2. A result that comes from a call with black is black, as shown in each case.
DELETE. Line L1 makes sure that is in the tree. Before line L2 the tree is valid with a black root. If both children of the root are black, line L2 paints the root red, which subtracts one from every path and so keeps (L4); now (P1) holds. Else the root has a red child, which is the left one by (L2), and (P2) holds. By Lemma 5, REMOVE returns a valid tree without . Line L4 paints the root black, which keeps (L4) and gives (L5).
Conclusion
Theorem. If is a left-leaning red-black tree, then after DELETE it is a left-leaning red-black tree whose keys are the old keys without .
Proof. If is not in , line L1 returns with no change. Else this is the paragraph on DELETE above, with Lemma 5. Each call of REMOVE or DELETE-MIN moves one level down, so the procedure stops after at most calls, where is the height.