-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffman.java
More file actions
180 lines (143 loc) · 4.94 KB
/
Huffman.java
File metadata and controls
180 lines (143 loc) · 4.94 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
package cs2321;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import cs2321.HeapPQ.PQEntry;
import cs2321.LinkedBinaryTree.Node;
import net.datastructures.*;
/*
* The format of the compressed file includes 3 continuously parts:
* 1. prefix tree in bit stream
* 2. length of the original file using 4 bytes
* 3. data coded with Huffman coding.
*
* Encoding prefix tree bit stream:
* if the node is external, output 0, followed by the letter
* if the node is internal, output 1, followed by
* the bit stream of left subtree, then the bit stream of right subtree.
*/
public class Huffman {
/**
*
* Compress file using Huffman code.
*
* @param inputFile The original data file
* @param outputFile The compressed data file that should be generated.
* @return the length of the data encoded with Huffman Code, don't include data for the prefix tree and length of the original file.
*/
public int compress(String inputFile, String outputFile) {
HeapPQ<Integer, LinkedBinaryTree<Character>> heap = new HeapPQ<Integer, LinkedBinaryTree<Character>>();
FileReader reader = null;
File f = new File(inputFile);
//TODO: implement the compress
try {
reader = new FileReader(f); //create a new file reader
} catch (FileNotFoundException e) {
e.printStackTrace();
}
char[] ch = new char[(int) f.length()];
try {
reader.read(ch); //read characters into the array
} catch (IOException e) {
e.printStackTrace();
}
//find frequency counts
int[] freq = new int[256];
for(char character: ch) {
freq[character]++;
}
//build the tree
for(int i = 0; i < 256; i++) {
if(freq[i] > 0) {
LinkedBinaryTree<Character> tempTree = new LinkedBinaryTree<Character>();
tempTree.addRoot((char)i);
heap.insert(freq[i], tempTree);
}
}
while(heap.size() > 1) {
Entry<Integer, LinkedBinaryTree<Character>> tempEntry1 = heap.removeMin();
Entry<Integer, LinkedBinaryTree<Character>> tempEntry2 = heap.removeMin();
int freqSum = tempEntry1.getKey() + tempEntry2.getKey();
LinkedBinaryTree<Character> tempTree = new LinkedBinaryTree<Character>();
tempTree.addRoot(null);
tempTree.addLeft(tempTree.root(), tempEntry1.getValue().root.getElement());
tempTree.addRight(tempTree.root(), tempEntry2.getValue().root.getElement());
heap.insert(freqSum,tempTree);
}
//build the code table
String[] codeTable = new String[256];
LinkedBinaryTree<Character> t = heap.removeMin().getValue();
buildCode(codeTable, t, t.root(), "");
//write the tree
DataOutputStream writer = null;
try {
writer = new DataOutputStream(new FileOutputStream(outputFile));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
writePrefixTree(t, t.root, writer);
return 0;
}
void buildCode(String[] codeTable, LinkedBinaryTree<Character> v, Position<Character> p, String code) {
if(v.isExternal(p)) {
codeTable[p.getElement()] = code;
}
else {
buildCode(codeTable,v, v.left(p), code + '0');
buildCode(codeTable, v, v.right(p), code + '1');
}
}
void writePrefixTree(LinkedBinaryTree<Character> v, Position<Character> p, DataOutputStream writer) {
if(v.isExternal(p)) {
try {
writer.write(1);
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.write(p.getElement());
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
else {
try {
writer.write(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writePrefixTree(v, v.left(p), writer);
writePrefixTree(v, v.right(p), writer);
}
}
/**
* Decode the compressed data file back to the original data file.
*
* @param inputFile : the compressed file
* @param outputFile : the file that should be generated by the decode function using ascii code.
*/
public void decode(String inputFile, String outputFile) {
//TODO: implement the decode
return;
}
public static void main(String [ ] args) {
Huffman huffman = new Huffman();
int length;
// db.txt has only two letters "ab". The length with Huffman coding should be 2.
length = huffman.compress("ab.txt", "ab.txt.huffman");
System.out.println("length is " + length);
// decode your newly created compress file. The generated file "ab.txt.decoded" should have same content as "ab.txt"
huffman.decode("ab.txt.huffman", "ab.txt.decoded");
// decode the previous correctly compressed file by instructor. The generated file "ab.txt.decoded" should have content as "ab.txt"
huffman.decode("ab.txt.compressed", "ab.txt.decoded");
// You may perform the above same testing for other files, like abra.txt, gogo.txt, tinytinyTable.txt
}
}