Heap-2 - Heaps 2/9/2006 12:37 AM Heaps and Priority Queues...

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

View Full Document Right Arrow Icon
Heaps 2/9/2006 12:37 AM 1 Heaps and Priority Queues 1 Heaps and Priority Queues 2 6 5 7 9 Heaps and Priority Queues 2 Priority Queue ADT (§ 2.4.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 smallest key and returns its element Additional methods ± minKey () 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
Background image of page 1

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

View Full DocumentRight Arrow Icon
Heaps 2/9/2006 12:37 AM 2 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 (§ 2.4.1) A comparator encapsulates the action of comparing two objects according to a given total order relation A generic priority queue uses an auxiliary comparator The comparator is external to the keys being compared When the priority queue needs to compare two keys, it uses its comparator Methods of the Comparator ADT, all with Boolean return type ± isLessThan (x, y) ± isLessThanOrEqualTo (x,y) ± isEqualTo (x,y) ± isGreaterThan (x, y) ± isGreaterThanOrEqualTo (x,y) ± isComparable (x)
Background image of page 2
Heaps 2/9/2006 12:37 AM 3 Heaps and Priority Queues 5 Sorting with a Priority Queue (§ 2.4.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 Algorithm PQ-Sort ( S, C ) Input sequence S , comparator C for the elements of S Output sequence S sorted in increasing order according to C P priority queue with comparator C while ¬ S.isEmpty () e S.remove ( S. first ()) P.insertItem ( e , e ) while ¬ P.isEmpty () e P.removeMin () S.insertLast ( e ) Heaps and Priority Queues 6 Sequence-based Priority Queue Implementation with an unsorted list Performance: ± insertItem takes O (1) time
Background image of page 3

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

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

Page1 / 13

Heap-2 - Heaps 2/9/2006 12:37 AM Heaps and Priority Queues...

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

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