24-priority-queue

# 24-priority-queue - CSE 143 Lecture 24 Priority Queues;...

This preview shows pages 1–7. Sign up to view the full content.

CSE 143 Lecture 24 Priority Queues; Huffman Encoding slides created by Marty Stepp and Daniel Otero http://www.cs.washington.edu/143/

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

View Full Document
2 Prioritization problems • The CSE lab printers constantly accept and complete jobs from all over the building. Suppose we want them to print faculty jobs before staff before student jobs, and grad students before undergraduate students, etc.? • You are in charge of scheduling patients for treatment in the ER. A gunshot victim should probably get treatment sooner than that one guy with a sore neck, regardless of arrival time. How do we always choose the most urgent case when new patients continue to arrive? • Why can't we solve these problems efficiently with the data structures we have (list, sorted list, map, set, BST, etc.)?
3 Some poor choices list : store customers/jobs in a list; remove min/max by searching (O( N )) – problem: expensive to search sorted list : store in sorted list; binary search it in O(log ) time – problem: expensive to add/remove (O( )) binary search tree : store in BST, search in O(log ) time for min element – problem: tree could be unbalanced b

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

View Full Document
4 Priority queue ADT priority queue : a collection of ordered elements that provides fast access to the minimum (or maximum) element – usually implemented using a tree structure called a heap • priority queue operations: – add adds in order; O(log N ) worst – peek returns minimum value; O(1) always – remove removes/returns minimum value; O(log ) worst – isEmpty , clear , size , iterator O(1) always
5 Java's PriorityQueue class public class PriorityQueue< E > implements Queue< E > Queue<String> pq = new PriorityQueue <String>(); pq.add("Stuart"); pq.add("Marty"); ... removes/returns min element returns minimum element returns iterator over elements removes all elements adds value in sorted order constructs new empty queue Description O(log N ) O(1) O(1) O(1) O(log ) O(1) Runtime Method/Constructor PriorityQueue< E >() add( E value) clear() iterator() peek() remove()

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

View Full Document
6 Inside a priority queue • Usually implemented as a heap : a kind of binary tree. • Instead of sorted left
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 10/30/2011 for the course COMPUTER S 143 taught by Professor John during the Spring '11 term at Washington State Community College.

### Page1 / 23

24-priority-queue - CSE 143 Lecture 24 Priority Queues;...

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

View Full Document
Ask a homework question - tutors are online