-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHoffmanEncoding.java
More file actions
101 lines (80 loc) · 2.73 KB
/
HoffmanEncoding.java
File metadata and controls
101 lines (80 loc) · 2.73 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
import java.util.*;
public class HoffmanEncoding {
HashMap<Character ,String > encoder ;
HashMap<String ,Character > decoder ;
private class Node implements Comparable<Node> {
Character data ;
int cost ;
Node left ;
Node right ;
Node (Character data , int cost ){
this.data = data ;
this.cost = cost ;
left = null ;
right = null ;
}
@Override
public int compareTo(Node node) {
return this.cost - node.cost ;
}
}
public HoffmanEncoding (String s) throws Exception{
HashMap <Character ,Integer > freqTable = new HashMap<>() ;
for (int i =0 ;i < s.length() ; i++){
char c = s.charAt(i);
if (freqTable.containsKey(c)){
int val = freqTable.get(c) + 1 ;
freqTable.put(c, val) ;
}
else {
freqTable.put(c, 1) ;
}
}
Heap <Node> heap = new Heap<>() ;
Set<Map.Entry<Character, Integer > > entrySet = freqTable.entrySet();
for (Map.Entry<Character ,Integer> entry : entrySet) {
Node node = new Node(entry.getKey(), entry.getValue()) ;
heap.insert(node);
}
while (heap.size() != 1){
Node first = heap.remove() ;
Node second = heap.remove() ;
Node newNode = new Node('\0', first.cost + second.cost);
newNode.left = first ;
newNode.right = second ;
heap.insert(newNode);
}
Node head = heap.remove() ;
this.encoder = new HashMap<>() ;
this.decoder = new HashMap<>() ;
initEncoderDecoder("", head);
}
private void initEncoderDecoder (String s , Node node){
if (node == null) return ;
if (node.left == null && node.right == null) {
this.encoder.put(node.data, s );
this.decoder.put(s, node.data) ;
}
initEncoderDecoder( s + "0", node.left);
initEncoderDecoder(s+"1", node.right);
}
public String encode (String sourse){
String ans = "" ;
for (int i = 0; i < sourse.length(); i++) {
ans = ans + encoder.get(sourse.charAt(i));
}
return ans ;
}
public String decode (String code){
String ans = "" ;
String key = "" ;
for (int i = 0; i < code.length(); i++) {
key = key + code.charAt(i) ;
if (decoder.containsKey(key)){
ans = ans + decoder.get(key);
key = "";
}
}
return ans ;
}
}