Lecture 12:�Priority Queue & Heap (Continue)
Announcement & Tentative Schedule
2
Priority Queue
3
Queue: Recap
3
enqueue
15
22
dequeue
11
9
4
Priority Queue
Key
Value
5
Priority Queue
Key
Value
6
Heap
7
Binary Heap
Min Heap
Max Heap
8
Max-heap: Insertion
9
Max-heap: Insertion
10
Max-Heap: Deletion
11
Max-Heap: Deletion
12
Max Heap: Heap sort
13
Max Heap: Heap sort
14
Max Heap: Heap sort
15
Max Heap: Heap sort
16
Max Heap: Heap sort
17
Max Heap: Heap sort
18
Max Heap: Heap sort
19
Max Heap: Heap sort
20
Building Heap
Not O(n logn) ?
21
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
6
8
1
0
2
9
0 1 2 3 4 5 6 7
22
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
6
8
1
0
2
9
0 1 2 3 4 5 6 7
23
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
6
8
1
9
2
0
0 1 2 3 4 5 6 7
24
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
6
8
1
9
2
0
0 1 2 3 4 5 6 7
25
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
6
8
1
9
2
0
0 1 2 3 4 5 6 7
26
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
9
8
1
6
2
0
0 1 2 3 4 5 6 7
27
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
3
5
9
8
1
6
2
0
0 1 2 3 4 5 6 7
28
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
9
5
3
8
1
6
2
0
0 1 2 3 4 5 6 7
29
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
9
5
8
3
1
6
2
0
0 1 2 3 4 5 6 7
30
Building max Heap
3 | 6 | 5 | 0 | 8 | 2 | 1 | 9 |
Intermediate Node
Leaf Node (n/2)
Total: n node
9
5
8
3
1
6
2
0
0 1 2 3 4 5 6 7
31
Building max Heap: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
32
Building max Heap: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
33
Building max Heap: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
34
Building max Heap: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
35
Building max Heap: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
36
37
38
Appendix
39
Homeworks
40