Skip to content

Commit dab317c

Browse files
authored
Create TreeHuffmanDecoding.java
1 parent bda4083 commit dab317c

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed

TreeHuffmanDecoding.java

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
import java.util.*;
2+
3+
abstract class Node implements Comparable<Node> {
4+
public int frequency; // the frequency of this tree
5+
public char data;
6+
public Node left, right;
7+
public Node(int freq) {
8+
frequency = freq;
9+
}
10+
11+
// compares on the frequency
12+
public int compareTo(Node tree) {
13+
return frequency - tree.frequency;
14+
}
15+
}
16+
17+
class HuffmanLeaf extends Node {
18+
19+
20+
public HuffmanLeaf(int freq, char val) {
21+
super(freq);
22+
data = val;
23+
}
24+
}
25+
26+
class HuffmanNode extends Node {
27+
28+
public HuffmanNode(Node l, Node r) {
29+
super(l.frequency + r.frequency);
30+
left = l;
31+
right = r;
32+
}
33+
34+
}
35+
36+
37+
class Decoding {
38+
39+
/*
40+
class Node
41+
public int frequency; // the frequency of this tree
42+
public char data;
43+
public Node left, right;
44+
45+
*/
46+
47+
void decode(String s, Node root)
48+
{
49+
int n = s.length();
50+
Node currentNode = root;
51+
StringBuilder decodedHuffString = new StringBuilder();
52+
53+
for(int i = 0; i < n; i++)
54+
{
55+
currentNode = (s.charAt(i) == '0')? currentNode.left : currentNode.right;
56+
57+
if(currentNode.left == null && currentNode.right == null)
58+
{
59+
decodedHuffString = decodedHuffString.append(currentNode.data);
60+
currentNode = root;
61+
}
62+
}
63+
64+
System.out.println(decodedHuffString.toString());
65+
}
66+
67+
68+
}
69+
70+
71+
public class TreeHuffmanDecoding {
72+
73+
// input is an array of frequencies, indexed by character code
74+
public static Node buildTree(int[] charFreqs) {
75+
76+
PriorityQueue<Node> trees = new PriorityQueue<Node>();
77+
// initially, we have a forest of leaves
78+
// one for each non-empty character
79+
for (int i = 0; i < charFreqs.length; i++)
80+
if (charFreqs[i] > 0)
81+
trees.offer(new HuffmanLeaf(charFreqs[i], (char)i));
82+
83+
assert trees.size() > 0;
84+
85+
// loop until there is only one tree left
86+
while (trees.size() > 1) {
87+
// two trees with least frequency
88+
Node a = trees.poll();
89+
Node b = trees.poll();
90+
91+
// put into new node and re-insert into queue
92+
trees.offer(new HuffmanNode(a, b));
93+
}
94+
95+
return trees.poll();
96+
}
97+
98+
public static Map<Character,String> mapA=new HashMap<Character ,String>();
99+
100+
public static void printCodes(Node tree, StringBuffer prefix) {
101+
102+
assert tree != null;
103+
104+
if (tree instanceof HuffmanLeaf) {
105+
HuffmanLeaf leaf = (HuffmanLeaf)tree;
106+
107+
// print out character, frequency, and code for this leaf (which is just the prefix)
108+
//System.out.println(leaf.data + "\t" + leaf.frequency + "\t" + prefix);
109+
mapA.put(leaf.data,prefix.toString());
110+
111+
} else if (tree instanceof HuffmanNode) {
112+
HuffmanNode node = (HuffmanNode)tree;
113+
114+
// traverse left
115+
prefix.append('0');
116+
printCodes(node.left, prefix);
117+
prefix.deleteCharAt(prefix.length()-1);
118+
119+
// traverse right
120+
prefix.append('1');
121+
printCodes(node.right, prefix);
122+
prefix.deleteCharAt(prefix.length()-1);
123+
}
124+
}
125+
126+
public static void main(String[] args) {
127+
Scanner input = new Scanner(System.in);
128+
129+
String test= input.next();
130+
131+
// we will assume that all our characters will have
132+
// code less than 256, for simplicity
133+
int[] charFreqs = new int[256];
134+
135+
// read each character and record the frequencies
136+
for (char c : test.toCharArray())
137+
charFreqs[c]++;
138+
139+
// build tree
140+
Node tree = buildTree(charFreqs);
141+
142+
// print out results
143+
printCodes(tree, new StringBuffer());
144+
StringBuffer s = new StringBuffer();
145+
146+
for(int i = 0; i < test.length(); i++) {
147+
char c = test.charAt(i);
148+
s.append(mapA.get(c));
149+
}
150+
151+
//System.out.println(s);
152+
Decoding d = new Decoding();
153+
d.decode(s.toString(), tree);
154+
155+
}
156+
}

0 commit comments

Comments
 (0)