Quadruples and so on we define a class called item

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
Image of page 3

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
Image of page 4
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
Image of page 5

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
Image of page 6
CPSC 221 Data Structures & Algorithms The Templated Class BinaryHeap Our implementation of the priority queue ADT is based on the heap ADT.
Image of page 7

Subscribe to view the full document.

Image of page 8

{[ snackBarMessage ]}

Get FREE access by uploading your study materials

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
Christopher Reinemann
"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

Ask a question for free

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