24-priority-queue

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

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

CSE 143 Lecture 24 Priority Queues and 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 N) time – problem: expensive to add/remove • binary search tree : store in BST, search in O(log N) time for min element – problem: tree could be unbalanced Î • auto-balancing BST – problem: extra work must be done to constantly re-balance the tree

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 – a mix between a queue and a BST – usually implemented using a tree structure called a heap • priority queue operations: – add adds in order; O(log N) worst – peek returns minimum element; O(1) – remove removes/returns minimum element; O(log N) worst – isEmpty , clear , size , iterator O(1)
5 Java's PriorityQueue class public class PriorityQueue< E > implements Queue< E > 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 N ) O(1) Avg. Runtime Method/Constructor public PriorityQueue< E >() public void add( E value) public void clear() public Iterator< E > iterator() public E peek() public E 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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 24

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

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

View Full Document
Ask a homework question - tutors are online