Download presentation
Presentation is loading. Please wait.
1
Priority Queues (Chapter 6.6):
Abstract model of Priority Queues Priority Queue Get the one with the highest priority insert the minimum one (or equivalently, maximum, or other requirement) a black box 1/1/2019 IT 179
2
Let’s cleaning up some confusion.
Binary Search Tree 17 Get the one with the highest priority insert 14 19 12 15 18 the minimum one (or maximum,) Is Binary Search Tree a priority queue? No! It is not, but it can be used (in the black box) to implement a priority queue. 1/1/2019 IT 179
3
Items with higher priority will be treated first.
Stacks (FILO), queues (FIFO) are Priority Queues Arrays are not, since we can randomly access the items in an array The priority of the item may be fixed or changed over the time. 1/1/2019 IT 179
4
Using a Binary Search Tree is overkilled. Why?
17 Get the one with the highest priority insert 14 19 12 15 18 11 13 the minimum one (or maximum) Items are totally ordered, but we don’t really this information (nothing comes free) 1/1/2019 IT 179
5
Our goals: 1. Can get the minimum one efficiently
2. and low maintenance 11 9 16 12 11 12 Get the minimum one 9 13 insert 13 12 14 17 15 19 18 13 14 17 15 19 18 16 14 17 15 19 18 Binary Heap 1/1/2019 IT 179
6
Binary Heap Using (abstractly) binary tree structure
For every node in the tree, its key is smaller than (or equal to) the keys of its two children. Therefore, for every node in the tree, its data is the smallest one among data stored in the sub-trees (the two children). Different from BST, not every key in the left-sub-tree is less than every key in the right-sub-tree. Loosing this requirement, we gain some benefits. 1/1/2019 IT 179
7
A Binary Heap but we can organize it in a better way
without too much extra cost. 1 4 6 5 9 23 10 25 8 14 11 25 12 29 28 12 17 20 15 22 29 30 19 36 13 29 23 30 37 1/1/2019 IT 179
8
A Binary Heap in complete binary tree
10 12 13 15 14 16 17 23 22 21 18 19 24 25 26 27 32 29 30 25 26 37 pack 1/1/2019 IT 179
9
A Binary Heap implemented in an array
10 1 12 13 2 3 15 14 16 17 4 5 6 7 23 22 21 28 39 24 25 42 10 11 15 8 9 12 13 14 27 32 29 40 35 36 37 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 1st child (left-child) of i is *i 2nd child (right-child) of i is 2*i+1 The parent of i is i / 2 double the size of the array if the space ran out 1/1/2019 IT 179
10
Heap operation: insert an item
11 10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 11 1/1/2019 IT 179
11
Heap operation: insert an item
11 10 12 13 15 14 16 17 23 22 21 39 24 25 42 11 27 32 29 40 35 36 37 28 1/1/2019 IT 179
12
Heap operation: insert an item
11 10 12 13 15 11 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 1/1/2019 IT 179
13
Heap operation: insert an item
11 10 11 13 15 12 16 17 23 22 21 39 24 25 42 14 27 32 29 40 35 36 37 28 1/1/2019 IT 179
14
Heap operation: delete the minimum
10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 1/1/2019 IT 179
15
Heap operation: delete the minimum
10 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 37 1/1/2019 IT 179
16
Heap operation: delete the minimum
10 37 12 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 1/1/2019 IT 179
17
Heap operation: delete the minimum
10 12 37 13 15 14 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 1/1/2019 IT 179
18
Heap operation: delete the minimum
10 12 14 13 15 37 16 17 23 22 21 28 39 24 25 42 27 32 29 40 35 36 1/1/2019 IT 179
19
Heap operation: delete the minimum
10 12 14 13 15 16 21 17 23 22 37 28 39 24 25 42 27 32 29 40 35 36 1/1/2019 IT 179
20
Heap operation: delete the minimum
10 12 14 13 15 16 21 17 23 22 35 28 39 24 25 42 27 32 29 40 37 36 1/1/2019 IT 179
21
Time complexity for insertion
best case: O(1) worst case: O(log n) 14 log n 15 21 23 22 37 25 27 32 29 40 35 36 30 27 average case: 1/1/2019 IT 179
22
Time complexity of Building a heap
Insert them one by one into the empty heap What is the cost? best case: O(n) average case: O(n) worst case: O(n log n) Can we improve the worst case? 34 15 30 2 23 9 25 6 32 5 40 11 25 21 7 1/1/2019 IT 179
23
Percolating a non-heap
Worst case: O(n) 34 2 15 2 5 30 7 7 2 15 6 5 23 9 25 7 30 21 6 23 9 21 15 6 32 5 23 40 11 31 30 21 7 25 15 32 34 40 11 31 30 25 Binary Heap 1/1/2019 IT 179
24
Percolating a non-heap
Worst case: O(S) S = Sum of heights of all nodes h O(S) = O(n) 1/1/2019 IT 179
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.