-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathHuffman.java
More file actions
183 lines (131 loc) · 3.5 KB
/
Huffman.java
File metadata and controls
183 lines (131 loc) · 3.5 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
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;
}
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);
}
}
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();
}
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();
}
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");
}
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();
}
}
public void printMap() {
for (Character entry : new_hash_map.keySet()) {
System.out.print(entry + " ");
System.out.println(new_hash_map.get(entry));
}
}
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();
}
}