-
Notifications
You must be signed in to change notification settings - Fork 4
/
Huffman.java
216 lines (155 loc) · 4.03 KB
/
Huffman.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
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
/*
Huffman Code implementation
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Comparator;
import java.util.HashMap;
import java.util.PriorityQueue;
class HuffmanNode {
int data;
char c;
HuffmanNode left;
HuffmanNode right;
}
class MyComparator implements Comparator<HuffmanNode> {
public int compare(HuffmanNode x, HuffmanNode y) {
return x.data - y.data;
}
}
class HuffmanBuild {
private HashMap<Character, Integer> hash_map;
private HashMap<Character, String> new_hash_map;
private PriorityQueue<HuffmanNode> q;
private String filename;
public HuffmanBuild(String filename) {
hash_map = new HashMap<Character, Integer>();
new_hash_map = new HashMap<Character, String>();
this.filename = filename;
}
/*
This function adds the frequency of the chat
*/
private void makeFrequencyMap(Character c) {
if (hash_map.containsKey(c)) {
hash_map.put(c, hash_map.get(c) + 1);
} else {
hash_map.put(c, 1);
}
}
/*
This function generates PriorityQueue
*/
private void makePriorityQueue() {
int size = hash_map.size();
q = new PriorityQueue<HuffmanNode>(size, new MyComparator());
for (Character alphabet : hash_map.keySet()) {
HuffmanNode hn = new HuffmanNode();
hn.c = alphabet;
hn.data = hash_map.get(alphabet);
hn.left = null;
hn.right = null;
q.add(hn);
}
generateHuffmanTree();
}
/*
This function generates the huffman tree
*/
private void generateHuffmanTree() {
HuffmanNode root = null;
while (q.size() > 1) {
HuffmanNode x = q.peek();
q.poll();
HuffmanNode y = q.peek();
q.poll();
HuffmanNode f = new HuffmanNode();
f.data = x.data + y.data;
f.c = '-';
f.left = x;
f.right = y;
root = f;
q.add(f);
}
generateCharCode(root, "");
printMap();
printCode();
}
/*
This function generates the huffman Code for each character in puts it to the hashMap
*/
private void generateCharCode(HuffmanNode root, String s) {
if (root.left == null && root.right == null) {
System.out.println(root.c + ":" + s);
new_hash_map.put(root.c, s);
return;
}
generateCharCode(root.left, s + "0");
generateCharCode(root.right, s + "1");
}
/*
This function prints the Huffman code of the inputted file
*/
private void printCode() {
File file;
try {
file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
for(int i=0; i<line.length(); i++) {
char ch = line.charAt(i);
sb.append(new_hash_map.get(ch));
}
sb.append(new_hash_map.get('\n'));
}
String code = sb.toString();
System.out.println(" "+code);
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
/*
This function prints HashMap<Character, String> which has the final codes of each character of the file
*/
public void printMap() {
for (Character entry : new_hash_map.keySet()) {
System.out.print(entry + " ");
System.out.println(new_hash_map.get(entry));
}
}
/*
This function reads the input file and creates a HashMap<Character, Integer>
*/
public void readFile() {
File file;
try {
file = new File(filename);
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
for(int i=0; i<line.length(); i++) {
makeFrequencyMap(line.charAt(i));
}
makeFrequencyMap('\n');
}
makePriorityQueue();
} catch(FileNotFoundException ex) {
ex.printStackTrace();
} catch(IOException ex){
ex.printStackTrace();
}
}
}
class Huffman {
public static void main(String[] args) {
HuffmanBuild hf = new HuffmanBuild(args[0]);
hf.readFile();
}
}