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

Info icon This 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 implementation
Image of page 1

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

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

{[ snackBarMessage ]}

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