Java Reference
In-Depth Information
the character-counting class
Figure 12.16 provides the CharCounter class, which is used to obtain the char-
acter counts in an input stream (typically a file). Alternatively, the character
counts can be set manually and then obtained later. (Implicitly, we are treating
eight-bit byte s as ASCII characters for this program.)
the huffman tree class
The tree is maintained as a collection of nodes. Each node has links to its left
child, right child, and parent (in Chapter 18 we discuss the implementation of
trees in detail). The node declaration is shown in Figure 12.17.
The HuffmanTree class skeleton is provided in Figure 12.18. We can create
a HuffmanTree object by providing a CharCounter object, in which case the tree
is built immediately. Alternatively, it can be created without a CharCounter
object. In that case, the character counts are read by a subsequent call to
readEncodingTable , and at that point the tree is built.
1 // CharCounter class: A character counting class.
2 //
3 // CONSTRUCTION: with no parameters or an open InputStream.
4 //
5 // ******************PUBLIC OPERATIONS***********************
6 // int getCount( ch ) --> Return # occurrences of ch
7 // void setCount( ch, count ) --> Set # occurrences of ch
8 // ******************ERRORS**********************************
9 // No error checks.
10
11 class CharCounter
12 {
13 public CharCounter( )
14 { }
15
16 public CharCounter( InputStream input ) throws IOException
17 {
18 int ch;
19 while( ( ch = input.read( ) ) != -1 )
20 theCounts[ ch ]++;
21 }
22
23 public int getCount( int ch )
24 { return theCounts[ ch & 0xff ]; }
25
26 public void setCount( int ch, int count )
27 { theCounts[ ch & 0xff ] = count; }
28
29 private int [ ] theCounts = new int[ BitUtils.DIFF_BYTES ];
30 }
figure 12.16
The CharCounter class
 
Search WWH ::




Custom Search