-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuffman.java
322 lines (242 loc) · 6.63 KB
/
huffman.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.event.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
public class huffman extends JFrame {
JLabel l1;
JTextField t1;
JButton b1,b2;
//Screen for taking the string input//
public huffman(){
super("GROUP-14 ADA ASSIGNMENT:HUFFMAN CODING");
setLayout(new FlowLayout());
setSize(1000,500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
build_component();
}
public void build_component(){
l1 = new JLabel("ENTER A STRING");
l1.setFont(new Font("Serif", Font.BOLD, 25));
t1 = new JTextField(50);
b1 = new JButton("ENTER");
b1.setSize(50,50);
b1.setLocation(95,45);
b2 = new JButton("EXIT");
b2.setSize(50,50);
b2.setLocation(105,45);
add(l1);
add(t1);
add(b1);
add(b2);
b1.addActionListener(new mylistener());
b2.addActionListener(new mylistener());
}
//Defining actions of the buttons when pressed//
private class mylistener implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==b2)
System.exit(0);
else{
String s = t1.getText();
Tree t = new Tree();
NewWindow NW= new NewWindow(t.buildTree(s));
}
}
}
public static void main(String args[]){
huffman h = new huffman();
}
}
// Node Class //
class Node implements Comparable<Node>{
public char ch;
public int freq;
public Node left,right;
public int count;
Node(char ch,int freq,Node left,Node right,int count){
this.ch=ch;
this.freq=freq;
this.left=left;
this.right=right;
this.count=count;
}
private boolean isLeaf() {
assert ((left == null) && (right == null)) || ((left != null) && (right != null));
return (left == null) && (right == null);
}
public int compareTo(Node a){
if(freq<a.freq)
return -1;
else if(freq>a.freq)
return 1;
else
return 0;
}
public void setcount(int c){
this.count=c;
}
}
//Tree Class//
class Tree extends JFrame{
//counting frequency of every token in the string//
public static int[] freqCount(String S){
int[] f=new int[10000];
for(int i=0;i<S.length();i++){
f[S.charAt(i)]++;
}
return f;
}
//building the huffman tree from a String S//
public static Node buildTree(String S){
PriorityQueue<Node>queue=new PriorityQueue<Node>();
int[]f=new int[10000];
f=freqCount(S);
for(char i=0;i<f.length;i++){
if(f[i]>0){
Node n=new Node(i,f[i],null,null,0);
queue.add(n);
}
}
int c=1;
while(queue.size()>1){
Node l1=queue.poll();
l1.setcount(c);
c++;
Node l2=queue.poll();
l2.setcount(c);
c++;
Node parent=new Node('\0',l1.freq+l2.freq,l1,l2,0);
queue.add(parent);
}
return queue.poll();
}
}
class NewWindow{
Node ab;
ArrayList<Node>node=new ArrayList<Node>();
public int width=700; // this may be changed accodingly to fit the tree in the screen//
public int height=10;
ArrayList<Integer>x = new ArrayList<Integer>(); //list of x coordinates of the nodes in the tree//
ArrayList<Integer>y = new ArrayList<Integer>(); //list of y coordinates of the nodes in the tree//
public int index(Node n){
int a=0;
for(int i=0;i<node.size();i++){
if(n==node.get(i))
{
a=i;
break;
}
}
return a;
}
//LevelFirstSearch traversal of the tree//
public void bfs(Node root){
Queue<Node> q= new LinkedList<Node>();
int c=1;
q.add(root);
while(!q.isEmpty()){
Node n=(Node)q.remove();
node.add(n);
if(n.left!=null)
q.add(n.left);
if(n.right!=null)
q.add(n.right);
}
for(int i=0;i<node.size();i++){
x.add(0);
y.add(0);
}
x.set(0,width);
y.set(0,height);
for(int j=0;j<node.size();j++){
if(node.get(j).left!=null && node.get(j).right!=null){
x.set(index(node.get(j).left),x.get(j)-25*c);
x.set(index(node.get(j).right),x.get(j)+25*c);
y.set(index(node.get(j).left),y.get(j)+100);
y.set(index(node.get(j).right),y.get(j)+100);
c++;
}
}
ab.setcount(node.size());
}
//CREATING THE NEW WINDOW FOR TREE ANIMATION //
public NewWindow(Node b) {
ab=b;
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("GROUP-14 ADA PROJECT:HUFFMAN TREE GENERATION ANIMATION ");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
//ADDING TestPane() object //
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
bfs(ab);
}
// TesPane Class ,animation done here //
public class TestPane extends JPanel {
private int b=node.size();
@Override
public Dimension getPreferredSize() {
return new Dimension(2800, 2800);
}
public TestPane(){
ActionListener animate = new ActionListener() {
public void actionPerformed(ActionEvent ae) {
repaint();
b--;
}
};
Timer timer = new Timer(100,animate);
timer.start();
setBackground(Color.PINK);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.RED);
for(int j=node.size()-1;j>=0;j--){
if(node.get(j).count<node.size()-b){
g2d.setColor(Color.YELLOW);
g2d.fillRect(x.get(j),y.get(j), 23, 23);
g2d.setColor(Color.RED);
g2d.drawRect(x.get(j), y.get(j), 23, 23);
g2d.setColor(Color.RED);
g2d.drawString(Integer.toString(node.get(j).freq),x.get(j)+5 , y.get(j)+13);
g2d.drawString(Character.toString(node.get(j).ch),x.get(j)+10 , y.get(j)+20);
g2d.setColor(Color.RED);
if((node.get(j)).left!=null){
g2d.drawLine(x.get(j)+20,y.get(j)+20,x.get(index(node.get(j).left)),y.get(index(node.get(j).left)));
g2d.drawString("1",(x.get(j)+x.get(index(node.get(j).left)))/2, (y.get(j)+y.get(index(node.get(j).left)))/2);
g2d.setColor(Color.RED);
}
if((node.get(j)).right!=null){
g2d.drawLine(x.get(j)+20,y.get(j)+20,x.get(index(node.get(j).right)),y.get(index(node.get(j).right)));
g2d.drawString("0",(x.get(j)+x.get(index(node.get(j).right)))/2, (y.get(j)+y.get(index(node.get(j).right)))/2);
g2d.setColor(Color.RED);
}
}
}
}
}
}