Lecture 016 - Heaps and Priority Queue

We have defined the following functions for priority queue:

empty : T
singleton : S -> T
findMin : T -> (S union {T})
insert : T x S -> T
deleteMin : T -> T x (S union {T})
meld : T x T -> T
fromSeq : S seq -> T

where meld merge two priority queues.

Priority can be used for sequential \log n heap sort. And other problems: - Huffman codes - clustering algorithms - event simulation - kinetic algorithms for motion simulation - you can use a max and a min heap for building "medium queue" (with quick select)

Implementing Priority Queues

Linked List: ether the following

Balanced Tree: deleteMin (delete left-most), insert both O(\log n). meld O(n).

Heaps: unlike search tree, heap only maintain partial order (min heap small on root, max heap big on root)

Binary Heap:

Costs of Different Heaps

Costs of Different Heaps

Note that Leftist Heap is better than all heaps listed here.

Meldable Priority Queues

Leftist Heaps

Leftist Heap: does not maintain BST property

Naive Meld Code

Naive Meld Code

Naive Meld Picture

Naive Meld Picture

Naive Meld: when meld two trees, choose choose right spine of smaller tree to insert bigger tree. (assuming min-heap, smaller tree is a tree with smaller key on root) O(m+n) work since resulting tree is imbalanced.

Unbalanced Leftist Property

Unbalanced Leftist Property

Example of Leftist Heap

Example of Leftist Heap

Right-looking Leftist Heap

Right-looking Leftist Heap

Leftist Meld: add another field called rank on each node, denoting the number of nodes you would traverse to reach the end if you always go right.

Complete heap always Leftist.

Leftist Code

Leftist Code

makeLeftistNode() piles larger right-rank tree on the left

Leftist Rank Lemma: rank of the root node is at most \log_2 (n+1).

To prove this. notice the smallest (in terms of the number of nodes) rank-0 heap has 0 node, and rank-1 heap has 1 node, rank-2 heap has 1 + 1 + 1 = 3 nodes (by the leftist property, the rank of the left child of x must be at least the rank of the right child of x), rank-3 heap has 1 + 3 + 3 nodes. In general, let n(r) denotes the number of nodes of a smallest heap with rank r, then if x is the root of smallest heap with rank r: n(r) = 1 + n(rank(L(x))) + n(rank(R(x))). Solving the recurrence gives smallest heap with rank r must contain at least 2^r - 1 nodes.

Leftist Work: makeLeftistNode() guarantees leftist property. Since meld only traverse right spines of A, B and both trees are leftist, we have W = O(\log m = \log n).

Table of Content