-
Notifications
You must be signed in to change notification settings - Fork 0
/
DecoderTree.java
67 lines (61 loc) · 1.71 KB
/
DecoderTree.java
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
package ADSHuffman;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class DecoderTree{
static DecoderTreeNode root=new DecoderTreeNode();
static String decodedFile="decoded.txt";
/*
* Creates Decode Tree by adding a node to left if 0 is encountered and node
* to right if 1 is encountered and at last bit adds data to the node
*/
public static void add_code_to_decode_tree(String data, String code) {
DecoderTreeNode temp = root;
int i = 0;
for (i = 0; i <= code.length() - 2; i++) {
if (code.charAt(i) == '0') {
if (temp.left == null) {
temp.left = new DecoderTreeNode();
temp = temp.left;
} else {
temp = temp.left;
}
} else {
if (code.charAt(i) == '1') {
if (temp.right == null) {
temp.right = new DecoderTreeNode();
temp = temp.right;
} else {
temp = temp.right;
}
}
}
}
if (code.charAt(i) == '0') {
temp.left = new DecoderTreeNode(data);
} else {
temp.right = new DecoderTreeNode(data);
}
}
/* Parse decoder tree to write value of each huffman code in the file */
public static void decode_code(String code) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(decodedFile));
DecoderTreeNode temp = root;
for (int i = 0; i < code.length(); i++) {
if (code.charAt(i) == '0') {
temp = temp.left;
if (temp.left == null && temp.right == null) {
bw.write(temp.getData() + "\n");
temp = root;
}
} else {
temp = temp.right;
if (temp.left == null && temp.right == null) {
bw.write(temp.getData() + "\n");
temp = root;
}
}
}
bw.close();
}
}