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

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

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

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

View Full Document Right 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.)?
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 (O( N )) binary search tree : store in BST, search in O(log N ) time for min element problem: tree could be unbalanced barb4right
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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()
Image of page 5

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

View Full Document Right Arrow Icon
6 Inside a priority queue • Usually implemented as a heap : a kind of binary tree.
Image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern