{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Quadruples and so on we define a class called item

Info iconThis preview shows pages 3–8. Sign up to view the full content.

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

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> 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
Background 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
Background image of page 5

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> 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
Background 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.
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
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}