-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHuffmanCompressor.java
94 lines (83 loc) · 2.52 KB
/
HuffmanCompressor.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
import java.util.PriorityQueue;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Comparator;
/**
* @author Alex Krentsel
* Main compression class.
*/
public class HuffmanCompressor {
public static String compressString(String s) {
List<HuffmanTree> nodes = generateNodes(s);
HuffmanTree huffmanTree = generateTree(nodes);
HashMap<Character, String> hashMap = generateHashMap(huffmanTree);
String compressedString = generateCompressedString(s, hashMap);
return compressedString;
}
private static List<HuffmanTree> generateNodes(String s) {
List<HuffmanTree> nodes = new ArrayList<>();
for(int i = 0; i < s.length(); i = i + 1) {
HuffmanTree newTree = new HuffmanTree(s.charAt(i), 1);
int index = -1;
for(int k = 0; k < nodes.size(); k++) {
if(nodes.get(k).c == newTree.c) {
index = k;
break;
}
}
if(index != -1) {
nodes.get(index).val += 1;
} else {
nodes.add(newTree);
}
}
return nodes;
}
private static HuffmanTree generateTree(List<HuffmanTree> nodes) {
PriorityQueue<HuffmanTree> priorityQueue = new PriorityQueue<>(nodes.size(), new Comparator<HuffmanTree>() {
@Override
public int compare(HuffmanTree h1, HuffmanTree h2) {
return h1.val - h2.val;
}
});
for(HuffmanTree node : nodes) {
priorityQueue.add(node);
}
while(priorityQueue.size() > 1) {
HuffmanTree a = priorityQueue.poll();
HuffmanTree b = priorityQueue.poll();
HuffmanTree combinedNode = new HuffmanTree('_', a.val + b.val, a, b);
priorityQueue.add(combinedNode);
}
return priorityQueue.poll();
}
private static HashMap<Character, String> generateHashMap(HuffmanTree t) {
HashMap<Character, String> hashMap = new HashMap<>();
generateHashMapHelper(t, "", hashMap);
return hashMap;
}
/**
* Recursively navigate our HuffmanTree to build up a HashMap of Character, String pairs.
**/
private static void generateHashMapHelper(HuffmanTree t, String path, HashMap<Character, String> hashMap) {
if(t.isLeaf()) {
hashMap.put(t.c, path);
} else {
if (t.leftBranch != null) {
generateHashMapHelper(t.leftBranch, path + "0", hashMap);
}
if (t.rightBranch != null) {
generateHashMapHelper(t.rightBranch, path + "1", hashMap);
}
}
}
private static String generateCompressedString(String input, HashMap<Character, String> hashMap) {
String output = "";
for (int i = 0; i < input.length(); i = i + 1) {
output = output + hashMap.get(input.charAt(i));
}
return output;
}
}