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 N ) time problem: expensive to add/remove (O( N )) binary search tree : store in BST, search in O(log N ) time for min element problem: tree could be unbalanced barb4right

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 N ) 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 N ) 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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern