Title: Lecture 21 Heaps and Priority Queues
1Lecture 21 Heaps and Priority Queues
- May 8, 2008
- Prof. Kyu Ho Park
2Heaps
? A heap is a binary tree with the following
properties ? It is complete. ? It is
implemented as an array. ? Each node in a heap
satisfies the heap condition , which states that
every nodes key is larger than ( ,or equal
to ) the keys of its children.
88
72
55
70
44
30
50
50
66
22
33
25
3Complete Binary Tree(CBT)
?Bounds on the size of a CBT ?Height of a
CBT ?Examples of CBT
? There is a natural mapping of each complete
tree into an array of the same size.
4Properties of Heaps
- ? Binary search trees are ordered from left to
right. - ? Heaps are binary trees that are ordered from
bottom to top, so that a traversal along any
leaf-to-root path will visit the keys - in ascending order max heaps.
- in descending order min heaps.
- ? Heaps are (1) to implement priority queues,
- (2) to implement the Heap sort
algorithm. - ? The parent of node number i is numbered
(i-1)/2 and its two children are numbered ( 2i1
) and ( 2i 2 ). - ? A heap is a complete binary tree in which the
keys along leaf-to-root path are ascending. It
makes a heap partially ordered by the relation
on its keys.
5Heapify Algorithms
- Input a node x in a complete binary tree.
- Precondition the two subtrees of x are heaps.
- Postcondition the subtree rooted x is a heap.
- 1. Let temp x.
- 2. While x is not a leaf, do steps 3-4.
- 3. Let y be the larger child of x.
- 4. If y.key gt x.key, do steps 5-6.
- 5. Copy y into x.
- 6. Set x y.
- 7. Copy temp into x.
6Heapify Operation
7Build_Heap Algorithm
- Input a complete binary tree.
- Postcondition T is a heap.
-
- 1. If T is a singleton, return.
- 2. Apply Build_Heap to the left subtree.
- 3. Apply Build_Heap to the right subtree.
- 4. Apply Heapify to the root.
-
8Heapify Operations
9Priority Queues
- ? A priority queue is a queue whose remove
operation depends upon the priority ranking of
its elements. - ? Ordinary queue FIFO, a Priority queue
Best-In, First-Out(BIFO). - ? ADT Priority Queue
- Operations
- 1. Add Insert a given element into the queue.
- 2. Best If the queue is not empty, return the
element that has the highest priority. - 3. RemoveBest If the queue is not empty, delete
and return the element that has the highest
priority. - 4. Size Return the number of elements in the
queue.
10Priority Queues
- ? A prioity queue is a queue whose remove
operation depends upon the priority rankings of
its elements. It assumes that the elements can be
compared to determine which have higher priority. - ? A PriorityQueue Interface
- 1 public interface PriorityQueue
- 2 public void add(Object object)
- 3 // POSTCONDITION the given object is in this
queue - 5 public Object best()
- 6 // RETURN the highest priority element in
this queue - 7 // PRECONDITION this queue is not empty
- 9 public Object removeBest()
- 10 // RETURN the highest priority element in
this queue - 11 // PRECONDITION this queue is not empty
- 12 // POSTCONDITION the returned object is not
in this queue - 14 public int size()
- 15 // RETURN the number of elements in this
queue - 16
11Inserting into the priority queue
the Highest Priority Element
12A Huffman Code
ACCEDE
13Huffman Codes
- ? The Huffman tree is a priority queue in which
the priority of each leaf is the relative
frequency of the character that it represents,
and the priority of each internal node is the sum
of the priorities of its children. - ? Algorithm Generating a Huffman Code
- Input a sequence of characters.
- Output a bit code for the input characters.
- Postconditions the bit code has the unique
property and is optimal. - 1. Tally the frequencies of the input
characters. - 2. Load the letter-frequency pairs into a min
priority queue. - 3. Coalesce the pairs into a Huffman tree.
- 4. Encode the character at each leaf with the
bit sequence along its root-to-leaf path.
14Huffman Algorithm
15Coalescing a Huffman Tree
- ? Algorithm Coalescing a Huffman Tree
- Input a min heap Q of integers.
- Output a Huffman tree H of integers.
- Postconditions the elements of Q are the leaves
of H. - 1. Restructure Q by interpreting each element as
itself a singleton tree. - 2. Repeat steps 3-5 while Q has more than one
element. - 3. Remove the two highest-priority trees x and y
from Q. - 4. Form the Huffman tree z with children x and
y. - 5. Return the remaining element of Q.
16Huffman Tree Properties
- ? A Huffman tree is a binary tree of integers
with two properties - 1. Each internal node is the sum of its
children. - 2. Its weighted external path length is minimal.
17Huffman Heaps with Minimal Weighted External Path
Length
18223171
18133162
13
131629
221638
22
WEPL 2(71) 3(29) 229
WEPL 2(62) 3(38) 238
18Coalescing a Priority Forest of Huffman Trees
19Huffman Codes Another Example
- ? We want to sends a message SUSIE SAYS IT IS
EASY. - Frequency Table
-
20Huffman Code-I
- ? Growing the Huffman tree
- 1.
- 2.
- 3.
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
2
T(1)
Y(2)
E(2)
A(2)
I(3)
sp(4)
S(6)
U(1)
Lf(1)
3
2
T(1)
U(1)
Lf(1)
21Huffman Code-I
- ? Growing the Huffman tree
- 4.
- 5.
- 6.
-
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
3
S(6)
4
A(2)
I(3)
sp(4)
3
Y(2)
E(2)
5
I(3)
4
sp(4)
S(6)
3
A(2)
22Huffman Code-I
- ? Growing the Huffman tree
- 7. 9.
- 8. 10.
-
S(6)
sp(4)
5
7
9
13
I(3)
4
S(6)
7
22
S(6)
9
7
13
sp(4)
5
9
23Huffman Code-II
- ? Growing the Huffman tree
- 1.
- 2.Min Heap
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
Lf(1)
U(1)
T(1)
Y(2)
E(2)
A(2)
I(3)
sp(4)
S(6)
24Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
2
Lf(1)
Lf(1)
U(1)
U(1)
T(1)
Y(2)
E(2)
A(2)
I(3)
sp(4)
S(6)
25Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
2
T(1)
Lf(1)
U(1)
2
Y(2)
E(2)
A(2)
I(3)
sp(4)
3
S(6)
T(1)
2
26Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
2
Y(2)
Lf(1)
U(1)
E(2)
A(2)
4
3
I(3)
sp(4)
S(6)
3
E(2)
Y(2)
T(1)
2
27Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
5
2
A(2)
3
A(2)
Lf(1)
U(1)
3
I(3)
4
4
sp(4)
S(6)
3
E(2)
Y(2)
T(1)
2
28Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
5
2
A(2)
3
I(3)
Lf(1)
U(1)
4
sp(4)
4
5
S(6)
3
E(2)
Y(2)
7
T(1)
2
I(3)
4
29Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
5
2
A(2)
3
sp(4)
Lf(1)
U(1)
5
S(6)
4
7
3
E(2)
Y(2)
7
9
T(1)
2
I(3)
4
sp(4)
5
30Huffman Code-II
- ? Growing the Huffman tree
-
Lf(1)
T(1)
U(1)
Y(2)
E(2)
A(2)
I(3)
S(6)
sp(4)
5
2
S(6)
13
A(2)
3
7
Lf(1)
U(1)
S(6)
7
4
9
3
E(2)
Y(2)
7
9
T(1)
2
I(3)
4
sp(4)
5
31Huffman Tree
22
9
13
sp
5
7
S
4
A
3
I
2
Y
E
T
Lf
U
32Different Heaps constructed with the same elements
33Enqueuing
34Dequeuing
35Top-down Method
36Bottom-up Method