Algorithm Search⌘K

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. K(t)K(t) is the set of keys of tt. RB(t,h)\text{RB}(t, h) means that tt meets (R1) search order, (R2) no red node has a red child, and (R3) equal black counts on all paths, with black height hh. IR(t,h)\text{IR}(t, h) means that tt meets (R1) and (R3) with black height hh, and that it meets (R2) except perhaps at its root: either RB(t,h)\text{RB}(t, h), or tt is red and both its children meet RB(⋅,h)\text{RB}(\cdot, h). 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 RB(t,h)\text{RB}(t, h) and tt is red, its children are black or nil and meet RB(⋅,h)\text{RB}(\cdot, h). (F2) If RB(t,h)\text{RB}(t, h) and tt is black, its children meet RB(⋅,h−1)\text{RB}(\cdot, h - 1). (F3) If IR(t,h)\text{IR}(t, h) and t≠nilt \ne \text{nil}, then BLACKEN(t)(t) meets RB(⋅,h′)\text{RB}(\cdot, h') with a black root, where h′=h+1h' = h + 1 if tt was red and h′=hh' = h if it was black. A nil tree has black height 0, and a tree of black height h≥1h \ge 1 is not nil.

BALANCE

Lemma 1. Let IR(a,h)\text{IR}(a, h) and IR(b,h)\text{IR}(b, h), with at least one of aa, bb meeting RB(⋅,h)\text{RB}(\cdot, h), and K(a)<x<K(b)K(a) < x < K(b). Then $t = $ BALANCE(a,x,b)(a, x, b) meets RB(t,h+1)\text{RB}(t, h + 1) with K(t)=K(a)∪{x}∪K(b)K(t) = K(a) \cup \{x\} \cup K(b), and its root is black, or red with two black children.

Proof. Line L10: aa and bb are red. BLACKEN of an infrared red tree of black height hh is a black-rooted RB\text{RB} tree of black height h+1h + 1 (F3). The new red root has two black children of black height h+1h + 1, so it meets (R2) and (R3) with black height h+1h + 1.

Line L11: aa is red with a red left child, and line L10 did not apply, so bb is not red. Since aa breaks (R2) at its root, aa is not RB\text{RB}, so RB(b,h)\text{RB}(b, h). As aa is infrared, its children a.lefta.left and a.righta.right meet RB(⋅,h)\text{RB}(\cdot, h). BLACKEN(a.left)(a.left) is black with black height h+1h + 1 (F3). The node NODE(black, a.righta.right, xx, bb) has two children of black height hh that meet RB, so it meets RB(⋅,h+1)\text{RB}(\cdot, h + 1). The red root has two black children of black height h+1h + 1. The keys are K(a.left)<a.key<K(a.right)<x<K(b)K(a.left) < a.key < K(a.right) < x < K(b), 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, a.righta.right is red and, as a child of the infrared tree aa, meets RB(⋅,h)\text{RB}(\cdot, h), and its children a.right.lefta.right.left and a.right.righta.right.right meet RB(⋅,h)\text{RB}(\cdot, h) by (F1); the two new black nodes have children of black height hh. Lines L13 and L14 are the mirror images of L11 and L12, with aa in the role of bb.

Line L15: no case applied. If aa is red, its children are not red (else L11 or L12), so RB(a,h)\text{RB}(a, h); the same holds for bb. A black root over two RB\text{RB} trees of black height hh meets RB(⋅,h+1)\text{RB}(\cdot, h + 1). □\square

Insertion

Lemma 2. Let RB(t,h)\text{RB}(t, h) and k∉K(t)k \notin K(t). Then K(INS(t,k))=K(t)∪{k}K(\text{INS}(t, k)) = K(t) \cup \{k\}. If tt is black, INS(t,k)(t, k) meets RB(⋅,h)\text{RB}(\cdot, h). If tt is red or nil, it meets IR(⋅,h)\text{IR}(\cdot, h).

Proof. By induction on the size of tt. If t=nilt = \text{nil}, line L3 returns a red leaf, which meets RB(⋅,0)\text{RB}(\cdot, 0) and so IR(⋅,0)\text{IR}(\cdot, 0). Let k<t.keyk < t.key (lines L4 to L6); the case k>t.keyk > t.key is the mirror image.

If tt is black, its children meet RB(⋅,h−1)\text{RB}(\cdot, h - 1) (F2). By induction INS(t.left,k)(t.left, k) meets IR(⋅,h−1)\text{IR}(\cdot, h - 1) (for either color of t.leftt.left, since RB implies IR), and t.rightt.right meets RB(⋅,h−1)\text{RB}(\cdot, h - 1). Lemma 1 gives RB(⋅,h)\text{RB}(\cdot, h) for line L5.

If tt is red, its children are black or nil and meet RB(⋅,h)\text{RB}(\cdot, h) (F1). By induction INS(t.left,k)(t.left, k) meets RB(⋅,h)\text{RB}(\cdot, h) when t.leftt.left is black; when t.leftt.left is nil it is a red leaf, which also meets RB(⋅,0)\text{RB}(\cdot, 0) with h=0h = 0. So line L6 makes a red root over two RB\text{RB} trees of black height hh, which is IR(⋅,h)\text{IR}(\cdot, h). □\square

Theorem 1. If tt is a version (it meets (R1) to (R4)), INSERT(t,k)(t, k) is a version with keys K(t)∪{k}K(t) \cup \{k\}.

Proof. If k∈K(t)k \in K(t), line L1 returns tt. 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). □\square

