1 public class HuffmanDemo
2 {
3 public static void main(String[] args)
4 {
5 Map<Character, Integer> frequencyMap = new HashMap<Character, Integer>();
6 frequencyMap.put('A', 2089);
7 frequencyMap.put('E', 576);
8 frequencyMap.put('H', 357);
9 frequencyMap.put('I', 671);
10 frequencyMap.put('K', 849);
11 frequencyMap.put('L', 354);
12 frequencyMap.put('M', 259);
13 frequencyMap.put('N', 660);
14 frequencyMap.put('O', 844);
15 frequencyMap.put('P', 239);
16 frequencyMap.put('U', 472);
17 frequencyMap.put('W', 74);
18 frequencyMap.put('\'', 541);
19 HuffmanTree tree = new HuffmanTree(frequencyMap);
20
21 Map<Character, String> encodingMap = tree.getEncodingMap();
22 String encoded = encode("ALOHA", encodingMap);
23 System.out.println(encoded);
24 String decoded = tree.decode(encoded);
25 System.out.println(decoded);
26 }
27
28 public static String encode(String toEncode, Map<Character, String> encodingMap)
29 {
30 String result = "";
31 for (int i = 0; i < toEncode.length(); i++)
32 {
33 char ch = toEncode.charAt(i);
34 result = result + encodingMap.get(ch);
35 }
36 return result;
37 }
38 }