L16cs211sp07-6up - The Bag Interface Priority Queues and Heaps interface Bag void put(E obj E get/extract some element boolean isEmpty Lecture

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

View Full Document Right Arrow Icon
Lecture 16 CS211 Spring 2007 Priority Queues and Heaps The Bag Interface interface Bag<E> { void put(E obj); E get(); //extract some element boolean isEmpty(); } Examples: Stack, Queue Stacks and Queues as Lists • Stack (LIFO) implemented as list put() , get() from front of list • Queue (FIFO) implemented as list put() on back of list, get() from front of list •A l l Bag operations are O(1) 55 120 19 16 first last Priority Queue Bag in which data items are Comparable lesser elements (as determined by compareTo() ) have higher priority get() returns the element with the highest priority = least in the compareTo() ordering • break ties arbitrarily 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 Priority Queues java.util.PriorityQueue<E> boolean add(E e) {. ..} //insert an element (put) void clear() {. ..} //remove all elements E peek() {. ..} //return min element without removing //(null if empty) E poll() {. ..} //remove min element (get) //(null if empty) int size() {. ..}
Background image of page 1

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

View Full DocumentRight Arrow Icon
Priority Queues as Lists • Maintain as unordered list add() puts new element at front – O(1) poll() must search the list – O(n) • Maintain as ordered list add() must search the list – O(n) poll() 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 add()
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/20/2008 for the course COM S 211 taught by Professor Kozen during the Spring '06 term at Cornell University (Engineering School).

Page1 / 8

L16cs211sp07-6up - The Bag Interface Priority Queues and Heaps interface Bag void put(E obj E get/extract some element boolean isEmpty Lecture

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