L17cs2110fa08

L17cs2110fa08 - Lecture 17 CS2110 Fall 2008 Priority Queues...

Info iconThis preview shows pages 1–9. 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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight 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 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)...
View Full Document

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell.

Page1 / 49

L17cs2110fa08 - Lecture 17 CS2110 Fall 2008 Priority Queues...

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

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