17-Heaps - 6/15/2011 CS/ENGRD 2110 Object-Oriented...

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

View Full Document Right Arrow Icon
1 CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2011 Thorsten Joachims Lecture 17: Heaps and Priority Queues Stacks and Queues as Lists Stack (LIFO) implemented as list insert (i.e. push) to, extract (i.e. pop) from front of list Queue (FIFO) implemented as list insert (i.e. add) on back of list, extract (i.e. poll) from front of list All operations are O(1) 55 120 19 16 first last Priority Queue ADT Definition data items are Comparable lesser elements (as determined by compareTo() ) have higher priority extract() returns the element with the highest priority i.e. least in the compareTo() ordering break ties arbitrarily alternatively could break ties FIFO, but lets keep it simple 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 (i.e. queue) insert() puts new element at front O(1) extract() must search the list O(n) Maintain as ordered list insert() must search the list
Background image of page 1

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

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

This note was uploaded on 09/05/2011 for the course CS 211 at Cornell University (Engineering School).

Page1 / 8

17-Heaps - 6/15/2011 CS/ENGRD 2110 Object-Oriented...

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

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