-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdaptiveHuffman.java
More file actions
427 lines (374 loc) · 15.2 KB
/
AdaptiveHuffman.java
File metadata and controls
427 lines (374 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
import java.util.ArrayList;
import java.util.Collections;
public class AdaptiveHuffman {
/**
* A pointer that constantly points the NYT node
*/
private Node nytNode;
/**
* A pointer that constantly points the root node
*/
Node root;
/**
* the inputting code string that needs to incode or decode
*/
private char[] codeStr;
/**
* stores characters that exist before
*/
private ArrayList<Character> alreadyExist;
/**
* Store all the node in the tree from left to right, from bottom to top.
* Mainly used for find the biggest node in a block
*/
ArrayList<Node> nodeList;
/**
* A global variance used for generating code by tree
*/
private String tempCode = "";
/**
* Initialize input string, nyt node and root pointer.
* @param codeStr the input string
*/
public AdaptiveHuffman(char[] codeStr){
this.codeStr = codeStr;
alreadyExist = new ArrayList<Character>();
nodeList = new ArrayList<Node>();
//Initialize the nyt Node.
nytNode = new Node("NEW", 0);
nytNode.parent = null;
root = nytNode;
nodeList.add(nytNode);
}
/**
* The trunk method of encoding.
* @return all codes of symbols
*/
public ArrayList<String> encode(){
ArrayList<String> result = new ArrayList<String>();
result.add("0");//Represent NEW
char temp = 0;
for ( int i=0; i<codeStr.length; i++ ) {
temp = codeStr[i];
result.add(getCode(temp));
updateTree(temp);
}
return result;
}
/**
* The trunk method of decoding.
* @return code string aftering decoding.
*/
public String decode(){
String result = "";
String symbol = null;
char temp = 0;
Node p = root;
//The first symbol is of course NEW, so find it by ASCII
symbol = getByAsc(0);
result += symbol;
updateTree( symbol.charAt(0) );
p = root;
for ( int i=9; i<codeStr.length; i++ ) {
temp = codeStr[i];
if ( temp=='0' ){
p = p.left;
}
else
p = p.right;
symbol = visit(p);
//If reach a leaf
if ( symbol!=null ){
if ( symbol=="NEW" ){
symbol = getByAsc(i);
i+=8;
}
result+=symbol;
updateTree( symbol.charAt(0) );
p = root;
}
}
return result;
}
/**
* It's a very important method that is used for updating the structure of tree after read
* each symbol. Called both during encoding and decoding.
* @param c the next character
*/
private void updateTree(char c){
/*
* If the character is not yet existed, create two nodes. The one is for the new character,
* the other is for its father node.
*/
Node toBeAdd = null;
if ( !isAlreadyExist(c) ){
Node innerNode = new Node(null, 1);//inner node with null letter
Node newNode = new Node(String.valueOf(c), 1);//stores symbol
//Pay attention to the linking process among nodes.
innerNode.left = nytNode;
innerNode.right = newNode;
innerNode.parent = nytNode.parent;
if ( nytNode.parent!=null )//In the first time the nyt node is root.
nytNode.parent.left = innerNode;
else {
root = innerNode;
}
nytNode.parent = innerNode;
newNode.parent = innerNode;
//The following two lines assure the right order in nodeList
nodeList.add(1, innerNode);
nodeList.add(1, newNode);
alreadyExist.add(c);
toBeAdd = innerNode.parent;
} else {
toBeAdd = findNode(c);
}
//Loop until all parent nodes are incremented.
while ( toBeAdd!=null ) {
Node bigNode = findBigNode(toBeAdd.frequency);
/**
* The nodes should not be swapped in the following two situations:
* 1.The biggest node is itself.
* 2.The one node is the other's parent node.
*/
if ( toBeAdd!=bigNode && toBeAdd.parent!=bigNode && bigNode.parent!=toBeAdd)
swapNode(toBeAdd, bigNode );
toBeAdd.frequency++;
toBeAdd = toBeAdd.parent;
}
}
private boolean isAlreadyExist(char temp) {
// TODO Auto-generated method stub
for ( int i=0; i<alreadyExist.size(); i++ ) {
if ( temp==alreadyExist.get(i) )
return true;
}
return false;
}
//Get the symbol using the next 8 bit as a ASCII code.
private String getByAsc(int index) {
// TODO Auto-generated method stub
int asc = 0;
int tempInt = 0;
for ( int i=7; i>=0; i-- ) {
tempInt = codeStr[++index] - 48;
asc += tempInt * Math.pow(2, i);
}
char ret = (char) asc;
return String.valueOf(ret);
}
/**
* Visit a node in the tree
* @param p the pointer to the node
* @return letter if it's a leaf, otherwise null.
*/
private String visit(Node p) {
// TODO Auto-generated method stub
if ( p.letter!=null ){
//The symbol has been found.
return p.letter;
}
return null;
}
/**
* Called when encoding. If the symbol is already existed, look for it in tree. Or else,
* return the ASCII code of the character with a prefix of code of NEW.
* @param c the input character
* @return a code according to the input character
*/
private String getCode(char c){
tempCode = "";
getCodeByTree(root, String.valueOf(c), "");
String result = tempCode;
if ( result=="" ) {
getCodeByTree(root, "NEW", "");
result = tempCode;
result += toBinary( getAscii(c) );
}
return result;
}
//Find the existing node in the tree
private Node findNode(char c) {
// TODO Auto-generated method stub
String temp = String.valueOf(c);
Node tempNode = null;
for ( int i=0; i<nodeList.size(); i++ ) {
tempNode = nodeList.get(i);
if ( tempNode.letter!=null && tempNode.letter.equals(temp) )
return tempNode;
}
return null;
}
/**
* Swap two nodes. Note that we should swap nodes but not only values, because the subtree
* is also needed to be swapped.
* @param n1 the node of which the frequency is to be incremented.
* @param n2 the biggest node in the block.
*/
private void swapNode(Node n1, Node n2) {
// TODO Auto-generated method stub
//note that n1<n2
//Swap the position in the list firstly
int i1 = nodeList.indexOf(n1);
int i2 = nodeList.indexOf(n2);
nodeList.remove(n1);
nodeList.remove(n2);
nodeList.add( i1, n2);
nodeList.add( i2, n1);
//Swap the position in the tree then
Node p1 = n1.parent;
Node p2 = n2.parent;
//If the two nodes have different parent node.
if ( p1!=p2 ) {
if ( p1.left==n1 ) {
p1.left = n2;
} else {
p1.right = n2;
}
if ( p2.left==n2 ) {
p2.left = n1;
} else {
p2.right = n1;
}
} else {
p1.left = n2;
p1.right = n1;
}
n1.parent = p2;
n2.parent = p1;
}
/**
* Find the node with biggest index in a certain block. Just look for the first node with the
* same frequency from the back.
* @param frequency
* @return the found node
*/
private Node findBigNode(int frequency) {
// TODO Auto-generated method stub
Node temp = null;
for ( int i=nodeList.size()-1; i>=0; i--) {
temp = nodeList.get(i);
if ( temp.frequency==frequency )
break;
}
return temp;
}
/**
* A recursion function that is used to generate a huffman code of a given symbol.
* @param node the beginning node
* @param letter of which the code to be found
* @param code the generated code
*/
private void getCodeByTree(Node node, String letter, String code) {
// TODO Auto-generated method stub
//Reach a leaf
if ( node.left==null && node.right==null ) {
if ( node.letter!=null && node.letter.equals(letter) )
tempCode = code;
} else {
if ( node.left!=null ) {
getCodeByTree(node.left, letter, code + "0");
}
if ( node.right!=null ) {
getCodeByTree(node.right, letter, code + "1");
}
}
}
/**
* Return the ASCII code of the character c. Just for readability.
*/
public static int getAscii(char c){
return (int)c;
}
/**
* Convert a decimal integer to a 8-bit binary code.
* @param decimal a integer to be converted
* @return string with 0,1
*/
public static String toBinary(int decimal){
String result = "";
for ( int i=0; i<8; i++ ) {
if ( decimal%2==0 )
result = "0" + result;
else
result = "1" + result;
decimal /= 2;
}
return result;
}
public static void displayList(ArrayList<String> l){
for ( int i=0; i<l.size(); i++ ) {
System.out.println( l.get(i) );
}
}
private static String catStr(ArrayList<String> l) {
// TODO Auto-generated method stub
String result = "";
for ( String s: l ){
result += s;
}
return result;
}
/**
* The following three methods are for arithmetic coding.
*/
//Get statistics by the final tree.
private void getStatistics() {
// TODO Auto-generated method stub
ArrayList<Symbol> symbolList = new ArrayList<Symbol>();
preOrder(root, symbolList);
// System.out.println("Symbol size is: " + symbolList.size());
Collections.sort(symbolList);
calRange(symbolList);
Symbol2F.writeSymbolToFile("data/symboltable.txt", symbolList);
}
/**
* Traverse all the node in the tree using preorder.
* @param node the beginning node
* @param symbolList a symbol container
*/
public static void preOrder(Node node, ArrayList<Symbol> symbolList){
if( node!=null ){
if ( node.letter!=null && (!node.letter.equals("NEW")) ) {
Symbol tempSymbol = new Symbol(node.letter, node.frequency);
symbolList.add( tempSymbol );
}
preOrder(node.left, symbolList);
preOrder(node.right, symbolList);
}
}
public void calRange(ArrayList<Symbol> symbolList) {
// TODO Auto-generated method stub
int total = codeStr.length;
double low = 0;
for ( Symbol tempSymbol: symbolList ){
tempSymbol.probability = tempSymbol.frequency / (double)total;
tempSymbol.low = low;
tempSymbol.high = low + tempSymbol.probability;
low += tempSymbol.probability;
}
System.out.println("low="+low);//It should be 1.
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/**
* Using when encoding.
*/
// String text = ReadFile.readFile("data/I have a dream.txt", true);
// AdaptiveHuffman ah = new AdaptiveHuffman( text.toCharArray() );
// ArrayList<String> code = ah.encode();
// WriteFile.writeFile("data/ihaveadreaminHuff.txt", catStr(code), true);
// ah.getStatistics();
// Diagnostics.calCompRate(text, code);
/**
* Using when decoding.
*/
String code = ReadFile.readFile("data/ihaveadreaminHuff.txt", false);
AdaptiveHuffman ah = new AdaptiveHuffman( code.toCharArray() );
String result = ah.decode();
WriteFile.writeFile("data/IhaveadreamFromHuff.txt", result, false);
}
}