Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Priority Queues (Heaps) Priority Queue ADT Heap Implementation CS 340 Page 100 Heap Applications Leftist Heaps.

Similar presentations


Presentation on theme: "Chapter 6: Priority Queues (Heaps) Priority Queue ADT Heap Implementation CS 340 Page 100 Heap Applications Leftist Heaps."— Presentation transcript:

1

2 Chapter 6: Priority Queues (Heaps) Priority Queue ADT Heap Implementation CS 340 Page 100 Heap Applications Leftist Heaps

3 CS 340 Page 101 Often, a FIFO queue structure has a need for a prioritization capability, so elements with greater priority are removed before lower-priority elements that were actually inserted first. Examples: Short print jobs may be prioritized over longer print jobs on a printer queue. Real-time network applications (e.g., video, audio) may be prioritized over e- mail and simple file transfers on a network switch’s forwarding queue. System maintenance tasks (e.g., memory defragmentation, mouse interrupts) may be prioritized over application software tasks on an operating system’s job queue. Priority Queues

4 CS 340 Page 102 A heap is a complete binary tree in which every node’s value is less than or equal to all of its offsprings’ values. One Priority Queue Implementation: The Heap 1010 1717 25253131 4545616158584747 1313 15153030 2323 Note:One convenient aspect of the heap is that it can be stored in an array. 10101010 10101010 17171717 17171717 13131313 13131313 25252525 25252525 31313131 31313131 15151515 15151515 30303030 30303030 45454545 45454545 61616161 61616161 58585858 58585858 47474747 47474747 23232323 23232323 Offspring of node i: nodes 2i+1 and 2i+2 Parent of node i: node  (i - 1) / 2 

5 CS 340 Page 103 When inserting a new element into a heap, start at the new leaf node that would maintain the binary tree’s completeness, and “percolate” the new element up to its appropriate position to maintain the heap order property. Inserting Into A Heap 1010 1717 25253131 454561615858 4747 1313 15153030 2323 1010 1717 25253131 454561615858 4747 1313 15153030 23231212 Insert 12 1010 1717 25253131 454561615858 4747 1313 12123030 23231515 Percolate Up 1010 1717 25253131 454561615858 4747 1212 13133030 23231515

6 CS 340 Page 104 When deleting the minimum element from a heap, create a “hole” node at the root (where the minimum element was), and slide the smaller of the hole’s offspring up until an appropriate slot is found for the last element. Deleting From A Heap 2929 4343 65655858 75758787 80808080 80808080 9191 51515151 51515151 63636363 636363637777 7373 Delete Min Percolate Down 4343 65655858 757587878080 9191 5151 63637777 7373 4343 65655858 757587878080 9191 5151 63637777 7373 4343 65657373 757587878080 9191 5151 63637777 5858

7 CS 340 Page 105 Rather than going to the expense of implementing a complicated system, sometimes it is possible to simulate the system using a statistical model, and to work out the obvious bugs prior to actual implementation. A heap makes a convenient structure in such simulations, where the heap nodes represent the discrete “events” of the system, ordered according to the time at which they “occur”. Example Application: Discrete Event Simulation Network Simulation Example

8 CS 340 Page 106 Network Simulation Example (Continued) 045: PC1B xmits on TR1 053: ATMS1 xmits on FB 072: PC1B xmits on TR1 068: PC2F xmits on TR2 049: ATMS2 recvs on TR2 080: PC1D xmits on TR1 059: PC2B recvs on TR2 049: ATMS2 recvs on TR2 053: ATMS1 xmits on FB 072: PC1B xmits on TR1 068: PC2F xmits on TR2 059: PC2B recvs on TR2 080: PC1D xmits on TR1 Delete Minimum Process Event 047: ATMS1 recvs on TR1 053: ATMS1 xmits on FB 072: PC1B xmits on TR1 068: PC2F xmits on TR2 049: ATMS2 recvs on TR2 080: PC1D xmits on TR1 059: PC2B recvs on TR2 Delete Minimum 049: ATMS2 recvs on TR2 053: ATMS1 xmits on FB 072: PC1B xmits on TR1 068: PC2F xmits on TR2 059: PC2B recvs on TR2 080: PC1D xmits on TR1 Process Event 049: ATMS2 recvs on TR2 053: ATMS1 xmits on FB 072: PC1B xmits on TR1 068: PC2F xmits on TR2 050: ATMS1 xmits on FB 080: PC1D xmits on TR1 059: PC2B recvs on TR2 Delete Minimum...

9 CS 340 Page 107 One weakness of the heap structure is the difficulty with which two heaps are merged into one. Merging Heaps 1010 1717 25253131 454561615858 4747 1212 13133030 23231515 3737 3939 63634141 717190905454 4646 4242 55554848 6464757549495252 878778789393 How would you merge the two heaps above into a single heap, maintaining the completeness of the binary tree and the heap order property? When might it be necessary to merge two heaps? One printer goes down, and its print jobs are redirected to a second printer with its own priority queue. One printer goes down, and its print jobs are redirected to a second printer with its own priority queue. One network route becomes too congested so a switch must merge the forwarding queues for two of its outgoing lines. One network route becomes too congested so a switch must merge the forwarding queues for two of its outgoing lines. The operating system for a multiprocessor system decides to devote one processor to a certain task, merging its job queue with that of another processor. The operating system for a multiprocessor system decides to devote one processor to a certain task, merging its job queue with that of another processor.

