Proofs: Persistent binary search tree
These are the human proofs of Persistent binary search tree, at full length.
Path-copying insertion and deletion keep the search order and every old version full
This proof shows: correctness.
Write for the set of keys in the subtree of , with . A tree is a search tree if (B1) holds at each node of it. For an interval with in the order extended by and , say that fits if is a search tree and .
Lemma 0 (old versions do not change). Let be a root that exists at some moment. Then the tree reachable from is the same at each later moment.
Proof. The procedures write fields only in NEW-NODE, which sets the fields of a node that it has just made. So no field of an existing node changes (B2). The tree reachable from is determined by the fields of the nodes reachable from , which all exist at that moment.
So we can reason about each call as if it ran on a fixed tree, and its result does not affect any other version.
Lemma 1 (DESCEND). Let be a search tree and a key. DESCEND returns where , (if ), each is the child of on the side of (left if , else right), for each , and is the child of on the side of (or when ). Moreover exactly when , and then .
Proof. The loop of lines L16 to L18 keeps the statement for the nodes appended so far, with the current in the role of . It stops when or , and it stops after at most steps, because each step goes one level down. If it stops at a node of key , then . If it stops at nil, then : by (B1), at each the key can only be in the subtree on the side of , so if were in , it would be in the subtree of the final position, which is empty.
For the path of Lemma 1, define the interval , and where and if , and and otherwise. By (B1), the subtree of the child of on the side of fits , and for each by induction.
Lemma 2 (REBUILD). Let be a search tree, let $(path, x) = $ DESCEND with , and let fit . Then $r = $ REBUILD is a search tree with , where for we read and .
Proof. By induction on the iterations of line L20, from down to . Let , and let be the copy of that the iteration for makes. We show that fits and has the keys .
Take the case (line L21); the other case (line L22) is the mirror image. The copy has key , left child and right child . The left child fits by the hypothesis (or by the assumption for ), so all its keys are below and above . The right child is the old right subtree of , which is a search tree whose keys are above and, since lies in a subtree that fits , below . And for the same reason. So the copy is a search tree and fits . Its keys are . The old subtree had (with in place of for ), and by the hypothesis , which gives the claim, because is disjoint from .
At we get , which fits and has the keys .
Theorem 1 (INSERT). If is a search tree, INSERT returns a search tree with keys , and is unchanged.
Proof. If , Lemma 1 gives , and line L5 returns . Else , so , and the leaf with key fits because . Lemma 2 gives a search tree with keys . Lemma 0 gives that is unchanged.
Theorem 2 (DELETE). If is a search tree, DELETE returns a search tree with keys , and is unchanged.
Proof. If , line L8 returns . Else is the node of key , and the subtree of fits . We build that fits with . Then Lemma 2 gives the result.
If (line L9), is a subtree of , so it fits , and . Line L10 is the mirror image.
If has two children (lines L11 to L13), let be the last node of the left steps from , so and is the least key of . Let be the nodes before on these steps. This is the path that DESCEND returns, with as its node: at each node of , is smaller, so the search goes left. By Lemma 2 applied to the search tree with (which fits the interval of on that path, being a subtree of ), the tree $r' = $ REBUILD is a search tree with keys . The new node of line L13 has key , left child , whose keys are below , and right child , whose keys are in and greater than the least one, . So it is a search tree, with keys , all in .
Corollary (full persistence). For each sequence of operations in which each update takes any earlier version, every version is at the end a search tree whose key set is the result of applying, in order, the updates on the chain from the empty tree to , and SEARCH returns true exactly when is in this set.
Proof. By induction on the number of updates, with Theorems 1 and 2 for the step and Lemma 0 for the fact that later updates change no earlier version. SEARCH returns at line L3 with the same loop as DESCEND, so by Lemma 1 it is right.