Java Reference
In-Depth Information
1 public HuffmanTree( )
2 {
3 theCounts = new CharCounter( );
4 root = null;
5 }
6
7 public HuffmanTree( CharCounter cc )
8 {
9 theCounts = cc;
10 root = null;
11 createTree( );
12 }
13
14 /**
15 * Return the code corresponding to character ch.
16 * (The parameter is an int to accommodate EOF).
17 * If code is not found, return an array of length 0.
18 */
19 public int [ ] getCode( int ch )
20 {
21 HuffNode current = theNodes[ ch ];
22 if( current == null )
23 return null;
24
25 String v = "";
26 HuffNode par = current.parent;
27
28 while ( par != null )
29 {
30 if( par.left == current )
31 v = "0" + v;
32 else
33 v = "1" + v;
34 current = current.parent;
35 par = current.parent;
36 }
37
38 int [ ] result = new int[ v.length( ) ];
39 for( int i = 0; i < result.length; i++ )
40 result[ i ] = v.charAt( i ) == '0' ? 0 : 1;
41
42 return result;
43 }
figure 12.19
Some of the Huffman
tree methods,
including constructors
and the routine for
returning a code for a
given character
The getChar method shown in Figure 12.20 is simpler: We start at the root
and branch left or right, as directed by the code. Reaching null prematurely
Search WWH ::




Custom Search