L17cs2110fa08-6up - Lecture 17 CS2110 Fall 2008 Priority...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Lecture 17 CS2110 Fall 2008 Priority Queues and Heaps Announcements Some changes to the CS major to be announced soon 2111 no longer required for new majors A&S and Engr students may drop 2111 until Nov 14 without transcript annotation If this affects you, please see me offline The Bag Interface interface Bag<E> { void insert(E obj); E extract(); //extract some element boolean isEmpty(); } Examples: Stack, Queue, PriorityQueue Stacks and Queues as Lists Stack (LIFO) implemented as list insert() , extract() from front of list Queue (FIFO) implemented as list insert() on back of list, extract() from front of list All Bag operations are O(1) 55 120 19 16 first last Priority Queue A Bag in which data items are Comparable lesser elements (as determined by compareTo() ) have higher priority extract() returns the element with the highest priority = least in the compareTo() ordering break ties arbitrarily Priority Queue Examples Scheduling jobs to run on a computer default priority = arrival time priority can be changed by operator Scheduling events to be processed by an event handler priority = time of occurrence Airline check-in first class, business class, coach FIFO within each class java.util.PriorityQueue<E> boolean add(E e) {...} //insert an element (insert) void clear() {...} //remove all elements E peek() {...} //return min element without removing //(null if empty) E poll() {...} //remove min element (extract) //(null if empty) int size() {...} Priority Queues as Lists Maintain as unordered list insert() puts new element at front O(1) extract() must search the list O(n) Maintain as ordered list insert() must search the list O(n) extract() gets element at front O(1) In either case, O(n 2 ) to process n elements Can we do better? Important Special Case Fixed number of priority levels 0,...,p 1 FIFO within each level Example: airline check-in insert() insert in appropriate queue O(1) extract() must find a nonempty queue O(p) Heaps A heap is a concrete data structure that can be used to implement priority queues...
View Full Document

Page1 / 9

L17cs2110fa08-6up - Lecture 17 CS2110 Fall 2008 Priority...

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

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