Download Data Structures: Priority Queues and Binary Heaps and more Slides Data Structures and Algorithms in PDF only on Docsity!
7/7/2012 Priorty Queues 6-
Chapter 6
Priority Queues
EE 232 Data Structures Session-05 , Spring-
7/7/2012 Priorty Queues 6-
Motivations
Printer Management
Three jobs are sent to a printer
in the order A, B and C
- Job A --- print a pdf file
- Job B --- print a word document
- Job C --- print the data structure ppt slides
One job might be particularly
important
Desirable to allow that job to run
as soon as the printer is
available
front rear
A B^ C
7/7/2012 Priorty Queues 6-
Motivations…
Multitasking Operating System
The scheduler of an operating system decides
which of the several processes to run
One algorithm of the scheduler uses FCFS queue
- Jobs are initially placed at end of the queue
- The scheduler will repeatedly take the first job on the queue, run it until either it finishes or its time limit is up and place it at the end of the queue if it does not finish
Interactive programs?
Processes such as Internet explorer would work?
7/7/2012 Priorty Queues 6-
Chapter 6: Priority Queues
6.1 Model
6.2 Simple Implementations
6.3 Binary Heap
6.4 Applications of Priority Queues
6.5 d - Heaps
6.6 Leftist Heaps
6.7 Skew Heaps
6.8 Binomial Queues
7/7/2012 Priorty Queues 6-
Chapter 6: Priority Queues
6.1 Model
6.2 Simple Implementations
6.3 Binary Heap
6.4 Applications of Priority Queues
6.5 d - Heaps
6.6 Leftist Heaps
6.7 Skew Heaps
6.8 Binomial Queues
7/7/2012 Priorty Queues 6-
Model
Priority queue is a data structure which allows at
least two operations
Insert
DeleteMin
- finds, returns and removes the minimum element in the priority queue
DeleteMin Priority Queue Insert
Basic model of a priority queue
7/7/2012 Priorty Queues 6-
Simple Implementations
Simple Linked List
Insert
DeleteMin
- traverse list in O ( N ) time
Sorted Linked List
Insert
• O ( N )
DeleteMin
• O (1)
7/7/2012 Priorty Queues 6-
Simple Implementations…
Binary Search Tree
Insert
DeleteMin
overkill to use BST
- no need for all the BST operations, pointers
7/7/2012 Priorty Queues 6-
More tree terminology
Degree of a node
The degree of a
node n is the number
of its children
Degree of a tree
The degree of a tree
is the maximum
degree of any of its
nodes
7/7/2012 Priorty Queues 6-
More tree terminology…
A node n in a binary
tree is full if it has
degree two
A full binary tree of
height h has leaves only
on level h and each of
its internal nodes is full
7/7/2012 Priorty Queues 6-
Binary Heap
Like binary search trees, heaps have two properties
Structure property Heap order property
Structure Property
A binary heap (or a heap) is a complete binary tree
A complete binary tree of height h has between 2 h
and 2 h +1^ -1 nodes
height of a complete binary tree is log N which is O (log N )
7/7/2012 Priorty Queues 6-
Binary Heap…
*We are able to store a complete tree as an array
Simply traverse the array in breadth-first order,
placing the entries into the array
*http://www.ece.uwaterloo.ca/~ece250/
For each depth, starting at zero: visit the nodes in the order from left to right
7/7/2012 Priorty Queues 6-
Binary Heap…
*To remove a node while keeping the structure, we
simply remove the last element in the array
*http://www.ece.uwaterloo.ca/~ece250/ docsity.com
7/7/2012 Priorty Queues 6-
Binary Heap…
For any element in array
position i
The left child is in position 2i The right child is in position 2i+ The parent is in position i/2
No pointer storage
Fast computation of 2i and
i/2 by bit shifting
i << 1 = 2i i >> 1 = i/2
A B C D E F G H I J
A
B C
D E F G
H I J