Proofs: Red-black tree
These are the human proofs of Red-black tree, at full length.
Insertion and deletion keep the red-black properties full
This proof shows: correctness, rotations.insert, rotations.delete.
The properties are (R0) the search-tree order, (R1) the root is black, (R2) every leaf is black, (R3) a red node has two black children, and (R4) for each node , all paths from down to a leaf have the same number of black nodes. A path weight is the number of black nodes on a path. Rotations keep the in-order sequence of the keys (see tree rotation), so they keep (R0). Each recoloring and rotation below changes only nodes in a subtree whose root is at a fixed place; we check (R4) by comparing, for each leaf of , the weight of the path from the top of to that leaf before and after. If all these weights stay equal, then (R4) holds for each node above , because the paths from such a node agree outside .
Insertion
Initialization. Lines L1 to L7 are the insertion of a binary search tree, so (R0) holds and the key set gains (or line L4 returns and nothing changes, when is present). The new node is red, with two black children . It replaces a leaf: each path that ended at that leaf now goes on through the red to a black leaf, so its weight is unchanged, and (R4) and (R2) hold. The only possible failures are (R1), when the tree was empty and is the root, and (R3), when the parent of is red.
Invariant. At each test of line L9:
- (I1) is red.
- (I2) (R0), (R2) and (R4) hold.
- (I3) (R1) holds unless is the root.
- (I4) (R3) holds, except that and its parent may both be red.
This holds at the first test, by the initialization.
The grandparent. If the loop body runs, is red. By (I3), the root is black unless is the root, and the parent of the root is the black , so is not the root, and is a node. By (I4), the only red-red pair is and , so is black. Let , and assume that is the left child of (line L10); the other case is the mirror image (line L18). Let be the right child of , the uncle.
Case 1: is red (line L13). Here is the subtree of . Before, each path from to a leaf passes (black) and then exactly one of , (red). After the recoloring, it passes (red) and one of , (black). Nothing else changes, so each path weight from the top of is the same, and (R4) holds. Now and are black, and is red with two black children. The only possible red-red pair is and its parent. The new is , which is red, so (I1) and (I4) hold. The root changed color only if is the root, which is then , so (I3) holds.
Case 2: is black and is the right child of (line L15). Let be the left subtree of , and , the subtrees of . Since and are red, (R4) at says that all paths into , and have the same weight from their roots. The left rotation at makes the left child of , with left child ; has subtrees and , and has right subtree . Both rotated nodes are red, so each path from 's left child has the same weight as before. The new is the old : it is red, it is the left child of its red parent (the old ), and this is the only red-red pair, because , and have black roots (they were children of red nodes, other than the pair , ). So the state is that of case 3.
Case 3: is black and is the left child of (lines L16 and L17). Let be the right subtree of . The roots of and of the subtrees of are black by (I4), and is black. By (R4) at , with and red, the paths into the subtrees of , into , and into the subtree of (counting ) all have the same weight . Lines L16 and L17 paint black and red and rotate right at . Now is the root of , with children (red) and (red), and has children and . Each path from has weight : the black , then a red node, then a path of weight . Before, each path from had weight too: the black , then a red node or the path through . So (R4) holds. The red nodes and have black children, and is black, so (R3) holds everywhere. If was the root, then is the new root and it is black.
Termination. Case 1 moves up two levels, so it runs at most times in a row. After case 3, the parent of is the black , so the test of line L9 fails. At exit, is black, so by (I4) (R3) holds, and by (I3) (R1) can fail only at the root. Line L19 paints the root black. This adds one to the weight of every path from the root, so (R4) still holds, and it cannot create a red-red pair. So the tree satisfies (R0) to (R4). Cases 2 and 3 happen at most once, so an insertion does at most two rotations.
Deletion
Weights with an extra black. During DELETE-FIXUP, the node carries an extra black: in path weights, counts one more than its color says. Write (R4) for (R4) with this count.
Lemma 1 (RB-DELETE). Let be the node that leaves its place (line L23 or L27), its original color, and the node that takes its place. After lines L23 to L31, the tree holds the old keys without the key of , and (R0) and (R2) hold. If is red, (R1), (R3) and (R4) hold. If is black, (R4) holds, and (R3) holds except for a red-red pair that contains , and (R1) holds except that may be a red root.
Proof. Lines L23 to L31 are the deletion of a binary search tree: if has at most one child, that child replaces ; else the successor , which has no left child, is replaced by its right child , and then replaces . So (R0) holds and the key of is gone. Line L31 gives the color of , so at the old place of the colors are as before. The only change of colors on paths is that the node of color no longer lies above .
If is red, the path weights do not change, so (R4) holds. A red is not the root, so (R1) holds. Its parent was black and its children were black, so putting under the parent of makes no red-red pair. In the two-child case, gets the color of ; if that color is red, the children of are , and or , all of which were children of a red node, so they are black. So (R3) holds.
If is black, each path through the old place of lost one black node, and these are exactly the paths through . With the extra black at , each weight is as before, so (R4) holds. A red-red pair can appear only between and its new parent, and the root changes only if becomes the root.
Invariant. At each test of line L33:
- (J1) (R0), (R2) and (R4) hold.
- (J2) (R3) holds, except for red-red pairs that contain .
- (J3) (R1) holds, except that may be a red root.
By Lemma 1 this holds at the first test. When the loop body runs, is black and is not the root. So by (J2) there is no red-red pair at all, and by (J3) the root is black.
Assume is the left child of (line L34); the right case is the mirror image (line L45). Let be the right child of (line L35). The paths from have weight at least 2 (the black and its extra), so by (R4) at the paths from have weight at least 2, and is not .
Case 1: is red (line L37). Then and the children , of are black. Let be the weight of each path from (with the extra), which by (R4) at is also the weight of each path from , and so of each path from and from (the red adds nothing). Line L37 paints black and red and rotates left at . Now is the top, with children and , and has children and . Each path from the top has weight : through (black), then (red) and a path from or , or a path from . Before, each path from had weight too. So (R4) holds. The red has the black children and , so (J2) holds, and if was the root, the black is the new root. The new sibling of is , which is black, and is red. One of cases 2 to 4 follows.
Case 2: is black and both children of are black (line L39). Line L39 paints red and moves the extra black from to . Each path from through loses one, and each path through loses the extra; the extra at adds one to all paths through . So the weights from the top of the subtree of are unchanged, and (R4) holds with the new . The red has black children. If is red, then and form a red-red pair, which contains the new , as (J2) allows. If is the root, it is the new , as (J3) allows.
Case 3: is black, its left child is red and its right child is black (line L42). Let and be the children of ; they are black. By (R4) at , the paths from , from and from have the same weight . Line L42 swaps the colors of and and rotates right at . Now (black) has children and (red), and has children and . Each path from has weight ; before, each path from had weight . So (R4) holds. The red has black children and . The new sibling of is , black, and its right child is red. This is case 4.
Case 4: is black and its right child is red (lines L43 and L44). Let be the left child of , of any color, and let be the color of (1 if black, 0 if red). Let be the weight of each path from with the extra. By (R4) at and (R4) at , each path from and each path from has weight , and is red, so each path from a child of has weight . Before, each path from has weight . Lines L43 and L44 give the color , paint and black, rotate left at , and drop the extra black of . Now is the top, with children and , and has children and . A path from the top through and has weight . A path through and has weight . A path through has weight . All equal , so (R4) holds with no extra black anywhere. The nodes and are black. If is red, then was red, so the parent of the top is black, since there was no red-red pair. So (R3) holds everywhere. If was the root, is the root with the color of , which was black by (J3). Line L44 sets to the root, so the loop ends.
Termination. Case 2 moves up one level. After case 1, the parent of is red, so case 2 ends the loop, and cases 3 and 4 end it in at most two more steps. Case 3 leads to case 4, which ends the loop. So the loop runs at most times. It does at most three rotations: one in case 1, one in case 3 and one in case 4, and each of these cases happens at most once.
Exit. At exit, is red or is the root. Line L46 paints black and drops its extra black. If was red, the paths through gain one from the color and lose the extra, so (R4) holds, and the red-red pairs that contained are gone. If is a black root, dropping the extra subtracts one from every path from the root, which keeps (R4). In both cases (R1) to (R4) hold. If was red, DELETE-FIXUP is not called and Lemma 1 already gives the properties.
Conclusion
Theorem. Starting from the empty tree, after any sequence of RB-INSERT and RB-DELETE calls the tree satisfies (R0) to (R4) and holds exactly the keys inserted and not deleted. A search returns true exactly for these keys. Each insertion does at most two rotations, and each deletion at most three.
Proof. The empty tree, the sentinel alone, satisfies (R0) to (R4). The insertion and deletion parts above show that each operation keeps the properties and changes the key set as stated. A search is correct by (R0).