CS301 – Data Structures Lecture No. 33 ___________________________________________________________________ Data Structures Lecture No. 33 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 6, 8 6.3, 8.1 Summary • Priority Queue Using Heap • The Selection Problem • Heap Sort • Disjoint Set ADT • Equivalence Relations Priority Queue Using Heap As discussed in the previous lecture, we generally prefer to employ the buildHeap to construct heap instead of using insert if we have all the required data. buildHeap is optimized as compared to insert method as it takes lesser time than Nlog 2 N . In the previous discussion, we had proved a theorem that if the number of links in the tree are counted, the sum of all is N-h-1 . We have been iterating a lot of times that the best use of heap is priority queue’s implementation. Let’s try to develop a class of priority queue with the help of a heap. You are very familiar with the concept of bank simulation. While explaining the bank simulation, we had used a priority queue that was implemented by using an array. Now, we will implement the same queue with the help of a heap. For this purpose, the code will be modified so that heap is used in place of array. The interface (.h file) will remain the same. However, the implementation (.cpp file) will be changed. Let’s see the following cpp code, which also shows the change in the .cpp file: 1. #include “Event.cpp” 2. #include “Heap.cpp” 3. #define PQMAX 30 4. class PriorityQueue 5. { 6. public: 7. PriorityQueue() Page 1 of 9

This
preview
has intentionally blurred sections.
Sign up to view the full version.

CS301 – Data Structures Lecture No. 33 ___________________________________________________________________ including .cpp files here as done in case of templates of C++. In the second line, there is heap.cpp while a constant PQMAX has been defined in third line, declaring the maximum size of the priority queue to be 30. In line 4, class PriorityQueue is declared. public

This
preview
has intentionally blurred sections.
Sign up to view the full version.