18. priority-queue

# Pqcompelemtype pqcomp private binaryheapitem pqcomp t

• Notes
• 14

This preview shows page 11 - 14 out of 14 pages.

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

Subscribe to view the full document.

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
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

Subscribe to view the full document.

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

{[ snackBarMessage ]}

Upload your study materials now and get free access to over 25 million documents.

Upload now for FREE access Or pay now for instant access
###### "Before using Course Hero my grade was at 78%. By the end of the semester my grade was at 90%. I could not have done it without all the class material I found."
— Christopher R., University of Rhode Island '15, Course Hero Intern

### 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