Lecture 11:�Priority Queue & Heap
Announcement & Tentative Schedule
2
Queue: Recap
3
enqueue
15
22
dequeue
11
9
3
Priority Queue
4
Priority Queue
Key
Value
5
Priority Queue
Key
Value
6
Heap
7
(Recap: Tree) Complete Binary Tree
Complete BT of
Height 2
Complete BT of
Height 3
Complete BT of Height 4
Must be filled (to be complete)
Optionally filled (to be complete)
8
(Recap: Tree) Array-based Implementation
[1]
[2]
[3]
[4]
[5]
[6]
[0]
[7]
[8]
[9]
[10]
[11]
[12]
[13]
[14]
8
7
2
3
9
2
1
5
9
Binary Heap
Min Heap
Max Heap
10
Heap and Priority Queue
11
Insertion
12
Heapify-up
13
Insertion example
14
Insertion example
15
Insertion example
16
Insertion example
17
Insertion example
18
Insertion example
19
Insertion example
20
Deletion
21
Heapify-down
22
Deletion Example
23
Deletion Example
31
24
Deletion Example
31
25
Deletion Example
26
Implementation (array)
27
Implementation (array)
28
Heap-sort (Min heap)
O(n log n) Sorting!
| Insert | Remove�Min | PQ-Sort Total |
Heap Sort | O(logn) | O(logn) | O(n logn) |
29