Download presentation
Presentation is loading. Please wait.
1
Implementing a Priority Queue
Heaps
2
Priority Queues Recall: 3 Container Adapters Priority Queue Stack
Priority Queue – implementation? Priority Queue Uses std::vector Maintains complete tree w/special ordering property Vector w/property is Heap
3
Binary Trees V <= V > V
4
Heaps (Max Heap) V <= V <= V
5
Binary Tree vs. Heap 6 13 3 9 9 3 1 7 13 7 6 1
6
Heap Shape Property A heap must be a “complete” binary tree
This means the levels of the tree will always be filled from left-to-right 13 Level 0 9 3 Level 1 7 6 1 Level 2
7
Insertion 13 9 3 7 6 1
8
Insertion 13 9 3 To maintain the Shape property, we must insert at the end of level 2 7 6 1 8
9
Insertion 13 9 3 But if we insert the 8 there the heap ordering property isn’t maintained! 8 <= 3 7 6 1 8
10
Insertion 13 8 <= 13 9 8 So we do the only sensible thing and swap the values! 3 <= 8 7 6 1 3
11
Insertion 9 8 This strategy applies for more than one “step”, too 7 6
13 9 8 This strategy applies for more than one “step”, too 7 6 1 3 18
12
Insertion 13 9 8 18 <= 7 7 6 1 3 18
13
Insertion 13 9 8 18 <= 9 18 6 1 3 7
14
Insertion 13 18 8 18 <= 13 9 6 1 3 7
15
Insertion 18 13 8 13 <= 18 9 6 1 3 7
16
Insertion – Done 18 13 8 The “13” node “bubbled up” from the bottom to its final position This operation is known as upheap() while n.data > parent(n).data: swap(n.data, parent(n).data) n = parent(n) if n == null: break 9 6 1 3 7
17
Removal We can only remove from the top
18 We can only remove from the top We need to maintain the shape property We need to maintain ordering property 13 8 9 6 1 3 7
18
Removal 7 Swap the “root” with the last
We compare the node with its two children Choose the larger one and swap Repeat until in place 13 8 9 6 1 3 18 removed
19
Removal Swap the “root” with the last
13 Swap the “root” with the last We compare the node with its two children Choose the larger one and swap Repeat until in place 7 8 9 6 1 3 18 removed
20
Removal Swap the “root” with the last
13 Swap the “root” with the last We compare the node with its two children Choose the larger one and swap Repeat until in place 9 8 7 6 1 3 18 removed
21
Removal The “7” node “bubbled down” from the topto its final position
13 The “7” node “bubbled down” from the topto its final position This operation is known as downheap() 9 8 7 6 1 3 18 removed
22
Heaps Are Arrays parent(i) = (i – 1) / 2 left(i) = 2 * i + 1
13 parent(i) = (i – 1) / 2 left(i) = 2 * i + 1 right(i) = 2 * i + 2 9 8 1 2 7 6 1 3 3 4 5 6
23
Complete Binary Tree Why store tree in vector? parent (i) = p (i) = ?
leftChild (i) = lc (i) = ? rightChild (i) = rc (i) = ?
24
Max and Min Heaps Max Heap property:
( nodes X) [ Value (X) >= Value (Child (X)) ]
25
priority_queue::push
v.push_back (50);
26
Upheap (restore heap property)
upHeap (v.size () – 1);
27
Push void PQ::push (const T& item) { v.push_back (item); upHeap (v.size () – 1); }
28
upHeap (Helper Method)
void PQ::upHeap (size_t pos) { T item = v[pos]; size_t i; // Move parent down for (i = pos; i != 0 && item > v[p (i)]; i = p (i)) v[i] = v[p (i)]; // swap unnecessary v[i] = item; }
29
priority_queue::pop v.front () = v.back (); v.pop_back (); 18
30
downHeap
31
Pop void PQ::pop () { v[0] = v.back (); v.pop_back (); // O (1) // Move elem down to proper place downHeap (0); }
32
downHeap (Helper Method)
void PQ::downHeap (size_t pos) { // Move v[pos] down, max child up size_t i, mc; T val = v[pos]; for (i = pos; (mc = lc (i)) < v.size (); i = mc) { if (mc + 1 < v.size () && v[mc] < v[mc + 1]) ++mc; if (val ??) // Move child up if necessary else ? } // Place val in correct spot
33
Heap Sort Overview Heaps O(N*lg (N)) sort in worst case
Can use heaps to sort in two ways 1) pqSort Push all elements Pop and place in vector back to front Complexity? 2*Sum (i =1:N) [ lg(i) ]
34
Heap Sort Overview 2) True heap sort (better, why?)
“Heapify” vector (O(N)) With STL assistance (make_heap) Roll our own “buildHeap” elemsToPlace = v.size () - 1 while (elemsToPlace > 0) Swap front and back of v --elemsToPlace Restore heap property at root (taking into consideration heap is one elem. smaller) In-place
35
make_heap (STL) int arr[] { 50, 20, 75, 35, 25 };
// Header <algorithm> make_heap (arr, arr + 5);
36
buildHeap Potential algorithms (several don’t work!)
Call downHeap (0) N times Call downHeap (i) varying i from 0 to N-1 Call downHeap (i) varying i from N-1 to 0 Call upHeap (i) varying i from 0 to N-1 Call upHeap (i) varying i from N-1 to 0
37
buildHeap Start at last internal node: position = ? Then do
downHeap (position)
38
buildHeap Cont’d
39
Heap Sorted Vector Now convert heap to sorted vector
Swap front and back Decrease size of heap by 1 downheap (0)
40
Heap Sorted Vector 4 2 7 6 9 10 5 3 1 a[ ]: Heap 7 2 4 5 3 9 6 10
a[ ]: Heap 7 2 4 5 3 9 6 10 Heap is in a[0..7] and the sorted region is empty Swap front and back
41
Heap Sorted Vector 4 2 7 6 9 3 5 1 10 a[ ]: Semiheap Sorted
10 a[ ]: Semiheap Sorted downHeap a[0..6] now represents a semiheap a[7] is the sorted region 7 2 4 5 9 6 3
42
Heap Sorted Vector 4 2 7 6 3 9 5 1 10 a[ ]: Becoming a Heap Sorted 7
10 a[ ]: Becoming a Heap Sorted downHeap 7 2 4 5 3 6 9
43
Heap Sorted Vector 4 2 3 6 7 9 5 1 10 a[ ]: Heap Sorted 3 2 4 5 7 6
10 a[ ]: Heap Sorted 3 2 4 5 7 6 9
44
Heap Sorted Vector 4 2 3 6 7 5 1 10 9 a[ ]: Semiheap Sorted 3 2 4 7
10 9 a[ ]: Semiheap Sorted downHeap 3 2 4 7 6 5
45
Heap Sorted Vector 4 2 3 6 5 7 1 10 9 a[ ]: Heap Sorted 3 2 4 5 6 7
10 9 a[ ]: Heap Sorted downHeap 3 2 4 5 6 7
46
Heap Sorted Vector 7 2 3 6 5 4 1 10 9 a[ ]: Semiheap Sorted 3 2 5 6
10 9 a[ ]: Semiheap Sorted downHeap 3 2 5 6 4
47
Heap Sorted Vector 7 2 3 4 5 6 1 10 9 a[ ]: Heap Sorted 3 2 5 4 6
48
Heap Sorted Vector 7 6 3 4 5 2 1 10 9 a[ ]: Semiheap Sorted 3 5 4 2
10 9 a[ ]: Semiheap Sorted downHeap 3 5 4 2
49
Transform a Heap Into a Sorted Array: Example
7 6 3 4 2 5 1 10 9 a[ ]: Sorted Becoming a Heap downHeap 3 2 4 5
50
Heap Sorted Vector 7 6 2 4 3 5 1 10 9 a[ ]: Heap Sorted 2 3 4 5
51
Heap Sorted Vector 7 6 5 4 3 2 1 10 9 a[ ]: Semiheap Sorted 3 4 2
10 9 a[ ]: Semiheap Sorted downHeap 3 4 2
52
Heap Sorted Vector 7 6 5 2 3 4 1 10 9 a[ ]: Heap Sorted 3 2 4
53
Heap Sorted Vector 7 6 5 4 3 2 1 10 9 a[ ]: Semiheap Sorted 3 2
10 9 a[ ]: Semiheap Sorted downHeap 3 2
54
Heap Sorted Vector 7 6 5 4 2 3 1 10 9 a[ ]: Heap Sorted 2 3
55
Heap Sorted Vector 7 6 5 4 3 2 1 10 9 a[ ]: Heap Sorted 2
56
Heap Sorted Vector 7 6 5 4 3 2 1 10 9 a[ ]: Sorted
57
Heap Sort (Code) // Create heap from vector bottom up // Start with last internal node // O(N) for “for” loop for (i = (N – 2) / 2; i >= 0; --i) downHeap (i); // O(N lg(N)) for “while” while (N > 1) { swap (v[0], v[N-1]); --N; downHeap (0); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.