-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.java
More file actions
212 lines (192 loc) · 4.78 KB
/
HuffmanTree.java
File metadata and controls
212 lines (192 loc) · 4.78 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
import java.util.*;
import java.io.*;
public class HuffmanTree
{
private Node root;
private HashMap<Integer, String> codeMap;
public HuffmanTree(int[] count)
{
Queue<Node> pqNode = new PriorityQueue<Node>();
root = null;
codeMap = new HashMap<Integer, String>();
for(int i = 0; i < count.length; i++)
{
if(count[i] != 0)
{
Node tempChar = new Node((char)i, count[i]);
pqNode.offer(tempChar);
}
}
Node EOF = new Node((char) 256, 1);
pqNode.offer(EOF);
while(pqNode.size() > 1)
{
Node temp1 = pqNode.poll();
Node temp2 = pqNode.poll();
Node knew = new Node((char) 0, temp1.frequency + temp2.frequency);
knew.left = temp1;
knew.right = temp2;
pqNode.offer(knew);
}
root = pqNode.poll();
}
public String getCode(Integer a)
{
return codeMap.get(a);
}
public void printTree()
{
TreePrinter.printTree(root);
}
public void write(String fileName)
{
String outputFileName = fileName;
PrintWriter diskFile = null;
try {
diskFile = new PrintWriter(new File(outputFileName));
}
catch (IOException io) {
System.out.println("Could not create file: " + outputFileName);
}
writerHelper(root, "", diskFile);
diskFile.close();
}
public void writerHelper(Node current, String ret, PrintWriter writer)
{
if(current.left == null && current.right == null)
{
codeMap.put(current.val, ret);
ret = current.val + "\n" + ret;
writer.println(ret);
}
if(current.right != null)
{
writerHelper(current.right, ret + "1", writer);
}
if(current.left != null)
{
writerHelper(current.left, ret + "0", writer);
}
}
public HuffmanTree(String codeFile)
{
codeMap = new HashMap<Integer, String>();
try
{
Scanner console = new Scanner(new File(codeFile));
while(console.hasNextLine())
{
if(root == null)
{
root = new Node(0, 0);
}
int value = Integer.parseInt(console.nextLine());
if(console.hasNextLine())
{
String temp = console.nextLine();
codeMap.put(value, temp);
root = tree(value, temp, root);
}
}
}
catch (IOException io)
{
System.out.println("Could not create file" );
}
}
public Node tree(int inte, String ret, Node current)
{
if(ret == "")
{
current.val = inte;
}
else if(ret.charAt(0) == '1')
{
if(current.right == null)
{
current.right = new Node(0, 0);
}
current.right = tree(inte, ret.substring(1), current.right);
}
else if(ret.charAt(0) == '0')
{
if(current.left == null)
{
current.left = new Node(0, 0);
}
current.left = tree(inte, ret.substring(1), current.left);
}
return current;
}
public void decode(BitInputStream in, String outFile)
{
int stream = in.readBit();
String print = "";
Node current = root;
while(current.val != 256)
{
if(current.val != 256)
{
if(stream == 0)
{
current = current.left;
if(current.val == 256) break;
}
if(stream == 1)
{
current = current.right;
if(current.val == 256) break;
}
if(current.right == null && current.left == null)
{
if(current.val != 256) print += (char)current.val;
current = root;
}
stream = in.readBit();
}
}
PrintWriter diskFile = null;
try {
diskFile = new PrintWriter(outFile);
diskFile.print(print);
diskFile.close();
}
catch (IOException io) {
System.out.println("Could not create file: " + outFile);
}
}
}
class Node implements Comparable <Node>
{
public int val;
public Node left, right;
public Integer frequency;
public Node(int val, Integer frequency)
{
this.val = val;
left = right = null;
this.frequency = frequency;
}
@Override
public String toString()
{
if((int) val == 32) return "SPACE";
if((int) val == 10) return "NL";
if((int) val == 0)
return "" + frequency;
if(val == 256) return "END";
return "" + (char) this.val;
}
public int compareTo(Node other)
{
if(other.frequency == this.frequency)
{
return 0;
}
if(other.frequency > this.frequency)
{
return -1;
}
return 1;
}
}