-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanEncoder.java
55 lines (52 loc) · 1.64 KB
/
HuffmanEncoder.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
import java.util.HashMap;
import java.util.PriorityQueue;
import java.io.InputStream;
import java.util.Map;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
public class HuffmanEncoder {
private static HuffmanNode buildHuffmanTree(PriorityQueue<HuffmanNode> pq) {
System.out.println(pq.size());
while(pq.size()>1) {
HuffmanNode first = pq.poll();
HuffmanNode second = pq.poll();
HuffmanNode newNode = new HuffmanNode(Character.MIN_VALUE,first.getFrequency()+second.getFrequency(),first,second);
pq.add(newNode);
}
return pq.poll();
}
public static void findCodes(HuffmanNode root,String s,Map<Character,String> codeMap) {
if(root.getLeft() == null && root.getRight() == null) {
codeMap.put(root.getChar(),s);
return;
}
findCodes(root.getLeft(),s+"0",codeMap);
findCodes(root.getRight(),s+"1",codeMap);
}
public static HuffmanNode generateCodes(InputStream inputStream,Map<Character,String> codeMap) {
int b;
char c;
Map<Character,HuffmanNode> map = new HashMap<>();
HuffmanNode root;
try {
while((b = inputStream.read())!=-1) {
c = (char)b;
//System.out.print(c);
if(!map.containsKey(c)) {
map.put(c,new HuffmanNode(c,1,null,null));
} else {
map.get(c).incrFreq();
}
}
// add the dummy EOF character
map.put('\u0000',new HuffmanNode('\u0000',1,null,null));
root = buildHuffmanTree(new PriorityQueue<>(map.values()));
findCodes(root,"",codeMap);
}catch(IOException ioE) {
ioE.printStackTrace();
return null;
}
return root;
}
}