Priority queues
Priority queue ADT Binary heap Heap insertion, removal
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
Priority queues Priority queue ADT Binary heap Heap insertion, - - PowerPoint PPT Presentation
Priority queues Priority queue ADT Binary heap Heap insertion, removal March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Priority queues? Suppose Geoff has made a to-do list (with priority values): 6 MT2 regrades 2 Vacuum
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4
G(9) D(100) G(9) C(3) F(7) E(5) A(4) B(6) insert D(100) removeMax
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7
Heap has asymptotically same performance as AVL tree, but MUCH simpler to implement
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9
complete binary trees incomplete binary trees
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10
98 86 41 13 65 32 29 9 10 44 23 21 32 Heaps are not fully ordered – an in-order traversal would result in: 9, 13, 10, 86, 44, 65, 23, 98, 21, 32, 32, 41, 29
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11
2 5 7 5 7 8 2 5 5 7 7 8
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12
1 2 3 4 5 6 7 1 2 3 4 5 6 .. .
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14
98 86 41 13 65 32 29 9 10 44 23 21 32 1 2 3 4 5 6 7 8 9 10 11 12 13 Heap Underlying array 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 15
class MinHeap { private: int size; // number of stored elements int capacity; // maximum capacity of array int* arr; // array in dynamic memory public: ... }; MinHeap::MinHeap(int initcapacity) { size = 0; capacity = initcapacity; arr = new int[capacity+1]; }
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 16
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 17
98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 14 Insert 81
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 18
98 86 41 13 65 32 29 9 10 44 23 21 32 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 81 14 Insert 81 81 81 29 parent: 14 2 = 7 81 29
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 19
98 86 41 13 65 32 81 9 10 44 23 21 32 98 86 41 13 65 32 81 9 10 1 2 3 4 5 6 7 44 23 21 32 10 11 12 13 8 9 value index 29 14 Insert 81 29 81 41 parent: 7 2 = 3 81 41 81 is less than 98 so finished
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 20
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 21
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 22
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 23
98 86 41 13 65 32 29 9 10 44 23 21 17 removeMax 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 17 10 11 12 13 8 9 value index
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 24
17 98 86 41 13 65 32 29 9 10 44 23 21 removeMax 17 13 98 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Replace root with rightmost leaf 17 17
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 25
17 86 41 13 65 32 29 9 10 44 23 21 removeMax 13 17 86 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? 17 86 children of root: 2 ∙ 1, 2 ∙ 1 + 1 = 2, 3 86 17
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 26
86 17 41 13 65 32 29 9 10 44 23 21 removeMax 13 86 17 41 13 65 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? children of 2: 2 ∙ 2, 2 ∙ 2 + 1 = 4, 5 65 17 65 17
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 27
86 65 41 13 17 32 29 9 10 44 23 21 removeMax 13 86 65 41 13 17 32 29 9 10 1 2 3 4 5 6 7 44 23 21 10 11 12 8 9 value index Swap with largest child ? ? children of 5: 2 ∗ 5, 2 ∗ 5 + 1 = 10, 11 17 44 17 44
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 28
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 29
3 27 9 31 46 12 1 2 3 4 5 6 value index 3 27 9 31 46 12 1 2 3 4 5 6 7 10 11 12 8 9 value index 62
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 30
March 16, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 31