Proofs: Persistent red-black tree
These are the human proofs of Persistent red-black tree, at full length.
Functional insertion and deletion keep the red-black invariant full
This proof shows: correctness.
We use the notation of the page. is the set of keys of . means that meets (R1) search order, (R2) no red node has a red child, and (R3) equal black counts on all paths, with black height . means that meets (R1) and (R3) with black height , and that it meets (R2) except perhaps at its root: either , or is red and both its children meet . Each claim on the keys below is that the keys are in order and that the key set is as stated; the order is kept because each NODE call puts keys in the order they have in its arguments, which we check once per case and then do not repeat.
Since no procedure writes a field of an existing node, a call never changes its arguments, and every old version keeps its tree. So it is enough to prove that each call returns the right tree.
Three facts are used often. (F1) If and is red, its children are black or nil and meet . (F2) If and is black, its children meet . (F3) If and , then BLACKEN meets with a black root, where if was red and if it was black. A nil tree has black height 0, and a tree of black height is not nil.
BALANCE
Lemma 1. Let and , with at least one of , meeting , and . Then $t = $ BALANCE meets with , and its root is black, or red with two black children.
Proof. Line L10: and are red. BLACKEN of an infrared red tree of black height is a black-rooted tree of black height (F3). The new red root has two black children of black height , so it meets (R2) and (R3) with black height .
Line L11: is red with a red left child, and line L10 did not apply, so is not red. Since breaks (R2) at its root, is not , so . As is infrared, its children and meet . BLACKEN is black with black height (F3). The node NODE(black, , , ) has two children of black height that meet RB, so it meets . The red root has two black children of black height . The keys are , which is the order of the new tree.
Lines L12, L13 and L14 are the same argument for the other three places of the red pair. In line L12, is red and, as a child of the infrared tree , meets , and its children and meet by (F1); the two new black nodes have children of black height . Lines L13 and L14 are the mirror images of L11 and L12, with in the role of .
Line L15: no case applied. If is red, its children are not red (else L11 or L12), so ; the same holds for . A black root over two trees of black height meets .
Insertion
Lemma 2. Let and . Then . If is black, INS meets . If is red or nil, it meets .
Proof. By induction on the size of . If , line L3 returns a red leaf, which meets and so . Let (lines L4 to L6); the case is the mirror image.
If is black, its children meet (F2). By induction INS meets (for either color of , since RB implies IR), and meets . Lemma 1 gives for line L5.
If is red, its children are black or nil and meet (F1). By induction INS meets when is black; when is nil it is a red leaf, which also meets with . So line L6 makes a red root over two trees of black height , which is .
Theorem 1. If is a version (it meets (R1) to (R4)), INSERT is a version with keys .
Proof. If , line L1 returns . Else Lemma 2 gives an infrared tree with the right keys (a black root gives RB, which is infrared), and BLACKEN at line L2 gives (R1) to (R4) by (F3).
Deletion
Lemma 3 (BAL-LEFT). Let and with , and . Then $t = $ BAL-LEFT meets with . If is black, meets .
Proof. Since , is not nil.
Line L25: is red. BLACKEN meets with a black root (F3). The new red root has the black left child BLACKEN and the right child , both of black height . So meets , and if is black, it meets (R2) at the root as well, so .
Line L26: is not red, so is black or nil and meets . The node is black, so its children meet (F2), and REDDEN is a red node over them, which meets . Lemma 1 applies, with as the RB argument, and gives .
Line L28: is not red, so , and is red. By (F1) the children of are black or nil with black height , so they are black nodes; let , and note that is black. By (F2) the children of meet . The node NODE(black, , , ) has two RB children of black height , so it meets . REDDEN is a red node over the two children of , which meet by (F2), so it is . And meets . Lemma 1 gives that BALANCE meets . The red root over two RB trees of black height meets . The keys are , which is the order of the new tree.
Lemma 3' (BAL-RIGHT). The mirror image: if and with , then BAL-RIGHT meets , and if is black. The proof is Lemma 3 with left and right exchanged, line by line (L29 for L25, L30 for L26, L31 for L27 and L28).
Lemma 4 (FUSE). Let and with . Then $t = $ FUSE meets with . If each of and is black or nil, meets .
Proof. By induction on the total size of and .
Lines L32 and L33: the result is the other tree, which meets .
Lines L34 to L37: and are red. By (F1), and are black or nil with black height , so by induction $c = $ FUSE meets . If is red (line L36), its children are black or nil and meet (F1); the two new red nodes each have two black-or-nil RB children of black height ( and , and and ), so they meet , and the red root over them is . If is not red (line L37), the new red node over and meets , and the red root over (black or nil) and it is .
Lines L38 to L41: and are black, so , and by (F2) and meet . By induction meets . If is red (line L40), its children meet by the definition of IR, so each new black node has two RB children of black height and meets , and the red root over two black nodes meets . If is not red (line L41), then , so the node NODE(black, , , ) meets and is black. Lemma 3 applies with , which meets and so , and gives , because its right argument is black.
Line L42: is red and, since lines L34 and L38 did not apply, is black. By (F1), is black or nil with black height , so by induction FUSE meets (both arguments black or nil). The red root over it and (black or nil, RB of black height ) meets . Line L43 is the mirror image, with red and black.
In each case, the keys of are the keys of followed by the keys of , in order, since each new node puts the keys of before those of and those of before those of .
Lemma 5 (DEL). Let and . Then . If is black, DEL meets . If is red, it meets .
Proof. By induction on the size of . Since , is not nil, and each recursive call is on a child that holds , so it is not nil either.
Case , black (line L19). Let , so as well. By induction DEL meets . Lemma 3 gives , and if is black. If is black, , and the result is . If is red, , and is black by (F1) and not nil (its black height is ), so the result is .
Case , not black (line L20). The child holds , so it is not nil, and it is red. Then is black by (R2), and . By induction DEL meets , and meets by (F2). A red root over two RB trees of black height is , as required for black .
Case (lines L22 and L23). The mirror image, with Lemma 3'.
Case (line L24). Let . Lemma 4 gives with keys . If is black, , so this is . If is red, both children are black or nil (F1), Lemma 4 gives , and .
Theorem 2. If is a version, DELETE is a version with keys .
Proof. If , line L16 returns . Else is black by (R4), so Lemma 5 gives an infrared tree with the right keys, and BLACKEN at line L17 gives a version by (F3), or nil, which is a version.
Persistence
Corollary. For any sequence of insertions and deletions in which each update takes any earlier version, every version is a red-black tree with the keys that the updates on its chain from the empty tree give, and CONTAINS answers correctly on it.
Proof. By induction on the number of updates, with Theorems 1 and 2, and the fact that no call writes to an existing node, so no update changes an earlier version. CONTAINS is the search of a binary search tree, which is correct by (R1).