24-priority-queue - CSE143 Lecture24...

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

View Full Document Right Arrow Icon
CSE 143 Lecture 24 Priority Queues and Huffman Encoding slides created by Marty Stepp and Daniel Otero http://www.cs.washington.edu/143/
Background image of page 1

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

View Full DocumentRight Arrow Icon
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.)?
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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)
Background image of page 4
5 Java's  PriorityQueue  class public class PriorityQueue< E > implements Queue< E > Method/Constructor Description Avg.  Runtime public PriorityQueue< E >() constructs new empty queue O(1) public void add( E value) adds value in sorted order O(log  N  ) public void clear() removes all elements O(1) public Iterator< E > iterator() returns iterator over elements O(1) public E peek() returns minimum element O(1) public E remove() removes/returns min element O(log  N  )
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 Inside a priority queue Usually implemented as a "heap": a kind of binary tree. Instead of sorted left 
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 24

24-priority-queue - CSE143 Lecture24...

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

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