Pqcompelemtype pqcomp private binaryheapitem pqcomp t

Info icon This preview shows pages 11–14. Sign up to view the full content.

PQComp<ElemType> PQComp; private : BinaryHeap<Item, PQComp> T; static const int DEF_SIZE = 8; c circlecopyrt Teresa Leyk Slide 11 Heap
Image of page 11

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

CPSC 221 Data Structures & Algorithms public : // class PriorityQueue cont. PriorityQueue( int size = DEF_SIZE) : T(size) { } bool isEmpty() const { return T.isEmpty(); } void insertItem( const int k, const ElemType& e) { T.insert(Item(k, e)); } const ElemType& minElement() throw (EmptyPriorityQueue) { if (T.isEmpty()) throw EmptyPriorityQueue(); return T.findMin().getElem(); } const int minKey() throw (EmpyPriorityQueue) { if (T.isEmpty()) throw EmptyPriorityQueue(); return T.findMin().getKey(); } void removeMin() throw (EmptyPriorityQueue) { if (T.isEmpty()) throw EmptyPriorityQueue(); T.deleteMin(); } }; c circlecopyrt Teresa Leyk Slide 12 Heap
Image of page 12
CPSC 221 Data Structures & Algorithms Example This is a simple example showing how to use the class PriorityQueue . #include "PriorityQueue.h" int main() { PriorityQueue<string> pq; string elem; elem = "abc"; pq.insertItem(3, elem); elem = "bbc"; pq.insertItem(7, elem); elem = "cnn"; pq.insertItem(8, elem); cout << "minElement: " << pq.minElement() << endl; cout << "minKey: " << pq.minKey() << endl; cout << "Min element removed." << endl; pq.removeMin(); return 0; } c circlecopyrt Teresa Leyk Slide 13 Heap
Image of page 13

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

CPSC 221 Data Structures & Algorithms Running Time The running time of: minElement() and minKey() is Θ ( 1 ) removeMin() is O ( lg n ) insertItem() is O ( lg n ) c circlecopyrt Teresa Leyk Slide 14 Heap
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern