Proofs: Zip tree
These are the human proofs of Zip tree, at full length.
Zip and unzip keep the zip-tree order full
This proof shows: correctness.
Say that a node beats a node when , or and . Keys are distinct, so this is a strict total order on the nodes. A tree is a zip tree when:
- (Z1) it is a binary search tree on its keys;
- (Z2) every node beats its children.
For a left child of , , so (Z2) asks ; for a right child, . This is the rank rule checked in the code. By (Z2) and transitivity, every node beats all of its proper descendants. So a zip tree is a treap whose priority is the pair , and for a given set of keys and ranks there is exactly one zip tree: the root must be the node that beats all others, and the two subtrees are the zip trees of the smaller and of the larger keys.
Lemma 1 (UNZIP). Let be a zip tree and a key not in . UNZIP returns zip trees and whose nodes are those of , with the keys less than in and the greater keys in .
Proof. Let be the nodes visited by the loop of line L6; they form the search path of in , from the root down. A node with goes to , and all of its left subtree has keys less than , so the whole left subtree goes along unchanged. A node with goes to with its right subtree. Every node of is on the path or in such a subtree, because the path leaves each visited node through the other side. So the keys are divided as claimed.
Consider the nodes of the path that go to , in the order visited. The loop makes the right child of (line L6), and line L7 ends the spine with nil. In , after visiting the path goes right, so lies in the right subtree of : its key is larger, and beats it. So the new link keeps (Z1) and (Z2). All of the right subtree of in consists of nodes from its old right subtree with keys less than , so (Z1) holds at . The left subtrees are unchanged. So is a zip tree with root , and is a zip tree by the mirror argument.
Lemma 2 (ZIP). Let and be zip trees with every key of less than every key of . ZIP returns a zip tree on the nodes of and .
Proof. Lines L9 and L10 are the loop form of the recursion
each pass of the loop hangs the root of one call at the place where the previous call puts its result. Since , the test says exactly that beats . We prove the recursion by induction on the number of nodes. In the third case, the root keeps its left subtree. By induction, is a zip tree on the nodes of and , all with keys greater than , so (Z1) holds at . Its root is or ; beats by (Z2), and beats , so (Z2) holds at . The fourth case is the mirror image, with beating .
Insertion. Line L1 returns if is present; otherwise is a new node with key . Line L2 walks down the search path of while the current node beats ; let be the node where it stops (possibly nil) and the node before it. Every node above on the path beats . If is a node, it does not beat , so beats , and by transitivity beats every node of the subtree of . By Lemma 1, line L3 makes the left subtree of a zip tree on the keys of that subtree less than , and the right subtree a zip tree on the greater keys; so (Z1) and (Z2) hold at . Line L4 puts in the place of . The subtree of held exactly the keys of the tree in the interval of that place, and is in this interval, since the search path of passes there; so (Z1) holds at and above. beats , so (Z2) holds there. Nothing else changes.
Deletion. Line L8 finds the node with key , or returns. By Lemma 2, ZIP of its two subtrees is a zip tree on all the nodes below , with the same set of keys apart from . It takes the place of ; its keys lie in the interval of that place, and the parent of beats all of them, because they were descendants. So (Z1) and (Z2) hold.
Theorem. Starting from the empty tree, after any sequence of insertions and deletions the tree is a zip tree on exactly the keys inserted and not deleted, and a search returns true exactly for these keys. Its shape depends only on the keys and their ranks, not on the order of the operations.
Proof. The empty tree is a zip tree. The two cases above keep (Z1) and (Z2) and change the key set as stated. The search is correct by (Z1). The shape is the unique zip tree for the keys and ranks, as shown at the start.