-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffmanTree.java
38 lines (29 loc) · 875 Bytes
/
HuffmanTree.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
package huffmanish;
import java.io.Serializable;
import java.util.BitSet;
import java.util.Map;
public class HuffmanTree implements Serializable {
private HuffmanNode root;
public HuffmanTree(HuffmanNode<Character> root) {
this.root = root;
}
public Map<Character, String> getCodes() {
return this.root.getCodes();
}
protected StringBuilder decode(BitSet bitSet) {
StringBuilder decoded = new StringBuilder();
HuffmanNode current = root;
for (int i = 0; i < bitSet.length(); i++) {
if (current.isLeaf()) {
decoded.append(current.value);
current = root;
}
if (bitSet.get(i)) {
current = current.right;
} else {
current = current.left;
}
}
return decoded;
}
}