Heaps &amp; Priority Queues

# Heaps &amp; Priority Queues - Lecture 21 Priority...

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

1 Lecture 21: Priority Queues & Heaps PIC 10B Todd Wittman Priority Queues square6 But what if a patient comes in who needs immediate medical attention (priority 1)? square6 In a priority queue , each element is assigned a unique priority. square6 Elements with the highest priority should be the first to leave the queue (HPFO?). 3, 2, 8 1, 5, 6, 7 The first one to leave is the 1. square6 There are several ways to implement a priority queue. We will use the data structure called a heap. square6 Suppose we are keeping track of a line of patients entering a hospital emergency room. square6 In a queue, the first person to enter the emergency room is the first person to see the doctor (FIFO).

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

View Full Document
2 The Heap Data Structure square6 A heap (or min-heap) is a data structure that is a cross between a queue and a binary search tree. Essentially it has the shape of a binary tree and the ordering of a queue. square6 Each node of the heap stores a value and has two children, just like in a binary tree. square6 The Heap Property : Every node has a value smaller than both its children. square6 The Shape Property : Every level of the heap is full, except for possibly the last level which is filled left to right. square6 The Queue Property: Elements can be removed or accessed only at the root node. (No traversals.) 1 2 3 The Heap Concept square6 Here's an example of 2 possible heaps on the numbers 0-8. square6 Note that the Heap Property implies that the minimum value is always at the root.
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