Algorithm Search⌘K

Proofs: Leftist heap

These are the human proofs of Leftist heap, at full length.

Merge keeps a leftist heap, and the right spine is short full

This proof shows: correctness.

A leftist heap is a binary tree with the invariant (L1) heap order, (L2) s(x.left)≥s(x.right)s(x.left) \ge s(x.right) at each node, and (L3) x.s=s(x.right)+1x.s = s(x.right) + 1 at each node, with s(NIL)=0s(\mathrm{NIL}) = 0. Write K(T)K(T) for the multiset of keys of a tree TT.

Lemma 1. A leftist tree whose root has rank rr has at least 2r−12^r - 1 nodes, so r≤lg⁡(n+1)r \le \lg(n + 1).

Proof. We show first that every path from the root down to an empty subtree has at least rr nodes, by induction on the number of nodes. An empty tree has r=0r = 0. For a node xx of rank rr, a path goes to the left or to the right subtree. The right subtree has rank r−1r - 1 by (L3), and the left subtree has rank at least r−1r - 1 by (L2). By induction, the path has at least r−1r - 1 nodes below xx, so at least rr with xx. So all nodes at depths 00 to r−1r - 1 exist, which gives at least 1+2+⋯+2r−1=2r−11 + 2 + \dots + 2^{r-1} = 2^r - 1 nodes. Then n≥2r−1n \ge 2^r - 1, so r≤lg⁡(n+1)r \le \lg(n + 1). □\square

Lemma 2. If aa and bb are the roots of leftist heaps AA and BB, possibly empty, then MERGE(a,b)\mathrm{MERGE}(a, b) returns the root of a leftist heap TT with K(T)=K(A)⊎K(B)K(T) = K(A) \uplus K(B), and it makes at most s(a)+s(b)+1s(a) + s(b) + 1 calls.

Proof. By induction on s(a)+s(b)s(a) + s(b). If aa or bb is NIL, line L1 returns the other heap with one call, and the claim holds. Otherwise, after line L2, a.key≤b.keya.key \le b.key. The call at line L3 has the arguments a.righta.right and bb, and s(a.right)+s(b)=s(a)−1+s(b)s(a.right) + s(b) = s(a) - 1 + s(b) by (L3). By induction, it returns a leftist heap RR with the keys of the right subtree of aa and of BB, after at most s(a)−1+s(b)+1s(a) - 1 + s(b) + 1 calls. So the total number of calls is at most s(a)+s(b)+1s(a) + s(b) + 1.

Now check the invariant at the node aa after lines L3 to L5. All other nodes are in the left subtree of aa, which did not change, or in RR, which is a leftist heap.

  • (L1) Every key of RR is a key of the old right subtree of aa, which is at least a.keya.key by heap order of AA, or a key of BB, which is at least b.key≥a.keyb.key \ge a.key by heap order of BB. The left subtree of aa did not change. So heap order holds at aa, and a swap of the children does not change this.
  • (L2) After line L4, the child of the larger rank is on the left.
  • (L3) Line L5 sets a.sa.s from the final right child.

The keys of TT are the keys of aa, its left subtree and RR, which is K(A)⊎K(B)K(A) \uplus K(B). □\square

Theorem. INSERT, DELETE-MIN and MELD keep the invariant, and DELETE-MIN returns a smallest key. Each of them takes O(log⁡n)O(\log n) time.

Proof. A new node with rank 1 and no children is a leftist heap. By Lemma 2, line L7 returns a leftist heap with the old keys and kk. In DELETE-MIN, the root holds a smallest key, because by heap order every key is at least the key of its parent, and so, by induction on the depth, at least the key of the root. The two subtrees of the root are leftist heaps, because the invariant is local to each node. By Lemma 2, line L8 returns a leftist heap with all keys except the key of the root. Meld is a single MERGE.

By Lemma 2 and Lemma 1, MERGE makes at most lg⁡(na+1)+lg⁡(nb+1)+1≤2lg⁡(n+1)+1\lg(n_a + 1) + \lg(n_b + 1) + 1 \le 2\lg(n + 1) + 1 calls, each with O(1)O(1) work outside the recursion. So each operation takes O(log⁡n)O(\log n) time. ■\blacksquare

Back to Leftist heap