CS301-Lec33 handout - CS301 Data Structures Lecture No. 33...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 33 ___________________________________________________________________ 8. { 9. heap = new Heap <Event> ( PQMAX ); 10. }; 11. 12. ~PriorityQueue() 13. { 14. delete heap; 15. }; 16. 17. Event * remove() 18. { 19. if( !heap->isEmpty() ) 20. { 21. Event * e; 22. heap->deleteMin( e ); 23. return e; 24. } 25. cout << "remove - queue is empty." << endl; 26. return (Event *) NULL; 27. }; 28. 29. int insert(Event * e) 30. { 31. if( !heap->isFull() ) 1. { 33. heap->insert( e ); 34. return 1; 35. } 36. cout << "insert queue is full." << endl; 37. return 0; 38. }; 39. 40. int full(void) 41. { 42. return heap->isFull(); 43. }; 44. 45. int length() 46. { 47. return heap->getSize(); 48. }; 49. }; The first line has a file Event.cpp that contains all the events used for simulation. We are Page 2 of 9
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 9

CS301-Lec33 handout - CS301 Data Structures Lecture No. 33...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online