Heap - Heaps 4/1/2003 8:44 AM Priority Queue ADT (7.1) A...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Heaps and Priority Queues 1 Heaps and Priority Queues 2 6 5 7 9 Heaps and Priority Queues 2 Priority Queue ADT (§7.1) A priority queue stores a collection of items An item is a pair (key, element) Main methods of the Priority Queue ADT ± insertItem (k, o) inserts an item with key k and element o ± removeMin () removes the item with the smallest key Additional methods ± minKey (k, o) returns, but does not remove, the smallest key of an item ± minElement () returns, but does not remove, the element of an item with smallest key ± size (), isEmpty () Applications: ± Standby flyers ± Auctions ± Stock market Heaps and Priority Queues 3 Total Order Relation Keys in a priority queue can be arbitrary objects on which an order is defined Two distinct items in a priority queue can have the same key Mathematical concept of total order relation ± Reflexive property: x x ± Antisymmetric property: x y y x x = y ± Transitive property: x y y z x z Heaps and Priority Queues 4 Comparator ADT (§7.1.4) A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses a comparator as a template argument, to define the comparison function (<,=,>) The comparator is external to the keys being compared. Thus, the same objects can be sorted in different ways by using different comparators. When the priority queue needs to compare two keys, it uses its comparator Heaps and Priority Queues 5 Using Comparators in C++ A comparator class overloads the “()” operator with a comparison function. Example: Compare two points in the plane lexicographically. class LexCompare { public: int operator()(Point a, Point b) { if (a.x < b.x) return –1 else if (a.x > b.x) return +1 else if (a.y < b.y) return –1 else if (a.y > b.y) return +1 else return 0; } }; To use the comparator, define an object of this type, and invoke it using its “()” operator: Example of usage: Point p(2.3, 4.5); Point q(1.7, 7.3); LexCompare lexCompare; if (lexCompare(p, q) < 0) cout << “p less than q”; else if (lexCompare(p, q) == 0) cout << “p equals q”; else if (lexCompare(p, q) > 0) cout << “p greater than q”; Heaps and Priority Queues 6 Sorting with a Priority Queue (§7.1.2) We can use a priority queue to sort a set of comparable elements ± Insert the elements one by one with a series of insertItem (e, e) operations ± Remove the elements in sorted order with a series of removeMin () operations The running time of this sorting method depends on the priority queue
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

Heap - Heaps 4/1/2003 8:44 AM Priority Queue ADT (7.1) A...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online