-
Notifications
You must be signed in to change notification settings - Fork 0
/
HuffmanCodingTest.java
46 lines (39 loc) · 2.04 KB
/
HuffmanCodingTest.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
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class HuffmanCodingTest {
public static HashMap<Character, String> huffmanBinaryCodes = new HashMap<Character, String>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Iterator<Map.Entry<Character, String>> it;
TreeNode root;
String testString = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaddhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhdfffgiiijccjj";
HuffmanEncoding huffEncd1 = new HuffmanEncoding();
huffEncd1.getCharFreq(testString);
huffEncd1.bldCharNodesPQ();
huffEncd1.bldHuffmanTree();
root = huffEncd1.getHuffmanTreeRoot();
huffmanBinaryCodes = huffEncd1.getHuffmanCodes(root, new String());
System.out.println("Huffman Codes for the First String.....:");
it = huffmanBinaryCodes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Character, String> entry = (Map.Entry<Character, String>) it.next();
System.out.println(entry.getKey()+" -->> "+entry.getValue());
}
System.out.println("\n");
testString = "gghhiajjjjjkabbbbcccakjlabcjsdbhcjhsbdjchbdsjchbdsjhbcsdhbcjkhsbdcjhbsdjbcjksdhbcjhbsdcjhbsdjchbsdjhbcjdshbcjkdshbjchbsdjhbcjsdhbcjdshbcjkhbsdjchbsdjbcjdsksnckjsdnckjnsdkcjndskjcndsknjsdkjcnksdjncksdnckjnsdkcnsdkcnanfcdaaeeeefaaaaabbccdabbbdascknakcsaclkasjcbkmas dddfffgiiijccjj";
HuffmanEncoding huffEncd2 = new HuffmanEncoding();
huffEncd2.getCharFreq(testString);
huffEncd2.bldCharNodesPQ();
huffEncd2.bldHuffmanTree();
root = huffEncd2.getHuffmanTreeRoot();
huffmanBinaryCodes = huffEncd2.getHuffmanCodes(root, new String());
System.out.println("Huffman Codes for the Second String.....:");
it = huffmanBinaryCodes.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<Character, String> entry = (Map.Entry<Character, String>) it.next();
System.out.println(entry.getKey()+" -->> "+entry.getValue());
}
System.out.println("\n");
}
}