18. priority-queue

# Quadruples and so on we define a class called item

• Notes
• 14

This preview shows page 3 - 8 out of 14 pages.

quadruples and so on) we define a class called Item that stores these two objects as member variables and that provides functions to access and update these variables. We assume that key is an integer, but element can be of any type (therefore we use template). If we want to use keys which are not integers we need to choose an appropriate type for keys, but also we need to use a comparator class with a special comparison function for comparing keys. It makes implementation of the priority queue more complex but also more general. c circlecopyrt Teresa Leyk Slide 3 Heap

Subscribe to view the full document.

CPSC 221 Data Structures & Algorithms template < typename ElemType> class Item { private : int key; ElemType elem; public : Item( const int k=0, const ElemType& e=ElemType()) : key(k), elem(e) { } //constructor const int getKey() const { return key; } const ElemType& getElem() const { return elem; } void setKey( const int k) { key = k; } void setElem( const ElemType& e) { elem = e; } }; c circlecopyrt Teresa Leyk Slide 4 Heap
CPSC 221 Data Structures & Algorithms The Comparator Pattern An important issue in the priority queue ADT is how to compare keys. There are a few design choices. We choose a more general approach where we define a comparator function . A convenient way to provide such a function in C++ is to define a comparator class . Typically such a class has no data members and no other member functions. To define a comparison member function we overload the “ () ” operator. The resulting function takes two operands, a and b and returns an integer i such that i < 0, i = 0, or i > 0, depending on whether a < b , a = b , a > b , respectively. Below is an example of such a class where the arguments are of class Item<ElemType> and ElemType is any C++ type. c circlecopyrt Teresa Leyk Slide 5 Heap

Subscribe to view the full document.

CPSC 221 Data Structures & Algorithms template < typename ElemType> class PQComp { public : int operator ()( const Item<ElemType>& e1, const Item<ElemType>& e2) { return e1.getKey() - e2.getKey(); } }; c circlecopyrt Teresa Leyk Slide 6 Heap
CPSC 221 Data Structures & Algorithms The Templated Class BinaryHeap Our implementation of the priority queue ADT is based on the heap ADT.

Subscribe to view the full document.

{[ snackBarMessage ]}

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