18. priority-queue

# Therefore we need a templated class binaryheap

This preview shows pages 7–12. Sign up to view the full content.

Therefore we need a templated class BinaryHeap . template < typename ElemType, typename Comp> class BinaryHeap { private : Comp comp; // Comp is a comparator class // and comp(a,b) will compare arguments a and b int curSize; //number of elements in heap ElemType * array; //(dynamic) heap array int length; //the length of the array static const int DEF_SIZE = 8; void getNewArray( int newSize) { array = new ElemType[newSize]; length = newSize; } c circlecopyrt Teresa Leyk Slide 7 Heap

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

View Full Document
CPSC 221 Data Structures & Algorithms public : // templated class BinaryHeap (cont) BinaryHeap( int size = DEF_SIZE) { //constructor curSize = 0; getNewArray(size); } ElemType& findMin() throw (EmptyHeap) { if ( isEmpty() ) throw EmptyHeap(); return array[0]; } bool isEmpty( ) const { return curSize == 0; } void buildHeap(); void insert( const ElemType& x); void deleteMin() throw (EmptyHeap); }; c circlecopyrt Teresa Leyk Slide 8 Heap
CPSC 221 Data Structures & Algorithms Implementation We present only three templated functions: insert , walkDown and deleteMin . The other BinaryHeap functions can be changed in a similar way. template < typename ElemType, typename Comp> void BinaryHeap<ElemType, Comp>::insert( const ElemType& x) { checkSize( ); //walk up (establish heap order now) int hole = curSize++; for ( ; hole > 0 && comp(array[(hole-1)/2], x) > 0; hole = (hole-1)/2) array[hole] = array[(hole-1)/2]; array[hole] = x; } c circlecopyrt Teresa Leyk Slide 9 Heap

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

View Full Document
CPSC 221 Data Structures & Algorithms template < typename ElemType, typename Comp> void BinaryHeap<ElemType, Comp>::deleteMin() throw (EmptyHeap) { array[0] = array[ --curSize ]; //decrease size walkDown(0); } template < typename ElemType, typename Comp> void BinaryHeap<ElemType, Comp>::walkDown( int hole) { int child; ElemType key = array[hole]; for ( ; 2 * hole+1 < curSize; hole = child) { child = 2 * hole+1; if (child != curSize-1 && comp(array[child], array[child+1]) > 0) child++; // right child = 2 * hole+2 if (comp(key, array[child]) > 0) array[hole]=array[child]; else break ; } array[hole] = key; } c circlecopyrt Teresa Leyk Slide 10 Heap
CPSC 221 Data Structures & Algorithms The Class PriorityQueue #include "BinaryHeap.h" // include the templated class Item here // include the templated class PQComp here template < typename ElemType> class PriorityQueue { protected : typedef Item<ElemType> Item; typedef

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern