在Java上运行规范的霍夫曼代码的参考代码如下:
import java.util.HashMap;
import java.util.Map;
import java.util.PriorityQueue;
class HuffmanNode implements Comparable<HuffmanNode> {
char character;
int frequency;
HuffmanNode left;
HuffmanNode right;
public HuffmanNode(char character, int frequency) {
this.character = character;
this.frequency = frequency;
}
@Override
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 = getFrequencyMap(text);
HuffmanNode root = buildHuffmanTree(frequencyMap);
Map<Character, String> codeMap = getCodeMap(root);
String encodedText = encodeText(text, codeMap);
String decodedText = decodeText(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> getFrequencyMap(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> priorityQueue = new PriorityQueue<>();
for (Map.Entry<Character, Integer> entry : frequencyMap.entrySet()) {
priorityQueue.offer(new HuffmanNode(entry.getKey(), entry.getValue()));
}
while (priorityQueue.size() > 1) {
HuffmanNode leftChild = priorityQueue.poll();
HuffmanNode rightChild = priorityQueue.poll();
HuffmanNode parent = new HuffmanNode('\0', leftChild.frequency + rightChild.frequency);
parent.left = leftChild;
parent.right = rightChild;
priorityQueue.offer(parent);
}
return priorityQueue.poll();
}
private static Map<Character, String> getCodeMap(HuffmanNode root) {
Map<Character, String> codeMap = new HashMap<>();
generateCodeMap(root, "", codeMap);
return codeMap;
}
private static void generateCodeMap(HuffmanNode node, String code, Map<Character, String> codeMap) {
if (node.left == null && node.right == null) {
codeMap.put(node.character, code);
return;
}
generateCodeMap(node.left, code + "0", codeMap);
generateCodeMap(node.right, code + "1", codeMap);
}
private static String encodeText(String text, Map<Character, String> codeMap) {
StringBuilder encodedText = new StringBuilder();
for (char c : text.toCharArray()) {
encodedText.append(codeMap.get(c));
}
return encodedText.toString();
}
private static String decodeText(String encodedText, HuffmanNode root) {
StringBuilder decodedText = new StringBuilder();
HuffmanNode currentNode = root;
for (char c : encodedText.toCharArray()) {
if (c == '0') {
currentNode = currentNode.left;
} else {
currentNode = currentNode.right;
}
if (currentNode.left == null && currentNode.right == null) {
decodedText.append(currentNode.character);
currentNode = root;
}
}
return decodedText.toString();
}
}
这段代码实现了霍夫曼编码的算法。它包括以下几个步骤:
getFrequencyMap()
函数根据输入的文本构建字符频率的映射。buildHuffmanTree()
函数根据字符频率构建霍夫曼树。getCodeMap()
函数根据霍夫曼树生成字符编码的映射。encodeText()
函数根据字符编码映射将文本进行编码。decodeText()
函数根据霍夫曼树将编码的文本进行解码。这段代码可以用于对任意文本进行霍夫曼编码和解码。它的应用场景包括数据压缩、数据传输等需要对文本进行高效编码和解码的场景。
腾讯云提供的相关产品和服务中,与霍夫曼编码相关的可能是数据存储服务、数据传输服务等。您可以访问腾讯云官方网站了解更多相关产品和服务的详细信息。
注意:本答案仅供参考,具体实现方式可能根据实际需求和情况有所调整。
Tencent Serverless Hours 第13期
云+社区沙龙online [国产数据库]
云+社区沙龙online [国产数据库]
云+社区技术沙龙[第9期]
企业创新在线学堂
云+社区沙龙online [新技术实践]
云+社区技术沙龙 [第30期]
云原生正发声
云+社区技术沙龙[第14期]
腾讯技术开放日
领取专属 10元无门槛券
手把手带您无忧上云