哈夫曼编码译码java
时间: 2024-03-30 07:32:06 浏览: 197
哈夫曼编码译码
哈夫曼编码是一种常用的数据压缩算法,它通过将出现频率较高的字符用较短的编码表示,而将出现频率较低的字符用较长的编码表示,从而实现对数据的压缩。下面是一个简单的哈夫曼编码和译码的Java实现示例:
```java
import java.util.*;
class HuffmanNode implements Comparable<HuffmanNode> {
char data;
int frequency;
HuffmanNode left, right;
public HuffmanNode(char data, int frequency) {
this.data = data;
this.frequency = frequency;
left = right = null;
}
public int compareTo(HuffmanNode node) {
return this.frequency - node.frequency;
}
}
public class HuffmanCoding {
public static void main(String[] args) {
String text = "Hello, World!";
Map<Character, Integer> frequencyMap = buildFrequencyMap(text);
HuffmanNode root = buildHuffmanTree(frequencyMap);
Map<Character, String> huffmanCodes = buildHuffmanCodes(root);
String encodedText = encode(text, huffmanCodes);
String decodedText = decode(encodedText, root);
System.out.println("Original text: " + text);
System.out.println("Encoded text: " + encodedText);
System.out.println("Decoded text: " + decodedText);
}
private static Map<Character, Integer> buildFrequencyMap(String text) {
Map<Character, Integer> frequencyMap = new HashMap<>();
for (char c : text.toCharArray()) {
frequencyMap.put(c, frequencyMap.getOrDefault(c, 0) + 1);
}
return frequencyMap;
}
private static HuffmanNode buildHuffmanTree(Map<Character, Integer> frequencyMap) {
PriorityQueue<HuffmanNode> pq = new PriorityQueue<>();
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
pq.add(new HuffmanNode(entry.getKey(), entry.getValue()));
}
while (pq.size() > 1) {
HuffmanNode left = pq.poll();
HuffmanNode right = pq.poll();
HuffmanNode parent = new HuffmanNode('\0', left.frequency + right.frequency);
parent.left = left;
parent.right = right;
pq.add(parent);
}
return pq.poll();
}
private static Map<Character, String> buildHuffmanCodes(HuffmanNode root) {
Map<Character, String> huffmanCodes = new HashMap<>();
buildHuffmanCodesHelper(root, "", huffmanCodes);
return huffmanCodes;
}
private static void buildHuffmanCodesHelper(HuffmanNode node, String code, Map<Character, String> huffmanCodes) {
if (node == null) {
return;
}
if (node.left == null && node.right == null) {
huffmanCodes.put(node.data, code);
}
buildHuffmanCodesHelper(node.left, code + "0", huffmanCodes);
buildHuffmanCodesHelper(node.right, code + "1", huffmanCodes);
}
private static String encode(String text, Map<Character, String> huffmanCodes) {
StringBuilder encodedText = new StringBuilder();
for (char c : text.toCharArray()) {
encodedText.append(huffmanCodes.get(c));
}
return encodedText.toString();
}
private static String decode(String encodedText, HuffmanNode root) {
StringBuilder decodedText = new StringBuilder();
HuffmanNode current = root;
for (char c : encodedText.toCharArray()) {
if (c == '0') {
current = current.left;
} else {
current = current.right;
}
if (current.left == null && current.right == null) {
decodedText.append(current.data);
current = root;
}
}
return decodedText.toString();
}
}
```
以上是一个简单的哈夫曼编码和译码的Java实现示例。你可以根据需要进行修改和扩展。希望对你有帮助!
阅读全文