Proofs: Treap
These are the human proofs of Treap, at full length.
Insertion and deletion keep the treap invariant full
This proof shows: correctness.
A treap is a binary tree with (P1) the search order on keys and (P2) the max-heap order on priorities: each node has a priority at least as high as those of its children. Write for the set of keys of the subtree . A rotation keeps the in-order sequence of the keys, so it keeps (P1) and the key set (see tree rotation). For the heap order we track bad edges: an edge from a parent to a child with .
Insertion
Lemma 1. Let be a treap or nil, with . Then has the keys , satisfies (P1), and has no bad edge. Moreover, its root is either the root of or the new node of key .
Proof. By induction on the size of . If is nil, line L1 returns one node. Else let (the case , with lines L4 and L5, is the mirror image). By induction, line L2 makes a treap with the new key, with (P1), no bad edge, and root either the old left child of or the new node . Every key of is less than , so the subtree of satisfies (P1). The only edge that can be bad is the edge from to the root of .
If the root of is , its priority is at most by (P2) for the old , so there is no bad edge, line L3 does nothing, and the root is .
If the root of is and , there is no bad edge and line L3 does nothing. If , line L3 rotates right at . Now is the root, is its right child, and the old right child of becomes the left child of . Check every changed edge. The edge is good, since . The edge is good: all nodes of other than are nodes of the old , so each has priority at most by (P2) for the old tree, and is one of them. The edge is unchanged and good, and the edge to the left child of is unchanged and good. So there is no bad edge, and the root is the new node.
So INSERT on a treap gives a treap with the new key. If is already in the tree, INSERT returns at line L6 with no change at the node of key and no rotation above it, since each child it returns is the same node as before.
Deletion
The heap order with one hole. Let be a node of a binary tree. Say that the tree has (P2) except at if every edge that does not touch is good, and, when has a parent , every child of has . In words: if were removed and its children hung on its parent, the tree would be a heap. Then for any two nodes and with below , : the path from to uses good edges, except that it may pass through , and the step from the parent of over to a child of does not increase the priority, by the definition. A treap has (P2) except at any of its nodes.
Lemma 2. Let have (P1) and (P2) except at , where . Then DELETE returns a treap with the keys , whose root, if any, is a node of other than .
Proof. By induction on the size of .
Case (lines L8 and L9). The node is in the child on the side of , by (P1). The subtree has (P2) except at , so by induction DELETE returns a treap with keys , whose root is a node of other than . By the remark above, , so the edge from to is good. The other child of and its edge are unchanged and good, so the subtree of is a treap. Its keys are , and (P1) holds because the keys of come from .
Case with at most one child (lines L10 and L11). The result is the other child, or nil. No edge in it touches , so it is a treap, and its keys are .
Case with two children and . Say , which is line L12; line L13 is the mirror image and also covers ties. Line L12 rotates right at : is the root, with the left child and the right child , and has the left child (the old one) and the right child . The edges that do not touch are and the edges inside the subtrees , and ; all are unchanged and good. The children of are , with by (P2) for the old edge , and , with . So the new tree has (P2) except at , and so does the subtree rooted at , which has fewer nodes than . By induction, DELETE returns a treap with keys whose root is a node of other than , so its priority is at most . The new tree, with root , left subtree and right subtree , is a treap with (P1), and its keys are . Its root is a node of other than .
If is not in the tree, DELETE follows the search path to nil at line L7 and changes nothing.
Conclusion
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a treap that holds exactly the keys inserted and not deleted since, and a search returns true exactly for these keys.
Proof. The empty tree is a treap. Lemma 1 and Lemma 2 (with the node of key ) show that each operation keeps (P1) and (P2) and changes the key set as stated. A search is correct by (P1).