0% found this document useful (0 votes)
10 views21 pages

Chapter 9 Priority Queues (Heaps)

Dddx

Uploaded by

x9fbxmzfvz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

Chapter 9 Priority Queues (Heaps)

Dddx

Uploaded by

x9fbxmzfvz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Data Structures CCCS 224

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

• support other operations that are not


required by priority queue (for example,
findMax)
• deleteMin operations make the tree
unbalanced
6
Binary Heap Implementation
Property 1: Structure Property
•Binary tree & completely filled (bottom level
is filled from left to right) (complete binary
tree)
•if height is h, size between 2h (bottom level has
only one node) and 2h+1-1 A

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

Percolate Up -> move the place to put 14 up


(move its parent down) until its parent <= 14
11
Insert
13

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

Percolate Down -> move the place to put 31


down (move its smaller child up) until its
children >= 31
14
14
deleteMin
16

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.

• But insertions take O(1) on the average.

• Hence the question: is it possible to do any


better?
18
buildHeap Algorithm
• General Algorithm
• Place the N items into the tree in any order,
maintaining the structure property.
• Call buildHeap
void buildHeap( PriorityQueue H, int N )
{
int i;

for( i = N / 2; i > 0; i-- )


percolateDown( H, i );
}

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

You might also like