-
Notifications
You must be signed in to change notification settings - Fork 78
/
HoffmannCoding.java
102 lines (81 loc) · 3.11 KB
/
HoffmannCoding.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
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
import java.util.*;
class Node {
int frequency;
char character;
Node left, right;
Node(char character, int frequency) {
this.character = character;
this.frequency = frequency;
left = right = null;
}
}
class HuffmanCoding {
private static Node root;
// Comparator to sort nodes based on frequency
private static class NodeComparator implements Comparator<Node> {
public int compare(Node n1, Node n2) {
return n1.frequency - n2.frequency;
}
}
// Build the Huffman Tree
private static void buildHuffmanTree(String text) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : text.toCharArray()) {
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}
PriorityQueue<Node> priorityQueue = new PriorityQueue<>(new NodeComparator());
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
priorityQueue.add(new Node(entry.getKey(), entry.getValue()));
}
while (priorityQueue.size() > 1) {
Node left = priorityQueue.poll();
Node right = priorityQueue.poll();
Node newNode = new Node('\0', left.frequency + right.frequency);
newNode.left = left;
newNode.right = right;
priorityQueue.add(newNode);
}
root = priorityQueue.poll();
}
// Generate the Huffman codes
private static void generateCodes(Node node, String code, Map<Character, String> huffmanCode) {
if (node == null) return;
if (node.left == null && node.right == null) {
huffmanCode.put(node.character, code);
}
generateCodes(node.left, code + "0", huffmanCode);
generateCodes(node.right, code + "1", huffmanCode);
}
// Encode the input text
public static String encode(String text) {
buildHuffmanTree(text);
Map<Character, String> huffmanCode = new HashMap<>();
generateCodes(root, "", huffmanCode);
StringBuilder encodedText = new StringBuilder();
for (char c : text.toCharArray()) {
encodedText.append(huffmanCode.get(c));
}
return encodedText.toString();
}
// Decode the encoded text
public static String decode(String encodedText) {
StringBuilder decodedText = new StringBuilder();
Node currentNode = root;
for (char bit : encodedText.toCharArray()) {
currentNode = (bit == '0') ? currentNode.left : currentNode.right;
if (currentNode.left == null && currentNode.right == null) {
decodedText.append(currentNode.character);
currentNode = root;
}
}
return decodedText.toString();
}
public static void main(String[] args) {
String text = "hello huffman";
System.out.println("Original Text: " + text);
String encodedText = encode(text);
System.out.println("Encoded Text: " + encodedText);
String decodedText = decode(encodedText);
System.out.println("Decoded Text: " + decodedText);
}
}