Deletion

Lemma 3 (BAL-LEFT). Let IR(l,h−1)\text{IR}(l, h - 1) and RB(r,h)\text{RB}(r, h) with h≥1h \ge 1, and K(l)<x<K(r)K(l) < x < K(r). Then $t = $ BAL-LEFT(l,x,r)(l, x, r) meets IR(t,h)\text{IR}(t, h) with K(t)=K(l)∪{x}∪K(r)K(t) = K(l) \cup \{x\} \cup K(r). If rr is black, tt meets RB(t,h)\text{RB}(t, h).

Proof. Since h≥1h \ge 1, rr is not nil.

Line L25: ll is red. BLACKEN(l)(l) meets RB(⋅,h)\text{RB}(\cdot, h) with a black root (F3). The new red root has the black left child BLACKEN(l)(l) and the right child rr, both of black height hh. So tt meets IR(t,h)\text{IR}(t, h), and if rr is black, it meets (R2) at the root as well, so RB(t,h)\text{RB}(t, h).

Line L26: ll is not red, so ll is black or nil and meets RB(l,h−1)\text{RB}(l, h - 1). The node rr is black, so its children meet RB(⋅,h−1)\text{RB}(\cdot, h - 1) (F2), and REDDEN(r)(r) is a red node over them, which meets IR(⋅,h−1)\text{IR}(\cdot, h - 1). Lemma 1 applies, with ll as the RB argument, and gives RB(t,h)\text{RB}(t, h).

Line L28: ll is not red, so RB(l,h−1)\text{RB}(l, h - 1), and rr is red. By (F1) the children of rr are black or nil with black height h≥1h \ge 1, so they are black nodes; let a=r.lefta = r.left, and note that r.rightr.right is black. By (F2) the children of aa meet RB(⋅,h−1)\text{RB}(\cdot, h - 1). The node NODE(black, ll, xx, a.lefta.left) has two RB children of black height h−1h - 1, so it meets RB(⋅,h)\text{RB}(\cdot, h). REDDEN(r.right)(r.right) is a red node over the two children of r.rightr.right, which meet RB(⋅,h−1)\text{RB}(\cdot, h - 1) by (F2), so it is IR(⋅,h−1)\text{IR}(\cdot, h - 1). And a.righta.right meets RB(⋅,h−1)\text{RB}(\cdot, h - 1). Lemma 1 gives that BALANCE(a.right,r.key,REDDEN(r.right))(a.right, r.key, \text{REDDEN}(r.right)) meets RB(⋅,h)\text{RB}(\cdot, h). The red root over two RB trees of black height hh meets IR(t,h)\text{IR}(t, h). The keys are K(l)<x<K(a.left)<a.key<K(a.right)<r.key<K(r.right)K(l) < x < K(a.left) < a.key < K(a.right) < r.key < K(r.right), which is the order of the new tree. □\square

Lemma 3' (BAL-RIGHT). The mirror image: if RB(l,h)\text{RB}(l, h) and IR(r,h−1)\text{IR}(r, h - 1) with h≥1h \ge 1, then BAL-RIGHT(l,x,r)(l, x, r) meets IR(⋅,h)\text{IR}(\cdot, h), and RB(⋅,h)\text{RB}(\cdot, h) if ll 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 RB(a,h)\text{RB}(a, h) and RB(b,h)\text{RB}(b, h) with K(a)<K(b)K(a) < K(b). Then $t = $ FUSE(a,b)(a, b) meets IR(t,h)\text{IR}(t, h) with K(t)=K(a)∪K(b)K(t) = K(a) \cup K(b). If each of aa and bb is black or nil, tt meets RB(t,h)\text{RB}(t, h).

Proof. By induction on the total size of aa and bb.

Lines L32 and L33: the result is the other tree, which meets RB(⋅,h)\text{RB}(\cdot, h).

Lines L34 to L37: aa and bb are red. By (F1), a.righta.right and b.leftb.left are black or nil with black height hh, so by induction $c = $ FUSE(a.right,b.left)(a.right, b.left) meets RB(c,h)\text{RB}(c, h). If cc is red (line L36), its children are black or nil and meet RB(⋅,h)\text{RB}(\cdot, h) (F1); the two new red nodes each have two black-or-nil RB children of black height hh (a.lefta.left and c.leftc.left, and c.rightc.right and b.rightb.right), so they meet RB(⋅,h)\text{RB}(\cdot, h), and the red root over them is IR(t,h)\text{IR}(t, h). If cc is not red (line L37), the new red node over cc and b.rightb.right meets RB(⋅,h)\text{RB}(\cdot, h), and the red root over a.lefta.left (black or nil) and it is IR(t,h)\text{IR}(t, h).

