Lecture 12:�Priority Queue & Heap (Continue)
Priority Queue
2
Queue: Recap
3
enqueue
15
22
dequeue
11
9
3
Priority Queue
Key
Value
4
Priority Queue
Key
Value
5
Heap
6
Binary Heap
Min Heap
Max Heap
7
Max-heap: Insertion
8
Max-heap: Insertion
9
Max-Heap: Deletion
10
Max-Heap: Deletion
11
Max Heap: Heap sort
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
Building Heap
Not O(n logn) ?
20
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
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
9
2
0
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
9
8
1
6
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
9
5
3
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
8
3
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: Time Complexity
Number of nodes Height
1 3
2 2
4 1
8 0
Level 3
Level 2
Level 1
Level 0
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
36
37
Appendix
38
Homeworks
39