1 / 81

810 likes | 943 Vues

Priority Queues. Briana B. Morrison Adapted from Alan Eugenio. Topics. API Application Implementation. Priority Queue. A Special form of queue from which items are removed according to their designated priority and not the order in which they entered.

Télécharger la présentation
## Priority Queues

**An Image/Link below is provided (as is) to download presentation**
Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.
Content is provided to you AS IS for your information and personal use only.
Download presentation by click this link.
While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.
During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

**Priority Queues**Briana B. Morrison Adapted from Alan Eugenio**Topics**• API • Application • Implementation Priority Queues**Priority Queue**A Special form of queue from which items are removed according to their designated priority and not the order in which they entered. Items entered the queue in sequential order but will be removed in the order #2, #1, #4, #3. Priority Queues**A PRIORITY QUEUE IS A CONTAINER**IN WHICH ACCESS OR DELETION IS OF THE HIGHEST-PRIORITY ITEM, ACCORDING TO SOME WAY OF ASSIGNING PRIORITIES TO ITEMS. Priority Queues**priority_queue();**Create an empty priority queue. Type T must implement the operator <. CLASS priority_queue CLASS priority_queue <queue> <queue> Constructor Operations bool empty() const; Check whether the priority queue is empty. Return true if it is empty, and false otherwise. Create void pop(); Remove the item of highest priority from the queue. Precondition: The priority queue is not empty. Postcondition: The priority queue has 1 less element Priority Queues**CLASS priority_queue**<queue> Operations void push(const T& item); Insert the argument item into the priority queue. Postcondition: The priority queue contains a new element. int size() const; Return the number of items in the priority queue. T& top(); Return a reference to the item having the highest priority. Precondition: The priority queue is not empty. const T& top(); Constant version of top(). Priority Queues**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 Comparator ADT Priority Queues**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; }}; Using Comparators in C++ 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”; Priority Queues**Applications**• Applications: • Standby flyers • Auctions • Stock market • Sorting • Huffman Coding Priority Queues**Sorting with a Priority Queue**• We can use a priority queue to sort a set of comparable elements • Insert the elements one by one with a series of push(e) operations • Remove the elements in sorted order with a series of pop() operations • The running time of this sorting method depends on the priority queue implementation AlgorithmPQ-Sort(S, C) • Inputsequence S, comparator C for the elements of S • Outputsequence S sorted in increasing order according to C P priority queue with comparator C while!S.empty () e S.remove (S.first ()) P.push(e) while!P.empty() e P.top()P.pop() S.insertLast(e) Priority Queues**Huffman Trees**Problem Input: A set of symbols, each with a frequency of occurrence. Desired output: A Huffman tree giving a code that minimizes the bit length of strings consisting of those symbols with that frequency of occurrence. Strategy: Starting with single-symbol trees, repeatedly combine the two lowest-frequency trees, giving one new tree of frequency = sum of the two frequencies. Stop when we have a single tree. Priority Queues

More Related