Lines L38 to L41: aa and bb are black, so h≥1h \ge 1, and by (F2) a.righta.right and b.leftb.left meet RB(⋅,h−1)\text{RB}(\cdot, h - 1). By induction cc meets IR(c,h−1)\text{IR}(c, h - 1). If cc is red (line L40), its children meet RB(⋅,h−1)\text{RB}(\cdot, h - 1) by the definition of IR, so each new black node has two RB children of black height h−1h - 1 and meets RB(⋅,h)\text{RB}(\cdot, h), and the red root over two black nodes meets RB(t,h)\text{RB}(t, h). If cc is not red (line L41), then RB(c,h−1)\text{RB}(c, h - 1), so the node NODE(black, cc, b.keyb.key, b.rightb.right) meets RB(⋅,h)\text{RB}(\cdot, h) and is black. Lemma 3 applies with l=a.leftl = a.left, which meets RB(⋅,h−1)\text{RB}(\cdot, h - 1) and so IR(⋅,h−1)\text{IR}(\cdot, h - 1), and gives RB(t,h)\text{RB}(t, h), because its right argument is black.

Line L42: bb is red and, since lines L34 and L38 did not apply, aa is black. By (F1), b.leftb.left is black or nil with black height hh, so by induction FUSE(a,b.left)(a, b.left) meets RB(⋅,h)\text{RB}(\cdot, h) (both arguments black or nil). The red root over it and b.rightb.right (black or nil, RB of black height hh) meets IR(t,h)\text{IR}(t, h). Line L43 is the mirror image, with aa red and bb black.

In each case, the keys of tt are the keys of aa followed by the keys of bb, in order, since each new node puts the keys of aa before those of cc and those of cc before those of bb. □\square

Lemma 5 (DEL). Let RB(t,h)\text{RB}(t, h) and k∈K(t)k \in K(t). Then K(DEL(t,k))=K(t)∖{k}K(\text{DEL}(t, k)) = K(t) \setminus \{k\}. If tt is black, DEL(t,k)(t, k) meets IR(⋅,h−1)\text{IR}(\cdot, h - 1). If tt is red, it meets RB(⋅,h)\text{RB}(\cdot, h).

Proof. By induction on the size of tt. Since k∈K(t)k \in K(t), tt is not nil, and each recursive call is on a child that holds kk, so it is not nil either.

Case k<t.keyk < t.key, t.leftt.left black (line L19). Let g=bh(t.left)≥1g = \text{bh}(t.left) \ge 1, so RB(t.right,g)\text{RB}(t.right, g) as well. By induction DEL(t.left,k)(t.left, k) meets IR(⋅,g−1)\text{IR}(\cdot, g - 1). Lemma 3 gives IR(⋅,g)\text{IR}(\cdot, g), and RB(⋅,g)\text{RB}(\cdot, g) if t.rightt.right is black. If tt is black, h=g+1h = g + 1, and the result is IR(⋅,h−1)\text{IR}(\cdot, h - 1). If tt is red, h=gh = g, and t.rightt.right is black by (F1) and not nil (its black height is g≥1g \ge 1), so the result is RB(⋅,h)\text{RB}(\cdot, h).

Case k<t.keyk < t.key, t.leftt.left not black (line L20). The child t.leftt.left holds kk, so it is not nil, and it is red. Then tt is black by (R2), and h=bh(t.left)+1h = \text{bh}(t.left) + 1. By induction DEL(t.left,k)(t.left, k) meets RB(⋅,h−1)\text{RB}(\cdot, h - 1), and t.rightt.right meets RB(⋅,h−1)\text{RB}(\cdot, h - 1) by (F2). A red root over two RB trees of black height h−1h - 1 is IR(⋅,h−1)\text{IR}(\cdot, h - 1), as required for black tt.

Case k>t.keyk > t.key (lines L22 and L23). The mirror image, with Lemma 3'.

Case k=t.keyk = t.key (line L24). Let g=bh(t.left)=bh(t.right)g = \text{bh}(t.left) = \text{bh}(t.right). Lemma 4 gives IR(⋅,g)\text{IR}(\cdot, g) with keys K(t)∖{k}K(t) \setminus \{k\}. If tt is black, h=g+1h = g + 1, so this is IR(⋅,h−1)\text{IR}(\cdot, h - 1). If tt is red, both children are black or nil (F1), Lemma 4 gives RB(⋅,g)\text{RB}(\cdot, g), and g=hg = h. □\square

Theorem 2. If tt is a version, DELETE(t,k)(t, k) is a version with keys K(t)∖{k}K(t) \setminus \{k\}.

Proof. If k∉K(t)k \notin K(t), line L16 returns tt. Else tt 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. □\square

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). ■\blacksquare

Back to Persistent red-black tree