Binary Search Trees showed a tree's cost depending on something the structure does not choose: twelve keys arriving in sorted order give a path of height 11, and the same twelve arriving shuffled give a height of 4. Rotations and the AVL Invariant fixed that by adding a rule the structure enforces on every insertion. This lesson fixes it differently — by making the shape depend on coins instead of on the arrival order.
Definition · A treapA treap is a binary tree in which each node holds a key and a priority. It satisfies the search property on the keys — every key in a left subtree is smaller, every key in a right subtree is larger — and the heap property on the priorities: every node's priority is smaller than its children's.
Theorem · UniquenessFor any finite set of pairs with distinct keys and distinct priorities, exactly one treap contains them.
Induct on the number of pairs. With none, the empty tree is the only one. Otherwise the heap property forces the pair with the smallest priority to be at the root: any other node with that pair would have an ancestor of larger priority.
The search property then forces the partition of the remaining pairs: every key smaller than the root's goes into the left subtree and every larger key into the right. Neither subtree has a choice about which pairs it contains.
Each subtree is itself a treap on a strictly smaller set with distinct keys and distinct priorities, so by induction each is unique. The whole tree is therefore unique.
□
The lab does not argue this, it checks it. Both panels are built from the same priority assignment and compared node for node, and the verdict reads the same tree, node for node or words that would refute the claim. Priorities are ranked rather than taken raw so that no two can collide; a tie would leave the shape undetermined and the claim would fail for a reason with nothing to do with treaps.
Example · Twelve keys, one priority assignment, two insertion ordersThe keys are 10, 20, …, 120 and the seed assigns priorities 10:6 20:3 30:1 40:10 50:12 60:7 70:2 80:11 90:9 100:5 110:4 120:8. Priority 1 belongs to key 30, so 30 is the root. Inserting in sorted order costs 8 rotations; inserting in a shuffled order costs 11. Both produce a tree of height 5 with mean depth 5/2, and the two trees are identical. A plain search tree on the same sorted order has height 11 and mean depth 11/2.
Height 5 against 11, and mean depth 5/2 against 11/2: the treap is less than half as deep on average, on the input that is worst for a plain tree. And it got there without any rule about heights — the only thing being maintained is heap order on twelve numbers the structure generated itself.
Insertion, and what a rotation is for here
To insert a key, give it a random priority, place it as a leaf by the ordinary search-tree rule, and then rotate it upwards while its priority is smaller than its parent's. A rotation preserves the search property — Rotations and the AVL Invariant proves that, and the in-order sequence is what it preserves — so the search property is never in danger, and the loop stops exactly when heap order is restored.
That is the whole implementation, and it is worth noticing what is absent. No height is stored, no balance factor is computed, no case analysis over four shapes. Deletion is the same idea in reverse: rotate the node down until it is a leaf, then remove it. The panel's rotation counts, 8 and 11 for the two orders, are the only work the structure did beyond the searches.
What the guarantee is, and what the printed 2 ln n is not
The depth of a treap is a random variable, and its expectation is the expected depth of a search tree built from a uniformly random insertion order — which is 2 ln n + O(1) for the mean depth, and O(log n) for the height. Both of those are stated on this path: The Shape Problem and Random BSTs states them, Randomised Quicksort derives the recurrence they come from, because a random root and a random pivot are the same object, and no lesson in this library carries the full argument.
So the panel prints 2 ln n in its own row, labelled asymptotic, and it is about 15 per cent wrong about the tree in front of you. At n = 12 it reads 4.970 while the counted mean depth is 2.500 and the counted height is 5. Three numbers, three meanings: one asymptote, one exact mean of integers, one count. Reading them as estimates of each other is the failure mode of this page, and it is why each is labelled where it is printed.
The guarantee is also in expectation and not always. Over seeds 1 to 12 at twelve keys the treap's height came out 5, 6, 4, 5, 5, 6, 6, 5, 5, 4, 6, 5 — never 11, and never 3 either. Move the priority seed and watch it move. An AVL tree's height at twelve keys is bounded by a rule the structure enforces; a treap's is a random variable whose bad cases are unlikely rather than impossible.
That is the trade, and it is the same trade a random pivot makes. The input is arbitrary, possibly chosen by someone who wants the structure to perform badly, and the coins are the structure's own, so no input can be bad for it — only an unlucky draw can, and the draw is not the adversary's to make. Universal Hashing bought the same thing for a hash table with the same manoeuvre.