{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

L16cs211sp07-6up - The Bag Interface Priority Queues and...

Info icon This 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 • 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 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() {...}
Image of page 1

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

View Full Document Right Arrow Icon