Heaps & Priority Queues

Heaps & Priority Queues - Lecture 21: Priority Queues &...

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

View Full Document Right Arrow Icon
1 Lecture 21: Priority Queues PIC 10B Todd Wittman Priority Queues s But what if a patient comes in who needs immediate medical attention (priority 1)? s In a priority queue , each element is assigned a unique priority. s 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. s There are several ways to implement a priority queue. We will use the data structure called a heap. s Suppose we are keeping track of a line of patients entering a hospital emergency room. s In a queue, the first person to enter the emergency room is the first person to see the doctor (FIFO).
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 The Heap Data Structure s 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. s Each node of the heap stores a value and has two children, just like in a binary tree. s The Heap Property : Every node has a value smaller than both its children. s The Shape Property : Every level of the heap is full, except for possibly the last level which is filled left to right. s The Queue Property: Elements can be removed or accessed only at the root node. (No traversals.) 1 2 3 The Heap Concept s Here's an example of 2 possible heaps on the numbers 0-8. s Note that the Heap Property implies that the minimum value is always at the root. s
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/27/2010 for the course PIC 157-051-20 taught by Professor Wittman during the Winter '10 term at UCLA.

Page1 / 8

Heaps & Priority Queues - Lecture 21: Priority Queues &...

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

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