10 CS 340 Page 108 One solution to the priority queue merging problem is the leftist heap. For any node X in a binary tree, define nullPathLength(X) to be the length of the shortest path from X to a descendant node without two children. A leftist heap is a binary tree with the heap order property (i.e., every node’s value is less than or equal to its offsprings’ values), as well as the leftist heap property: the null path length of each node’s left child is greater than or equal to the null path length of its right child. Like the heap, the leftist heap performs insertions and removals in O(logn) time, but the leftist heap also performs merges in O(logn) time, a big improvement over the heap’s O(n) merge time. Examples: Leftist Heaps 1010 1717 25253131 454561615858 1212 13137777 2323 77 1111 24244646 373763635050 5959 71718484 9595

11 CS 340 Page 109 template leftNode * leftistHeap :: Merge(leftNode *h1, leftNode *h2) { if (h1 == NULL) return h2; if (h2 == NULL) return h1; if (h2->element > h1->element) return Merge1(h1, h2); return Merge1(h2, h1); } template leftNode * leftistHeap :: Merge1(leftNode *h1, leftNode *h2) { if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1; } template leftNode * leftistHeap :: Merge(leftNode *h1, leftNode *h2) { if (h1 == NULL) return h2; if (h2 == NULL) return h1; if (h2->element > h1->element) return Merge1(h1, h2); return Merge1(h2, h1); } template leftNode * leftistHeap :: Merge1(leftNode *h1, leftNode *h2) { if (h1->left == NULL) h1->left = h2; else { h1->right = Merge(h1->right, h2); if (h1->left->nullPathLength right->nullPathLength) Swap(h1->left, h1->right); h1->nullPathLength = h1->right->nullPathLength + 1; } return h1; } Merging Leftist Heaps

12 CS 340 Page 110 Leftist Heap Merging Example 7777 5959 2323 1313 1212 5959 7777 1010 4545 2525 61615858 3131 1717 2323 1313 1212 5959 7777 3737 2424 63635050 4646 1111 84847171 9595 77 7777 1010 4545 2525 61615858 3131 1717 2323 1313 1212 5959 3737 2424 63635050 4646 1111 84847171 9595 7777 1010 4545 2525 61615858 3131 1717 2323 1313 1212 5959 77 1010 4545 2525 61615858 3131 1717 2323 1313 1212 7777 77 3737 2424 63635050 4646 1111 84847171 5959 9595 Original Merge Call 1010 4545 2525 61615858 3131 1717 2323 1313 1212 7777 5959 1st Recursive Call 2323 1313 1212 7777 2nd Recursive Call 5959 77775959 3rd Recursive Call Final Swap

13 CS 340 Page 111 STL Priority Queues The Standard Template Library includes a template class for priority queues, priority_queue, implemented as a maximum heap. #include using namespace std; void main( ) { // The first priority_queue uses the // default vector base container priority_queue q1; q1.push( 87 ); q1.push( 65 ); q1.push( 43 ); q1.push( 21 ); cout << "q1 = ( "; while ( !q1.empty( ) ) { cout << q1.top( ) << " "; q1.pop( ); } cout << ")" << endl; // The second priority_queue uses the vector // base container, but specifies that the comparison // function greater be used for ordering elements, // i.e., that the priority queue be a min-heap. priority_queue, greater > q2; q2.push( 87 ); q2.push( 65 ); q2.push( 43 ); q2.push( 21 ); cout << "q2 = ( "; while ( !q2.empty( ) ) { cout << q2.top( ) << " "; q2.pop( ); } cout << ")" << endl; } #include using namespace std; void main( ) { // The first priority_queue uses the // default vector base container priority_queue q1; q1.push( 87 ); q1.push( 65 ); q1.push( 43 ); q1.push( 21 ); cout << "q1 = ( "; while ( !q1.empty( ) ) { cout << q1.top( ) << " "; q1.pop( ); } cout << ")" << endl; // The second priority_queue uses the vector // base container, but specifies that the comparison // function greater be used for ordering elements, // i.e., that the priority queue be a min-heap. priority_queue, greater > q2; q2.push( 87 ); q2.push( 65 ); q2.push( 43 ); q2.push( 21 ); cout << "q2 = ( "; while ( !q2.empty( ) ) { cout << q2.top( ) << " "; q2.pop( ); } cout << ")" << endl; }


Download ppt "Chapter 6: Priority Queues (Heaps) Priority Queue ADT Heap Implementation CS 340 Page 100 Heap Applications Leftist Heaps."

Similar presentations


Ads by Google