Lec20 - Priority Queues

Lec20 Priority - Lecture 20 Priority Queues Heaps PIC 10B Todd Wittman Priority Queues Suppose we are keeping track of a line of patients entering

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

1 Lecture 20: 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).

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

View Full Document
2 The Heap Concept 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
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/19/2011 for the course PIC 10B taught by Professor Wittman during the Winter '08 term at UCLA.

Page1 / 8

Lec20 Priority - Lecture 20 Priority Queues Heaps PIC 10B Todd Wittman Priority Queues Suppose we are keeping track of a line of patients entering

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

View Full Document
Ask a homework question - tutors are online