Huffmancode
Huffmancode
Radu Trı̂mbiţaş
November 11, 2012
Algorithm 1 Huffman(C)
1: n := |C|;
2: Q := C;
3: for i := 1 to n − 1 do
4: allocate a new node z
5: z.lef t := x := Extract-Min(Q);
6: z.right := y := Extract-Min(Q);
7: z.f req := x.f req + y.f req;
8: Insert(Q, z);
9: end for
10: return Extract-Min(Q); {return the root of the tree}
a b c d e f
Frequency (in thousands) 45 13 12 16 9 5
Fixed-length codeword 000 001 010 011 100 101
Variable-length codeword 0 101 100 111 1101 1100
1
Figure 1: The steps of Huffmans algorithm for the frequencies given in Table
1. Each part shows the contents of the queue sorted into increasing order by
frequency. At each step, the two trees with lowest frequencies are merged.
Leaves are shown as rectangles containing a character and its frequency. Internal
nodes are shown as circles containing the sum of the frequencies of their children.
An edge connecting an internal node with its children is labeled 0 if it is an edge
to a left child and 1 if it is an edge to a right child. The codeword for a letter
is the sequence of labels on the edges connecting the root to the leaf for that
letter. (a) The initial set of n = 6 nodes, one for each letter. (b)(e) Intermediate
stages. (f) The final tree.
2
Heap procedure discussed in [1, Section 6.3]. The for loop in lines 3–8 executes
exactly n − 1 times, and since each heap operation requires time O(log n), the
loop contributes O(n log n) to the running time. Thus, the total running time
of Huffman on a set of n characters is O(n log n). We can reduce the running
time to O(n log log n) by replacing the binary min-heap with a van Emde Boas
tree (see [1, Chapter 20]).
References
[1] Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein,
Introduction to Algorithms, Third Edition, MIT Press, 2009