Java Reference
In-Depth Information
figure 12.12
Huffman's algorithm
after the final merge
58
T 6
T 4
T 5
i
sp
e
T 3
a
T 2
t
T 1
s
nl
12.1.3 implementation
We now provide an implementation of the Huffman coding algorithm, with-
out attempting to perform any significant optimizations; we simply want a
working program that illustrates the basic algorithmic issues. After discussing
the implementation we comment on possible enhancements. Although signifi-
cant error checking needs to be added to the program, we have not done so
because we did not want to obscure the basic ideas.
Figure 12.13 illustrates some of the I/O classes and constants to be used.
We maintain a priority queue of tree nodes (recall that we are to select two
trees of lowest weight).
1 import java.io.IOException;
2 import java.io.InputStream;
3 import java.io.OutputStream;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.DataInputStream;
7 import java.io.DataOutputStream;
8 import java.io.BufferedInputStream;
9 import java.io.BufferedOutputStream;
10 import java.util.PriorityQueue;
11
12 interface BitUtils
13 {
14 public static final int BITS_PER_BYTES = 8;
15 public static final int DIFF_BYTES = 256;
16 public static final int EOF = 256;
17 }
figure 12.13
The import directives
and some constants
used in the main
compression program
algorithms
 
Search WWH ::




Custom Search