{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Therefore we need a templated class binaryheap

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

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

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

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

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

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

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

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}