-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhuffman.java
More file actions
252 lines (160 loc) · 5.29 KB
/
Copy pathhuffman.java
File metadata and controls
252 lines (160 loc) · 5.29 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
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
package lab3;
/*
* The task is to complete the Huffman algorithm so that it takes in a sentence and outputs Huffman codes.
* */
import java.util.*;
public class huffman
{
public static void main(String[]args)
{
int[]ASCIIarray = new int[256];
Scanner sc = new Scanner(System.in);
String word = new String("");
System.out.println("Please enter a String");
word = sc.nextLine();
for(int i=0; i<word.length(); i++) //loops from 0 to the length of the String
{
int numWord = (int)word.charAt(i); //Sets numWord equal to the decimal value of the ASCII character
ASCIIarray[numWord]++; //Increments the slot of the corresponding ASCII character
int bin = Integer.parseInt(Integer.toBinaryString(numWord)); //Converts the decimal value of the ASCII character into binary
String binary = new String(""+bin); //converts binary number to String
if(binary.length()==6)
{
System.out.print("0" + binary + " "); //Prints the binary number to the screen
}
else
{
System.out.print(binary + " "); //Prints the binary number to the screen
}
}
System.out.println("");
System.out.println("");
System.out.println("");
PriorityQueue < Tree > PQ = new PriorityQueue < Tree >() ; //make a priority queue to hold the forest of trees
int ultcounter=0;
for(int j=0; j<ASCIIarray.length; j++)
{
if(ASCIIarray[j]!=0)
{
ultcounter++;
}
}
int position=0;
int bigg=0;
int count=0;
for(int l=0; l<word.length();l++)
{
bigg=0;
if(count<ultcounter)
{
for(int k = 0; k<ASCIIarray.length; k++)
{
if(ASCIIarray[k]!=0)
{
int currbig=ASCIIarray[k];
if(currbig>bigg)
{
bigg=currbig;
position=k;
}
}
}
System.out.println("'"+(char)position+"' appeared "+bigg+" times");
Tree myTree= new Tree();
myTree.frequency=ASCIIarray[position];
myTree.root= new Node();
myTree.root.letter = (char)position;
PQ.add(myTree);
ASCIIarray[position]=0;
count++;
}
position=0;
}
while(PQ.size()>1)
{
Tree firstTree = PQ.poll();
Tree secondTree = PQ.poll();
Tree comboTree = new Tree();
comboTree.frequency = firstTree.frequency + secondTree.frequency;
comboTree.root = new Node();
comboTree.root.leftChild = firstTree.root;
comboTree.root.rightChild = secondTree.root;
PQ.add(comboTree);
}
System.out.println("");
Tree HuffmanTree = PQ.poll(); //one tree remains
int totalLength=0;
String theCode = new String("");
for(int i=0; i<word.length();i++)
{
theCode=HuffmanTree.getCode(word.charAt(i));
System.out.print(theCode+" ");
totalLength+=theCode.length();
}
System.out.println("");
int num1 = (totalLength);
int num2 = (word.length()*7);
double compression = (((double)num1/(double)num2)*100);
System.out.println("");
System.out.println("Compressed size is: " + num1 + " bits / " + num2 + " bits = " + (int)compression + "%");
sc.close();
}
}
class Node
{
public char letter; //stores letter
public Node leftChild; // this node's left child
public Node rightChild; // this node's right child
} // end class Node
class Tree implements Comparable<Tree>
{
public Node root; // first node of tree
public int frequency=0;
// -------------------------------------------------------------
public Tree()
{
root = null;
} // no nodes in tree yet
// -------------------------------------------------------------
//the PriorityQueue needs to be able to somehow rank the objects in it
//thus, the objects in the PriorityQueue must implement an interface called Comparable
//the interface requires you to write a compareTo() method so here it is:
public int compareTo(Tree object)
{ //
if(frequency-object.frequency>0)
{ //compare the cumulative frequencies of the tree
return 1;
}
else if(frequency-object.frequency<0)
{
return -1; //return 1 or -1 depending on whether these frequencies are bigger or smaller
}
else
{
return 0; //return 0 if they're the same
}
}
// -------------------------------------------------------------
String path; //this variable will track the path to the letter we're looking for
public String getCode(char letter)
{
path = new String("");
inOrder(root, letter, path);
return path;
}
public void inOrder(Node localRoot, char letter, String path)
{
if(localRoot != null)
{
if(localRoot.letter==letter)
{
this.path=path;
}
else
{
inOrder(localRoot.rightChild, letter, "1"+path);
inOrder(localRoot.leftChild, letter, "0"+path);
}
}
}
}