Proofs: Persistent treap
These are the human proofs of Persistent treap, at full length.
The persistent treap keeps every version, and each version is a random treap full
This proof shows: correctness.
A treap on a finite set of keys with priorities is a binary tree whose nodes hold the keys of , once each, in search-tree order (I1) and in heap order on priorities (I2). For a node write for the set of keys in its subtree, with . A tree whose size fields satisfy (I3) is called sized.
Lemma 1 (no change). No procedure writes a field of an existing node. Hence and the shape below never change once is made.
Proof. The procedures make nodes only by COPY and NEW-NODE, which write the fields of a node that did not exist before, and set the size from the children at that moment. CONTAINS only reads.
Lemma 2 (split). If is a sized treap, then SPLIT returns sized treaps and with and , and every priority of or is the priority of the same key in .
Proof. By induction on the size of . For nil, line L1 is right. If (line L2), then all of and belong to the part below , and by induction SPLIT splits the right subtree into and . The copy of with right child has above all keys of and below all keys of , so it is in search order; its priority is at least each priority in and in , because those are priorities of the subtree of ; so it is in heap order. Its size is set from its children. The tree is a treap by induction, and its keys are those of at least , which are all the keys of at least . Line L3 is the mirror case.
Lemma 3 (merge). If and are sized treaps with every key of less than every key of , then MERGE returns a sized treap on with the same priorities.
Proof. By induction on . Lines L4 and L5 are clear. If (line L6), then is the highest priority of both trees, so may be the root. By induction MERGE is a treap on the keys of and , all of which are above , and with priorities at most . So the copy of with this right child is a treap. Line L7 is the mirror case, using .
Lemma 4 (insert). If is a sized treap and , then INS returns a sized treap on , in which has priority and every other key has its old priority.
Proof. By induction on the size of . In line L10, either is nil, or , so is higher than every priority of by heap order. By Lemma 2, SPLIT gives treaps and on the keys below and above , and the new node with key , priority and children and is in search order and heap order. Otherwise , and goes to the side given by the search order (lines L11 and L12). By induction the new subtree is a treap on the old keys of that side plus , with priorities at most (the old ones, and ). So the copy of with this child is a treap.
Lemma 5 (delete). If is a sized treap and , then DEL returns a sized treap on with the old priorities.
Proof. By induction on the size of . If , line L17 merges and , whose keys are separated by ; Lemma 3 applies. Otherwise lies in the subtree on the side chosen by line L15 or L16, which by induction is replaced by a treap on its keys minus , with priorities at most ; the copy of is a treap.
Theorem 1 (persistence and correctness). After any sequence of operations, every version is a sized treap on the set that the abstract ordered set gives to it, and every query returns the abstract answer on its version.
Proof. By induction on the operations. The empty set is nil. INSERT returns unchanged when is present (line L8) and otherwise is right by Lemma 4. DELETE returns when is absent (line L13) and otherwise is right by Lemma 5. split and merge are right by Lemmas 2 and 3; merge checks its precondition. CONTAINS follows the search order, so by (I1) it finds if and only if is present. By Lemma 1 the earlier versions do not change.
Theorem 2 (shape of a version). Suppose the sequence of operations, and the keys they use, is fixed in advance and does not depend on the priorities, and the priorities of new keys are independent and uniform on . Then for each version with key set , the priorities of the keys of are independent and uniform, and the tree of the version is the unique treap on with these priorities. Hence the expected depth of the key of rank in a version of keys is .
Proof. A key in a version got its priority when it was inserted on the path of the version tree that leads to this version, and each priority is copied with its key and never changed (Lemmas 2 to 5 keep old priorities). Distinct keys of got their priorities in distinct calls of INSERT, so the priorities of are independent and uniform. With probability 1 they are distinct. A treap on a set with distinct priorities is unique: the key with the highest priority must be the root, and the two sides are treaps on the keys below and above it, unique by induction. So the version is the treap on with random priorities, which has the same distribution as an ephemeral treap on , and the depth formula of Seidel and Aragon holds.