Proofs: Randomized heap
These are the human proofs of Randomized heap, at full length.
Randomized meld is correct and takes O(log n) expected time full
This proof shows: correctness, time.expected.
A tree is in heap order if the key of each node is at least the key of its parent. Write for the multiset of keys of and for its number of nodes.
Lemma 1 (correctness). For every result of the coins, if and are heap-ordered trees with roots and , possibly empty, then returns a heap-ordered tree with .
Proof. By induction on . If one tree is empty, line L1 returns the other. Otherwise, after line L2, , and is a smallest key of , because every key of is at least and every key of is at least . Let be the subtree chosen at line L3. The call at line L4 has fewer nodes, since is not in or , so by induction it returns a heap-ordered tree with , every key of which is at least . The other subtree of is unchanged. So the tree with root is heap-ordered and holds .
Theorem 1. INSERT, DELETE-MIN and MELD keep heap order and the right multiset, and DELETE-MIN returns a smallest key.
Proof. As for any heap-ordered tree: insert melds with a one-node tree, and delete-min removes the root, which holds a smallest key, and melds its two subtrees, which hold all other keys. Lemma 1 gives the rest.
Lemma 2 (random walk). For a binary tree , let be the number of nodes visited by a walk that starts at the root, and at each node moves to the left or the right child with probability each, independently, until it reaches an empty subtree. Then .
Proof. By induction on . If is empty, . Otherwise let have subtrees and with and nodes, so . Then
Because is concave, for . With and ,
Lemma 3 (the meld path). Let the shapes of and be fixed before the meld, and let the coins of the meld be fresh. Then the number of calls of MELD that reach line L2 is at most , where has the distribution of a prefix of and of a prefix of . So the expected number of calls, with the last call at line L1, is at most .
Proof. Each call that reaches line L2 has two nonempty roots. One of them, the smaller one , is the node at which this call flips its coin; call it the node charged to the call. Each node is charged at most once, because after its call the path goes into a subtree of it. Consider the nodes of that are charged, in the order of the calls. The first one, if any, is the root of . If is a charged node of , the call at chooses a child by its coin. The next call involves the subtree with its original root: if that subtree is not empty, its root is either charged in this next call or, if it loses the comparison, it is passed on unchanged as the second argument, and every later call keeps the root of this subtree as one of its two arguments until that root is charged or the meld ends. So the charged nodes of are a prefix of the walk in driven by the coins flipped at nodes of . These coins are independent and fair. The same holds for . Every call that reaches line L2 charges a node of or of , so the number of such calls is at most , where and for walks with these coins. Lemma 2 and linearity of expectation give the bound.
Theorem 2. Against a sequence of operations fixed in advance, each meld of heaps of sizes and makes at most calls in expectation, insert at most , and delete-min at most , where is the size before the operation.
Proof. The shapes before an operation depend only on earlier coins, and the coins of the operation are independent of them, so Lemma 3 applies conditioned on the shapes, and then also without the condition. Insert melds with one node: . Delete-min melds two subtrees of total size ; with , concavity gives for , so the bound is . Each call does work, so each operation takes expected time.