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 minheap)
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 08.
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.
 Winter '08
 Wittman

Click to edit the document details