-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanConverter.java
More file actions
183 lines (166 loc) · 6.23 KB
/
HuffmanConverter.java
File metadata and controls
183 lines (166 loc) · 6.23 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
package Huffman_Encoder;
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
//constructor
public class HuffmanConverter{
// ASCII number of characters
public static final int NUMBER_OF_CHARACTERS = 256;
private String contents;
private HuffmanTree huffmanTree;
private int count[];
private String code[];
// Construct using an input string.
// Initialize count and code.
public HuffmanConverter(String input) {
this.contents = input;
this.count = new int[NUMBER_OF_CHARACTERS];
this.code = new String[NUMBER_OF_CHARACTERS];
}
// Record how often each character occurs and store in count.
public void recordFrequencies() {
for(char c: contents.toCharArray()) {
count[(int)c]++;
}
int distinct = 0;
for (int f : count) {
if (f > 0) {
distinct++;
}
}
HuffmanNode[] nodes = new HuffmanNode[distinct];
int idx = 0;
for (int i = 0; i < count.length; i++) {
if(count[i]>0) {
String letter;
if(i == '\n') {
letter = "\\n";
}else {
letter = Character.toString((char)i);
}
HuffmanNode node = new HuffmanNode(letter, (double)count[i]);
nodes[idx++] = new HuffmanNode(letter, (double)count[i]);
}
}
BinaryHeap<HuffmanNode> heap = new BinaryHeap<>(nodes);
heap.printHeap();
}
// Construct a Huffman tree from count and
// store the tree in huffmanTree.
public void frequenciesToTree() {
int distinct = 0;
for (int f : count) {
if (f > 0) {
distinct++;
}
}
HuffmanNode[] nodes = new HuffmanNode[distinct];
int idx = 0;
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) {
String letter = Character.toString((char)i);
nodes[idx++] = new HuffmanNode(letter, (double)count[i]);
}
}
BinaryHeap<HuffmanNode> heap = new BinaryHeap<>(nodes);
this.huffmanTree = HuffmanTree.createFromHeap(heap);
}
// Construct code from huffmanTree.
public void treeToCode() {
for (int i = 0; i < code.length; i++) {
code[i] = "";
}
treeToCode(huffmanTree.root, "");
printFormattedLegend(huffmanTree.root, "");
}
private void treeToCode(HuffmanNode t, String encoding) {
if (t == null) {
return;
}
if (t.letter.length() > 1) {
treeToCode(t.left, encoding + "0");
treeToCode(t.right, encoding + "1");
}else {
char c = t.letter.charAt(0);
code[(int)c] = encoding;
}
}
//The original Part 1 printLegend method could not clearly handle quoting characters
// Therefore, I add this new method.
// printFormattedLegend in HuffmanConverter to customize the output format as required.
private void printFormattedLegend(HuffmanNode node, String encoding) {
if (node == null) return;
if (node.left == null && node.right == null) {
char c = node.letter.charAt(0);
String display;
if (c == '\n') {
display = "'\\n'";
} else {
display = "'" + c + "'";
}
System.out.println(display + "=" + encoding);
} else {
printFormattedLegend(node.left, encoding + "0");
printFormattedLegend(node.right, encoding + "1");
}
}
// Encode content using code.
public String encodeMessage() {
StringBuilder sb = new StringBuilder();
for (char c : contents.toCharArray()) {
sb.append(code[(int)c]);
}
String encoded = sb.toString();
return encoded;
}
// Decode a Huffman encoding.
public String decodeMessage(String encodedStr) {
StringBuilder sb = new StringBuilder();
HuffmanNode node = huffmanTree.root;
for (char bit : encodedStr.toCharArray()) {
if (bit == '0') {
node = node.left;
} else {
node = node.right;
}
if (node.left == null && node.right == null) {
sb.append(node.letter);
node = huffmanTree.root;
}
}
return sb.toString();
}
// Read an input file.
public static String readContents(String filename) {
String temp = "";
try {
File file = new File(filename);
Scanner sc = new Scanner(file);
while (sc.hasNextLine()) {
temp += sc.nextLine();
temp += "\n";
}
sc.close();
return temp;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return "";
}
public static void main(String args[]) {
String input = HuffmanConverter.readContents(args[0]);
HuffmanConverter h = new HuffmanConverter(input);
h.recordFrequencies();
// Print a list of characters and frequencies here!
h.frequenciesToTree();
h.treeToCode();
// Print the Huffman encoding here!
String encoded = h.encodeMessage();
System.out.println();
System.out.println("Huffman Encoding:");
System.out.println(encoded+"\n");
System.out.println("Message size in ASCII encoding: "+h.contents.length()*8);
System.out.println("Message size in Huffman coding: "+encoded.length()+"\n");
System.out.println(h.decodeMessage(encoded));
}
}