Chapter 9 Priority Queues (Heaps)
Chapter 9 Priority Queues (Heaps)
Priority Queues
(Heaps)
1
Outlines
1. Priority Queues Model
2. Implementation of Priority Queues
3. Binary Heap
2
Priority Queue (Heap)
• A kind of queue
• Dequeue gets element with the highest
priority
• Priority is based on a comparable value
(key) of each object (smaller value higher priority, or
higher value higher priority)
• Example Applications:
– printer -> print (dequeue) the shortest document first
– operating system -> run (dequeue) the shortest job first
– normal queue -> dequeue the first enqueued element
first
3
Priority Queue (Heap) Operations
deleteMin insert
Priority Queue
• insert (enqueue)
• deleteMin (dequeue)
– smaller value higher priority
– Find / save the minimum element, delete it
from structure and return it
4
Implementation using Linked List
• Unsorted linked list
– insert takes O(1) time
– deleteMin takes O(N) time
• Sorted linked list
– insert takes O(N) time
– deleteMin takes O(1) time
5
Implementation using Binary Search
Tree
• insert takes O(log N) time on the average
• deleteMin takes O(log N) time on the average
B C
D E F G
H I J
7
Property 2: Heap Order Property
(for Minimum Heap)
• Any node is smaller than (or equal to) all
of its children (any subtree is a heap)
• Smallest element is at the root (findMin
take O(1) time) 13
21 16
24 31 19 68
65 26 32
8
Array Implementation of Binary
Heap
A
B C
D E F G
H I J
A B C D E F G H I J
0 1 2 3 4 5 6 7 8 9 10 11 12 13
9
Insert
• Create a hole in the next available location
• Move the hole up (swap with its parent) until data
can be placed in the hole without violating the
heap order property (called percolate up)
13
21 16 13
24 31 19 68 21 16
24 19 68
65 26 32
65 26 32 31
10
insert 14
Insert
13
13
21 16
24 19 21 16
31 68
24 19 68
65 26 32
65 26 32 31
16
24 21 19 68 13
65 26 32 31 14 16
24 21 19 68
65 26 32 31
12
deleteMin
• Create a hole at the root
• Move the hole down (swap with the smaller one
of its children) until the last element of the heap
can be placed in the hole without violating the
heap order13 property (called percolate down)
14 16
19 21 19 68 14 16
19 21 19 68
65 26 32 31
65 26 32 31
13
deleteMin
13
14 16
19 21 19 68 14 16
19 21 19 68
65 26 32 31
65 26 32 31
19 21 19 68
14
65 26 32 31
19 16
21 19 68
65 26 32 31
15
deleteMin
14
19 16
26 21 19 68
65 32 14
31
19 16
26 21 19 68
65 31 32
16
Running Time
• insert
– worst case: takes O(log N) time.
• deleteMin
– worst case: takes O(log N) time.
17
Building a Heap
• Sometimes it is required to construct it
from an initial collection of items O(NlogN)
in the worst case.
19
buildHeap Example - I
initial after
heap percolateDown(7)
after after
percolateDown(6) percolateDown(5)
20
buildHeap Example - II
after after
percolateDown(4) percolateDown(3)
after after
percolateDown(2) percolateDown(1)
21