Download presentation
Presentation is loading. Please wait.
1
Chapter 8 – Binary Search Tree
8.5 Heaps and Priority Queues Chapter 8 – Binary Search Tree
2
Attendance Quiz #28 Heaps / Priority Queues
3
Tip #30: Solving Compiler Errors
Heaps / Priority Queues When you get any compiler error in MS Visual Studio, then in the Error List Panel, you will see ERROR CODE along corresponding error. Write that ERROR Code with keyword "MSDN" in GOOGLE, and you will get a link on MSDN. In that link, you will find every detail on that error and information on how to fix it. For other compilers, you can often search for the name of the compiler followed by the error message.
4
8.5, pgs. 484-495 8.5 Heaps and Priority Queues
Inserting an Item into a Heap Removing an Item from a Heap Implementing a Heap Priority Queues The priority_queue Class Using a Heap as the Basis of a Priority Queue 8.5, pgs
5
Heaps and Priority Queues
Heaps / Priority Queues A heap is a complete binary tree with the following properties: The value in the root is greater than or equal to all items in the tree (max-heap.) Every subtree is a heap.
6
Min Heap Heaps / Priority Queues In a max-heap, each node’s value is larger than or equal to the values of its children, and the root node stores the largest value. The heap shown below is called a min-heap because the root node stores a smallest value. The text presents algorithms and illustrations for max heaps. Here we present the corresponding algorithms and illustrations for min heaps.
7
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 29 37 26 76 32 74 89 20 28 39 66
8
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 29 8 37 26 76 32 74 89 20 28 39 66
9
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 29 66 37 26 76 32 74 89 20 28 39 8
10
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 8 66 37 26 76 32 74 89 20 28 39 29
11
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 8 66 37 26 76 32 74 89 20 28 39 29
12
Inserting an Item into a Min-Heap
Heaps / Priority Queues 6 18 8 66 37 26 76 32 74 89 20 28 39 29
13
Removing an Item from a Heap
Heaps / Priority Queues 6 18 8 66 37 26 76 32 74 89 20 28 39 29
14
Removing an Item from a Heap
Heaps / Priority Queues 66 18 8 37 26 76 32 74 89 20 28 39 29
15
Removing an Item from a Heap
Heaps / Priority Queues 8 18 66 37 26 76 32 74 89 20 28 39 29
16
Removing an Item from a Heap
Heaps / Priority Queues 8 18 29 37 26 76 32 74 89 20 28 39 66
17
Removing an Item from a Heap
Heaps / Priority Queues 8 18 29 37 26 76 32 74 89 20 28 39 66
18
Insert 3 into the min-heap.
8 34 12 49 36 20 14 77 51 65 87 29 66 Insert 3 into the min-heap. 8 34 42 49 36 50 84 77 51 65 87 52 71 Remove a node from the min-heap.
19
Implementing a Min-Heap
Heaps / Priority Queues Because a heap is a complete binary tree, it can be implemented efficiently using an array rather than a linked data structure. For a node at position p with an array, The left child's position is 2p + 1. The right child's position is 2p + 2. A node at position c in the array can find its parent at floor((c – 1) / 2). The root is found at index 0. NOTE: most implementations use index 0 as the size of the heap and put the root at index 1 – the book does not. The array representation is a level order traversal of the binary tree.
20
Implementing a Min-Heap
Heaps / Priority Queues 8 8 18 18 29 29 20 20 28 28 39 39 66 66 37 37 26 26 76 76 32 32 74 74 89 89 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89
21
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
22
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
23
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
24
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
25
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
26
Implementing a Min-Heap
Heaps / Priority Queues For a node at position p, L. child position: 2p + 1 R. child position: 2p + 2 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent L. Child R. Child
27
Implementing a Min-Heap
Heaps / Priority Queues 8 18 29 37 26 76 32 74 89 20 28 39 66 A node at position c can find its parent at (c – 1) / 2 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent Child
28
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 37 26 76 32 74 89 20 28 39 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89
29
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 66 37 26 76 32 74 89 10 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89
30
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 66 37 26 76 32 74 89 10 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Child
31
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 66 37 26 76 32 74 89 10 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent Child
32
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 66 37 26 76 32 74 89 10 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 66 37 26 76 32 74 89 Parent Child
33
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 10 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 37 26 76 32 74 89 66 Parent Child
34
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 10 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 37 26 76 32 74 89 66 Parent Child
35
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 10 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 37 26 76 32 74 89 66 Parent Child
36
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 29 20 28 39 10 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 29 20 28 39 37 26 76 32 74 89 66 Parent Child
37
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 10 20 28 39 29 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 20 28 39 29 37 26 76 32 74 89 66 Parent Child
38
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 10 20 28 39 29 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 20 28 39 29 37 26 76 32 74 89 66 Parent Child
39
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 10 20 28 39 29 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 20 28 39 29 37 26 76 32 74 89 66 Parent Child
40
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 10 20 28 39 29 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 20 28 39 29 37 26 76 32 74 89 66 Parent Child
41
Inserting into a Vector Min-Heap
Heaps / Priority Queues Insert the new element at the end of the vector. Set child = vector.size() – 1. Set parent = (child – 1) / 2. while (parent >= 0 AND table[parent] > table[child]) Swap table[parent] and table[child]. Set child = parent. Set parent = (child - 1) / 2. 8 18 10 20 28 39 29 37 26 76 32 74 89 66 1 2 3 4 5 6 7 8 9 10 11 12 13 18 20 28 39 29 37 26 76 32 74 89 66
42
Removal from a Vector Min-Heap
Heaps / Priority Queues Remove the last element (i.e., the one at size() – 1) and set the item at 0 to this value. Set parent to 0. while (true) Set left_child to (2 * parent) + 1 and right_child to left_child + 1 if leftChild >= table.size() Break out of loop. Set min_child (the smaller child) to left_child if right_child < table.size() and table[left_child] > table[right_child] Set min_child to right_child if table[parent] > table[min_child] Swap table[parent] and table[min_child] Set parent to min_child else
43
Performance of the Heap
Heaps / Priority Queues Removal / insertion algorithms Removal traces a path from the root to a leaf. Insertion traces a path from a leaf to the root. Both require at most h steps where h is the height of the tree. The largest full tree of height h has 2h-1 nodes. The smallest complete tree of height h has 2(h-1) nodes. Both insert and remove are O(log n) where n is the number of items in the heap.
44
Create a min-heap vector implementing the tree to the right
3 8 13 9 11 18 25 35 22 12 15 30 19 Create a min-heap vector implementing the tree to the right 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Add 10 to the min-heap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Remove a node from the min-heap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
45
Create a min-heap vector implementing the tree to the right
3 8 13 9 11 18 25 35 22 12 15 30 19 Create a min-heap vector implementing the tree to the right 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 25 35 22 30 19
46
Create a min-heap vector implementing the tree to the right
3 8 10 9 11 18 13 35 22 12 15 30 19 25 3 8 13 9 11 18 25 35 22 12 15 30 19 Create a min-heap vector implementing the tree to the right 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 25 35 22 30 19 Add 10 to the min-heap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 35 22 30 19 25
47
Create a min-heap vector implementing the tree to the right
8 9 10 22 11 18 13 35 25 12 15 30 19 3 8 13 9 11 18 25 35 22 12 15 30 19 Create a min-heap vector implementing the tree to the right 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 25 35 22 30 19 Add 10 to the min-heap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 35 22 30 19 25 Remove a node from the min-heap 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 22 18 35 25 30 19
48
Priority Queues Heaps / Priority Queues The heap is used to implement a special kind of queue called a priority queue. Sometimes a FIFO queue may not be the best way to implement a waiting line. A priority queue is a data structure in which only the highest-priority item is accessible. In a print queue, sometimes it is more appropriate to print a short document that arrived after a very long document. A priority queue is a data structure in which only the highest-priority item is accessible (as opposed to the first item entered).
49
Insertion into a Priority Queue
Heaps / Priority Queues
50
priority_queue Class Heaps / Priority Queues C++ provides a priority_queue class that uses the same interface as the queue given in Chapter 6. The differences are in the specification for the top and pop functions—these are defined to return a largest item in the queue rather than an oldest item.
51
Heap Priority Queue Heaps / Priority Queues In a priority queue, just like a heap, the largest item always is removed first. Because heap insertion and removal is O(log n), a heap can be the basis of a very efficient implementation of a priority queue. We will call our class KW::priority_queue to differentiate it from class std::priority_queue in the C++ standard library, which also uses a heap as the basis of its implementation. The interfaces for our class and the standard class are identical, but the implementations are slightly different. To remove an item from the priority queue, we take the first item from the vector; this is the largest item in the max heap. We then remove the last item from the vector and put it into the first position of the vector, overwriting the value currently there. Then following the algorithm for a max heap, we move this item down until it is larger than its children or it has no children.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.