COP3530.01, Spring 2001
April 05, 2001
S. Lang
Solution Key to Test #2
1.
(a) (10 pts.) Construct the Huffman codes for 4 characters A, B, C, and D, assuming their
frequencies are 28, 30, 10, and 24, respectively.
A (28)  (58)  (92)
Huffman’s codes:
A – 00; B – 01; C – 10; D – 11.
(The merge tree is represented with the leaf nodes on the
B (30) 
left side of the figure, the left branch on top of the right
Branch for each internal node)
C (10)  (34) 
D (24) 
(b) (5 pts.) If we are constructing the Huffman codes for
n
characters given their frequencies,
give the time complexity of the algorithm with a
brief
(two lines) explanation.
It takes O(
n
) time to convert the array of
n
frequency values into a minheap.
Then we
repeatedly delete the next two smallest numbers from the heap, add then insert the sum into
the heap, representing an internal node.
This process continues until a single value is left.
The loop takes O(
n
) iterations; each iteration takes O(lg
n
) time, so the total time of the loop
is O(
n
lg
n
).
The codes can be generated by a tree traversal for O(
n
) time.
The algorithm’s
total time is O(
n
lg
n
).
2.
(15 pts.) Suppose a binary minheap is maintained in an integer array
H
[1..
n
].
If the value
H
[
k
] is modified to become
smaller
, 1
≤
k
≤
n
, write in C++ or Java (
circle which one you
choose
) a function that restores the heap property using the following function prototype:
void percolateUp (int
k
)
// assume
H
[1..
n
] and
n
“global”
(See Text for the answer.)
3.
Suppose we use a divideandconquer strategy to solve a problem that uses an array of size
n
as input.
(i)
Use
bn
amount of time to divide the problem into two approximately equal
sized subproblems.
(ii)
Solve the two subproblems separately using recursion; the recursion
terminates when
n
= 1.
(iii)
Use
cn
2
amount of time to combine the two solutions of Part (ii) into a
solution to the original problem.
(a)
(8 pts.)
Give a recurrence for the time complexity
T
(
n
) of the algorithm.
No explanation
is needed.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
This is the end of the preview.
Sign up
to
access the rest of the document.
 Summer '09
 Computer Science, Insertion Sort, optimal solution

Click to edit the document details