Algorithm Search⌘K

Proofs: Treap merge

These are the human proofs of Treap merge, at full length.

Treap merge returns a treap of the union 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. Write K(t)K(t) for the set of keys of the subtree tt, and N(t)N(t) for its set of nodes. In a treap, the root has the highest priority of all its nodes, since every node is reached from the root by edges that do not increase the priority.

Theorem. Let AA and BB be treaps or nil, with x<yx < y for every x∈K(A)x \in K(A) and y∈K(B)y \in K(B). Then MERGE(A,B)(A, B) returns a treap TT with K(T)=K(A)∪K(B)K(T) = K(A) \cup K(B) and N(T)=N(A)∪N(B)N(T) = N(A) \cup N(B), and its root is the root of AA or of BB. Each node keeps its key and priority.

Proof. By induction on ∣N(A)∣+∣N(B)∣|N(A)| + |N(B)|. If AA is nil, line L1 returns BB, which is right; if BB is nil, line L2 returns AA.

Case A.pri>B.priA.pri > B.pri (lines L3 to L5). The pair A.rightA.right and BB meets the precondition: the keys of A.rightA.right are keys of AA, so they are less than the keys of BB. It has one node fewer, so by induction line L4 makes A.rightA.right a treap T′T' with keys K(A.right)∪K(B)K(A.right) \cup K(B), whose root is the old root of A.rightA.right or the root of BB. We check that AA with left subtree A.leftA.left and right subtree T′T' is a treap.

  • (P1) at the root: the keys of A.leftA.left are less than A.keyA.key, as before. The keys of T′T' are keys of A.rightA.right, which are greater than A.keyA.key, or keys of BB, which are greater than every key of AA. Inside A.leftA.left and T′T', (P1) holds.
  • (P2) at the root: the left child is unchanged. The root of T′T' is the root of A.rightA.right, whose priority is at most A.priA.pri by (P2) in AA, or the root of BB, whose priority B.priB.pri is less than A.priA.pri. Inside A.leftA.left and T′T', (P2) holds.

The keys are K(A.left)∪{A.key}∪K(A.right)∪K(B)=K(A)∪K(B)K(A.left) \cup \{A.key\} \cup K(A.right) \cup K(B) = K(A) \cup K(B), the nodes likewise, and the root is the root of AA.

Case A.pri≤B.priA.pri \le B.pri (lines L6 and L7). This is the mirror image. The pair AA and B.leftB.left meets the precondition, so by induction line L6 makes B.leftB.left a treap T′T' with keys K(A)∪K(B.left)K(A) \cup K(B.left) whose root is the root of AA or of B.leftB.left. Its keys are keys of AA, less than every key of BB and so less than B.keyB.key, or keys of B.leftB.left, less than B.keyB.key. Its root has priority A.pri≤B.priA.pri \le B.pri or at most B.priB.pri by (P2) in BB. So BB with the left subtree T′T' and its old right subtree is a treap with the keys K(A)∪K(B)K(A) \cup K(B), and its root is the root of BB.

Termination and cost. Each call either stops or recurses with one node fewer in total, and the node that is set aside is the root of AA or of BB. In the first case the next call works on A.rightA.right, a step down the right spine of AA; in the second case on B.leftB.left, a step down the left spine of BB. So the number of calls is at most the length of the right spine of AA plus the length of the left spine of BB, plus one, and each call does O(1)O(1) work. ■\blacksquare

Back to Treap merge