
CSCI-1200 Computer Science II — Fall 2007
Lecture 22 – Priority Queues and Leftist Heaps
Review from Lecture 21
•Queues and Stacks, What’s a Priority Queue?
•A Priority Queue as a Heap, percolate_up and percolate_down
•A Heap as a Vector, Building a Heap, Heap Sort
•HW8 Contest Results
Today’s Class
•Merging heaps are the motivation for leftist heaps
•Mathematical background & Basic algorithms
22.1 Leftist Heaps — Overview
•Our goal is to be able to merge two heaps in O(log n) time, where nis the number of values stored in the larger
of the two heaps.
–Merging two binary heaps requires O(n) time
•Leftist heaps are binary trees where we deliberately attempt to eliminate any balance.
•Leftists heaps are implemented explicitly as trees.
22.2 Leftist Heaps — Mathematical Background
•Definition: The null path length (NPL) of a tree node is the length of the shortest path to a node with 0
children or 1 child. The NPL of a leaf is 0. The NPL of a NULL pointer is -1.
•Definition: Aleftist tree is a binary tree where at each node the null path length of the left child is greater
than or equal to the null path length of the right child.
•Definition: The right path of a node (e.g. the root) is obtained by following right children until a NULL child
is reached.
–In a leftist tree, the right path of a node is at least as short as any other path to a NULL child.
•Theorem: A leftist tree with r > 0 nodes on its right path has at least 2r−1 nodes.
–This can be proven by induction on r.
•Corollary: A leftist tree with nnodes has a right path length of at most blog(n+ 1)c=O(log n) nodes.
•Definition: Aleftist heap is a leftist tree where the value stored at any node is less than or equal to the value
stored at either of its children.
22.3 Leftist Heap Operations
•The insert and delete_min operations will depend on the merge operation.
•Here is the fundamental idea behind the merge operation. Given two leftist heaps, with h1 and h2 pointers to
their root nodes, and suppose h1->value <= h2->value. Recursively merge h1->right with h2, making the
resulting heap h1->right.
•When the leftist property is violated at a tree node involved in the merge, the left and right children of this
node are swapped. This is enough to guarantee the leftist property of the resulting tree.
•Merge requires O(log n+ log m) time, where mand nare the numbers of nodes stored in the two heaps, because
it works on the right path at all times.