-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanCode.java
200 lines (163 loc) · 4.53 KB
/
HuffmanCode.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.Scanner;
/**
*
*/
public class HuffmanCode {
PriorityQueue<Node> tree = new PriorityQueue<>((n1, n2) -> (n1.posb < n2.posb) ? -1 : 1);
TreeMap<String, String> hCodes = new TreeMap<>();
public static class Node {
String ch;
int posb;
Node left, right;
Node()
{
this.ch = "";
this.posb =0;
this.left = null;
this.right=null;
}
Node(String ch, int posb) {
this.ch = ch;
this.posb = posb;
this.left = null;
this.right = null;
}
Node(Node n1, Node n2)
{
this.posb = n1.posb + n2.posb;
ch = n1.ch + n2.ch;
if (n1.posb < n2.posb) {
this.right = n2;
this.left = n1;
} else {
this.right = n1;
this.left = n2;
}
}
boolean isLeaf() {
return (left == null) && (right == null);
}
}
public boolean isOutOfPossibilityBound()
{
boolean retVal = false;
int tPosb = 0;
Iterator<Node> iter = tree.iterator();
while (iter.hasNext()) {
Node current = iter.next();
tPosb = tPosb+current.posb;
}
if(tPosb>100)
retVal = true;
return retVal;
}
public void buildHuffmanTree()
{
while (tree.size() > 1)
{
Node n = new Node(tree.poll(), tree.poll());
tree.add(n);
}
}
public void createHuffmanCodes(Node n1, String code)
{
if(n1 == null)
return;
// At this point all leaf nodes should be our character
if(n1.isLeaf())
{
hCodes.put(n1.ch, code);
}
else {
if(n1.right != null)
{
code = code+"1";
createHuffmanCodes(n1.right, code);
}
if(n1.left != null)
{
code = code+"0";
createHuffmanCodes(n1.left, code);
}
}
}
public void display()
{
for(Entry<String, String> entry : hCodes.entrySet()) {
String ch = entry.getKey();
String code = entry.getValue();
System.out.println(ch + " => " + code);
}
}
public static void main(String[] args) {
int nOfC = -1;
boolean reachedTotalPossibility = false;
Scanner sc = new Scanner(System.in);
HuffmanCode hc = new HuffmanCode();
try {
System.out.print("Enter number of codes: ");
nOfC = sc.nextInt();
}
catch (Exception e)
{
System.out.print("Input Error!\n");
}
if(nOfC>0)
{
int tEntries = 0;
while(true)
{
System.out.print("Enter a character and possibility: ");
try {
String str = sc.next();
String cha = ""+str.charAt(0);
int possibility = sc.nextInt();
hc.tree.add(new HuffmanCode.Node(cha, possibility));
tEntries++;
if(hc.isOutOfPossibilityBound())
{
reachedTotalPossibility = true;
System.out.println("Total possibility can not be more than 100, qutting!");
break;
}
if(tEntries == nOfC)
break;
}
catch(Exception e)
{
System.out.print("Invalid input! \n");
}
}
if(!reachedTotalPossibility)
{
hc.buildHuffmanTree();
hc.createHuffmanCodes(hc.tree.peek(), "");
hc.display();
}
}
else
{
System.out.println("Number of codes can not be lessthan or equal to 0");
}
// HuffmanCode.Node n1 = new HuffmanCode.Node("a", 32);
// HuffmanCode.Node n2 = new HuffmanCode.Node("b", 21);
// HuffmanCode.Node n3 = new HuffmanCode.Node("c", 1);
// HuffmanCode.Node n4 = new HuffmanCode.Node("d", 9);
// HuffmanCode.Node n5 = new HuffmanCode.Node("e", 8);
//
// hc.tree.add(n1);
// hc.tree.add(n2);
// hc.tree.add(n3);
// hc.tree.add(n4);
// hc.tree.add(n5);
//
// hc.buildHuffmanTree();
// hc.createHuffmanCodes(hc.tree.peek(), "");
// hc.display();
return;
}
}