This preview shows pages 1–5. Sign up to view the full content.
3. Priority Queues
3. Priority Queues
•
ADT Stack : LIFO.
•
ADT Queue : FIFO.
•
ADT Priority Queue : pick the element with the lowest (or
highest) priority.
Malek Mouhoub, CS340 Winter 2007
1
3. Priority Queues
3. Priority Queues
•
3.1 Model
•
3.2 Implementation
•
3.3 Binary Heap
•
3.4 dHeaps
•
3.5 MinMax Heap
Malek Mouhoub, CS340 Winter 2007
2
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 3.2 Implementation
3.2 Implementation
1st case : use a linked list
•
insert at the front of the list :
O
(1)
•
deleteMin requires traversing the list :
O
(
N
)
Malek Mouhoub, CS340 Winter 2007
3
3.2 Implementation
2nd case : use a sorted linked list
•
insertion requires
O
(
N
)
•
but only
O
(1)
for deleteMin.
Malek Mouhoub, CS340 Winter 2007
4
3.2 Implementation
3rd case : use a binary search tree
•
O
(log
N
)
average running time for both insertion and deletion.
•
Elements are deleted at each time from the left subtree which
affects the balance of the tree.
•
The binary search tree supports many operations that are not
required by priority queue.
Malek Mouhoub, CS340 Winter 2007
5
3.3 Binary Heap
3.3 Binary Heap
•
A binary heap is a complete binary tree : a tree that is completely Flled,
with possible exception of the bottom level, which is Flled from left to
right.
•
Can easily be represented by an array (and an integer representing the
current heap size) instead of a linked list.
•
±or any element in array position
i
:
–
the left child is in position
2
i
,
–
the right child is in position
2
i
+ 1
,
–
and the parent is in position
[
i/
2]
.
•
The operations required to traverse the tree are very simple and are
performed quickly (because of the heaporder property).
Malek Mouhoub, CS340 Winter 2007
6
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document 3.3 Binary Heap
A
B
C
D
E
H
I
J
F
G
A
B
C
D
E
F
G
H
I
J
0
1
2
3
4
5
6
7
8
9
10
11
12
13
Malek Mouhoub, CS340 Winter 2007
7
3.3 Binary Heap
HeapOrder Property
•
The smallest element should be at the root.
•
Any node should be smaller than all of its descendants (any
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 11/15/2010 for the course CS 340 taught by Professor Dr.malek during the Fall '10 term at University of Regina.
 Fall '10
 Dr.Malek

Click to edit the document details