next up previous
Next: Chapter 7: Sorting Up: CS 3345: Algorithm Analysis Previous: Chapter 5: Hashing

Chapter 6: Priority Queues (Heaps)

Priority Queues (Heaps)
Minimum set of required functions:
Insert
: equivalent to Enqueue
Delete_Min
: returns and removes minimum element
Use binary search trees?

Implementation: Binary Heap
Structure Property

Implementation (contd.)
Heap Order Property

Basic Heap Operations
Insert(x)
: percolate up

Operations (contd.)
Delete_Min
: percolate down

Other Heap Operations
Decrease_Key(P, d)
: decrease value of pth element by d
Increase_Key(P, d)
: fix heap using
percolate down
Remove(I)
: remove node at position I
Build_Heap
: build a heap containing n
elements

Build_Heap
Simple Implementation Better Implementation Running Time: (# of comparisons) + (# of assignments)

# of comparisons = O($\sum$heights of nodes)

= O(n)

Applications: Selection Problem
Find the kth largest/smallest element
Solution 1

Applications: Selection Problem
Solution 2

Leftist Heaps
Null path length(X): length of shortest path from node X to a node without two children

Npl(NULL) = -1


Npl(X) = min(Npl(right child), Npl(left child)) + 1

Leftist heap property: for every node X,
Npl(left child) $\geq$Npl(right child)

Leftist heap operations
Merge: recursively merge heap with larger root with the right subheap of heap with smaller root.

Time = O(sum of lengths of right paths)

= O(log n)

Leftist heap operations (contd.)
Insert: treat item as a one node heap, and merge




Delete_Min: remove root, merge two subtrees

Skew Heaps
For any m consecutive operations, time = O(m log n)


next up previous
Next: Chapter 7: Sorting Up: CS 3345: Algorithm Analysis Previous: Chapter 5: Hashing
Ravi Prakash
1999-11-17