-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHuffmanTree.java
More file actions
152 lines (128 loc) · 4.37 KB
/
Copy pathHuffmanTree.java
File metadata and controls
152 lines (128 loc) · 4.37 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
package Project02;
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
public class HuffmanTree {
class Node {
// static class Node implements Comparable{
Node left, right;
char key;
int frequency;
Node(char k, int v )
{
this.key = k;
this.frequency = v;
this.left = null;
this.right = null;
}
Node(char k, int v, Node left, Node right )
{
this.key = k;
this.frequency = v;
this.left = left;
this.right = right;
}
public String toString() {
return (key + ":" + frequency) ;
}
}
/* Execution includes insert, delete, search, printing function */
private Node root;
String decodeStr = "";
public HuffmanTree() // constructor
{
root = null;
}
/*
* First construct a frequency table for the message,
* then create a Huffman tree for this message
*/
public void buildTree(String str) {
//Construct a frequency table for the message
HashMap<Character, Integer> freqs = new HashMap<Character, Integer>();
char key;
for (int i=0; i<str.length(); i++) {
key = str.charAt(i);
if ( freqs.containsKey(key))
freqs.put(key, freqs.get(key)+1); //key exists, increase the value
else
freqs.put(key, 1); // add key, value = 1
}
/* print the frequency table */
System.out.println("\nFrequency table: ");
for(Character key1: freqs.keySet())
System.out.println(key1 + " " + freqs.get(key1));
/* minimum heap (priority queue) */
PriorityQueue<Node> queues = new PriorityQueue<>((l, r) -> l.frequency - r.frequency);
for(Character key2: freqs.keySet()) // 1. Insert node into last position
queues.add(new Node(key2, freqs.get(key2)));// 2. Bubble upward
/* Create a Huffman Tree for the message */
while (queues.size() > 1) {
Node left = queues.poll();
Node right = queues.poll();
int sum = left.frequency + right.frequency;
queues.add(new Node('#', sum, left, right));
}
root = queues.peek();
}
/*
* Create a code table
*/
public Map<Character, String> encode() {
HashMap<Character, String> huffmanCode = new HashMap<Character, String>();
Map<Character, String> tCode = encode(root, "", huffmanCode);
return tCode;
}
public HashMap<Character, String> encode(Node node, String strCode, HashMap<Character, String> huffmanCode) {
if (node == null)
return huffmanCode;
if(node.left == null && node.right == null)
huffmanCode.put(node.key, strCode);
encode(node.left, strCode + "0", huffmanCode);
encode(node.right, strCode + "1", huffmanCode);
return huffmanCode;
}
/*
* Decode the message from binary
*/
public void decode(StringBuilder sb) {
int i=-1;
while( i<sb.length() - 1) {
i = decode(root, sb, i);
}
}
public int decode(Node node, StringBuilder sb, int index) {
if (node == null)
return index;
if (node.left == null && node.right == null) {
decodeStr = decodeStr + node.key;
return index;
}
index ++;
if (sb.charAt(index) == '0') // go left if the code is '0'
index = decode(node.left, sb, index);
else // go right if the code is '1'
index = decode(node.right, sb, index);
return index;
}
/* Encode the message into binary */
public StringBuilder encodeString(Map<Character, String> huffmanCode, String str) {
StringBuilder sb = new StringBuilder();
for (int i=0; i<str.length(); i++) {
sb.append(huffmanCode.get(str.charAt(i)));
}
return sb;
}
/*
* print the code table, binary of encode the message,
* and the message of decode to the console
*/
public void printCode(Map<Character, String> huffmanCode, StringBuilder sb) {
System.out.println("\n\nHuffman Codes: ");
for (Map.Entry<Character, String> entry : huffmanCode.entrySet())
System.out.println(entry.getKey() + " " + entry.getValue());
System.out.println("\nthe code of encode the message into binary: ");
System.out.print(sb + "\n");
System.out.println("\nDecode message: " + decodeStr);
}
}