# Heap - Heaps 8:44 AM Priority Queue ADT(7.1 A priority...

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

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue.

### Page1 / 5

Heap - Heaps 8:44 AM Priority Queue ADT(7.1 A priority...

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

View Full Document
Ask a homework question - tutors are online