-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathhuffmanproject.java
278 lines (237 loc) · 9.28 KB
/
huffmanproject.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
/***************************************************
Project name: huffman tree
Filename: h_rybacki_prj.java
Student Name: Harry Rybacki
Date: 29Oct2012
Tasks
1. Build and display a complete Huffman Tree
2. Encode 'tea' and display the resulting bits
3. Decode the bits associated with 'tea' and print the resulting string
Computing requirements:
Language: Java
Platform: Linux
Submission requirements: Tested on UNCG's linux server. One file h_rybacki_prj.java
***************************************************/
import java.util.ArrayList;
import java.util.PriorityQueue;
public class huffmanproject {
public static class HuffNode implements Comparable<HuffNode> {
// fields
public int value;
public int weight;
public HuffNode leftTree;
public HuffNode rightTree;
public HuffNode parent;
// constructors
public HuffNode() {
parent = null;
}
public HuffNode( int v, int w, HuffNode lTree, HuffNode rTree, HuffNode par ) {
value = v;
weight = w;
leftTree = lTree;
rightTree = rTree;
parent = par;
}
// setters/getters
@Override
public int compareTo(HuffNode rhs) {
return weight - rhs.weight;
}
@Override
public String toString() {
String str = "";
str += this.value;
return str;
}
}
// object representing a huffman tree
public static class HuffTree {
// fields
private int size = 0;
private HuffNode root = new HuffNode();
private PriorityQueue<HuffNode> huffQueue = new PriorityQueue();
public ArrayList<String> pathTable = new ArrayList();
public ArrayList<Character> valueTable = new ArrayList();
// constructor
public HuffTree(int[] freq, char[] code) {
// get the counts
this.size = freq.length;
// throw exception if arrays are different sizes
if (freq.length != code.length) {
throw new UnsupportedOperationException("Error: Character and code length mismatch.");
}
// build huffQueue from frequencies given
for (int i = 0; i < this.size; i++) {
huffQueue.offer(new HuffNode(code[i], freq[i], null, null, null));
}
// build huffman tree from queue
createTree();
// build code table from huffman tree
createTable(this.root, "");
}
// setters/getters
/**
* creates Huffman Tree from frequencies and values
* @param null
*/
private void createTree() {
// while elements remain in huffQueue, add to tree
while (huffQueue.size() > 1) {
// pop off two minimum elements in huffQueue
HuffNode tempL = huffQueue.poll();
HuffNode tempR = huffQueue.poll();
// create root for two minimum elements and build tree
HuffNode parent = new HuffNode(0, tempL.weight+tempR.weight, tempL, tempR, null);
tempL.parent = parent;
tempR.parent = parent;
// add new tree back in huffQueue
huffQueue.offer(parent);
this.size++;
}
// set HuffTree root to remaining element in huffQueue
this.root = huffQueue.peek();
}
/**
* creates code table for a huffman tree
* @param HuffNode -- root for tree, string -- for building paths
*/
private void createTable(HuffNode curr, String str) {
// if iterator is null, return
if (curr == null) return;
// else if leaf, display path and value
if (curr.leftTree == null && curr.rightTree == null) {
char tempChar;
if (curr.value == 32)
tempChar = ' ';
if (curr.value == 10)
tempChar = 'n';
else
tempChar = (char)curr.value;
// add value and path to code tables
this.valueTable.add(tempChar);
this.pathTable.add(str);
}
// add 0 if before moving to left child
str += "0";
// recursively call in pre-order
createTable(curr.leftTree, str);
// adjust path and add 1 before moving to right child
str = str.substring(0, str.length()-1);
str += "1";
createTable(curr.rightTree, str);
}
/**
* display given huffman tree using pre-order traversal
* @param HuffNode -- root of tree to be displayed
*/
// global variable used for representing 'levels' of tree
String tacks = "";
public void getTree(HuffNode curr) {
// if iterator is null, return
if (curr == null) return;
// else if leaf, display level, weight, and value
if (curr.leftTree == null && curr.rightTree == null) {
// case statements to handle displaying space and newline
switch (curr.value) {
case 32:
System.out.println(tacks + curr.weight + ": sp");
break;
case 10:
System.out.println(tacks + curr.weight + ": nl");
break;
default:
System.out.println(tacks + curr.weight + ": " + (char)curr.value);
break;
}
}
// else display level and weight
else
System.out.println(tacks + curr.weight);
// increment level marker
tacks += "- ";
// recursively call in pre-order
getTree(curr.leftTree);
getTree(curr.rightTree);
// decrement level marker
tacks = tacks.substring(0, tacks.length()-2);
}
/**
* returns size of a given huffman tree
* @param HuffTree - to obtain size of
* @return int -- size of tree
*/
public int getSize() { return this.size; }
/**
* returns encoded bits for a given string
* @param String -- to be encoded
* @return String -- encoded version of original string
*/
public String encode(String input){
// create empty string to hold code
String str = "";
// iterate through given string
for (int x = 0; x < input.length(); x++) {
// iterate through code tables
for (int i = 0; i < valueTable.size(); i++) {
// if char in string matches code in table, add path to string
if (valueTable.get(i) == input.charAt(x))
str += pathTable.get(i);
}
}
return str;
}
/**
* returns decoded string for a given set of bits
* @param String -- bits to be decoded
* @return String -- decoded version of bits
*/
public String decode(String bits) {
// create empty string to hold decoded message
String decodedStr = "";
// iterate through bits
for (int i = 0; i < bits.length(); i++) {
if (!getChar(bits.substring(0, i+1)).equals("")) {
decodedStr += getChar(bits.substring(0, i+1));
bits = bits.substring(i+1);
i = 0;
}
}
return decodedStr;
}
/**
* returns character for a given set of bits
* @param String -- bits to be checked
* @return String -- character associated with bits if any
*/
private String getChar(String bits) {
// create string to hold potential character
String character = "";
// traverse code table to seek match
for (int i = 0; i < pathTable.size(); i++) {
// add to string if match is found
if (pathTable.get(i).equals(bits))
character = valueTable.get(i).toString();
}
return character;
}
}
// driver program -- main
public static void main(String[] args) {
// fields
int freq[] = {10, 15, 12, 3, 4, 13, 1};
char code[] = {'a', 'e', 'i', 's', 't', ' ', '\n'};
// build Huffman Tree using given codes/frequencies
HuffTree hTree = new HuffTree(freq, code);
// display contents of Huffman Tree in Pre-Order Traversal
System.out.println("Display Tree:");
HuffNode curr = hTree.root;
hTree.getTree(curr);
System.out.println("");
// encode 'tea'
System.out.println("Encode 'tea': " + hTree.encode("tea") +"\n");
// decode 'tea' -- using the actual methods built in
System.out.println("Decode '" + hTree.encode("tea") + "': " +
hTree.decode(hTree.encode("tea")));
}
}