- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeHuffmanDecoding.java
More file actions
Latest commit
175 lines (127 loc) · 4.8 KB
/
Copy pathTreeHuffmanDecoding.java
File metadata and controls
175 lines (127 loc) · 4.8 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
/*
Huffman Coding Decoder
You are tasked with implementing a decoder for Huffman coding. Huffman coding is a lossless data compression algorithm
that assigns variable-length codes to input characters based on their frequencies. More frequent characters are assigned shorter codes,
while less frequent characters receive longer codes. Your goal is to decode a given binary-encoded string using a provided Huffman tree.
Input
A string s representing the Huffman-encoded binary string (composed of '0's and '1's).
A reference to the root node of a Huffman tree that has been constructed based on character frequencies.
Output
A single line containing the decoded string.
Constraints
The input string s will only contain characters '0' and '1'.
The Huffman tree will be valid and contain characters in its leaf nodes.
*/
importjava.util.*;
abstractclassNodeimplementsComparable<Node> {
publicintfrequency; // the frequency of this tree
publicchardata;
publicNodeleft, right;
publicNode(intfreq) {
frequency = freq;
}
// compares on the frequency
publicintcompareTo(Nodetree) {
returnfrequency - tree.frequency;
}
}
classHuffmanLeafextendsNode {
publicHuffmanLeaf(intfreq, charval) {
super(freq);
data = val;
}
}
classHuffmanNodeextendsNode {
publicHuffmanNode(Nodel, Noder) {
super(l.frequency + r.frequency);
left = l;
right = r;
}
}
classDecoding {
/*
class Node
public int frequency; // the frequency of this tree
public char data;
public Node left, right;
*/
voiddecode(Strings, Noderoot)
{
intn = s.length();
NodecurrentNode = root;
StringBuilderdecodedHuffString = newStringBuilder();
for(inti = 0; i < n; i++)
{
currentNode = s.charAt(i) == '0'? currentNode.left : currentNode.right;
if(currentNode.left == null && currentNode.right == null)
{
decodedHuffString = decodedHuffString.append(currentNode.data);
currentNode = root;
}
}
System.out.println(decodedHuffString.toString());
}
}
publicclassSolution {
// input is an array of frequencies, indexed by character code
publicstaticNodebuildTree(int[] charFreqs) {
PriorityQueue<Node> trees = newPriorityQueue<Node>();
// initially, we have a forest of leaves
// one for each non-empty character
for (inti = 0; i < charFreqs.length; i++)
if (charFreqs[i] > 0)
trees.offer(newHuffmanLeaf(charFreqs[i], (char)i));
asserttrees.size() > 0;
// loop until there is only one tree left
while (trees.size() > 1) {
// two trees with least frequency
Nodea = trees.poll();
Nodeb = trees.poll();
// put into new node and re-insert into queue
trees.offer(newHuffmanNode(a, b));
}
returntrees.poll();
}
publicstaticMap<Character,String> mapA=newHashMap<Character ,String>();
publicstaticvoidprintCodes(Nodetree, StringBufferprefix) {
asserttree != null;
if (treeinstanceofHuffmanLeaf) {
HuffmanLeafleaf = (HuffmanLeaf)tree;
// print out character, frequency, and code for this leaf (which is just the prefix)
//System.out.println(leaf.data + "\t" + leaf.frequency + "\t" + prefix);
mapA.put(leaf.data,prefix.toString());
} elseif (treeinstanceofHuffmanNode) {
HuffmanNodenode = (HuffmanNode)tree;
// traverse left
prefix.append('0');
printCodes(node.left, prefix);
prefix.deleteCharAt(prefix.length()-1);
// traverse right
prefix.append('1');
printCodes(node.right, prefix);
prefix.deleteCharAt(prefix.length()-1);
}
}
publicstaticvoidmain(String[] args) {
Scannerinput = newScanner(System.in);
Stringtest= input.next();
// we will assume that all our characters will have
// code less than 256, for simplicity
int[] charFreqs = newint[256];
// read each character and record the frequencies
for (charc : test.toCharArray())
charFreqs[c]++;
// build tree
Nodetree = buildTree(charFreqs);
// print out results
printCodes(tree, newStringBuffer());
StringBuffers = newStringBuffer();
for(inti = 0; i < test.length(); i++) {
charc = test.charAt(i);
s.append(mapA.get(c));
}
//System.out.println(s);
Decodingd = newDecoding();
d.decode(s.toString(), tree);
}
}