1.Huffman编码
1.1 简单介绍
Huffman树又名最优树,是带权路径长度最短的树。最早接触Huffman树是在计算最短带权路径长度的时候,后来Huffman树又可以用于来构造最短前缀码,即Huffman编码。
1.2 Huffman节点的定义
1.2.1 Huffman树的节点定义
每个节点的内容包括: 字符 (仅对叶节点有效)、权重 (用的整数, 该字符的个数)、指向子节点父节点的引用。
char character;
int weight;
HuffmanNode leftChild;
HuffmanNode rightChild;
HuffmanNode parent;
public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild,
HuffmanNode paraRightChild,HuffmanNode paraParent) {
character = paraCharacter;
weight = paraWeight;
leftChild = paraLeftChild;
rightChild = paraRightChild;
parent = paraParent;
}// Of HuffmanNode
1.2.2 Huffman编码准备
inputText 用于读入字符串文件,并用alphabet 来存储读入的字符串中出现的字母,charCounts记录记录对应字符出现次数,charMapping用于记录字符在树中的位置,huffmanCodes 记录信息元对应的编码,nodes数组用于存所有的节点。
public static final int NUM_CHARS = 256;
String inputText;
int alphabetLength;
char[] alphabet;
int[] charCounts;
int[] charMapping;
String[] huffmanCodes;
HuffmanNode[] nodes;
public Huffman(String paraFilename) {
charMapping = new int[NUM_CHARS];
readText(paraFilename);
}// Of the first constructor
1.3 文件读取
- try-catch:try中代码正常运行则直接继续后续运行,不执行catch中代码;若try中代码出错则跳过剩下的代码转而执行catch中的代码。
- Files.newBufferedReader:利用通用缓存读取文本流。
- Collect:收集流中的元素。
public void readText(String paraFilename) {
try {
inputText = Files.newBufferedReader(Paths.get(paraFilename), StandardCharsets.UTF_8).lines()
.collect(Collectors.joining("\n"));
} catch (Exception ee) {
System.out.println(ee);
System.exit(0);
} // Of try
2. 总代码
package datastructure.tree;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* Huffman tree, encoding , and decoding. For simplicity, only ASCII characters
* are supported.
*
* @author Yunhua Hu yunhuahu0528@163.com.
*/
public class Huffman {
/**
* An inner class for Huffman nodes.
*/
class HuffmanNode {
/**
* The char. Only valid for leaf nodes.
*/
char character;
/**
* weight. It can also be double.
*/
int weight;
/**
* The left child.
*/
HuffmanNode leftChild;
/**
* The right child.
*/
HuffmanNode rightChild;
/**
* The parent. It helps constructing the Huffman code if each character.
*/
HuffmanNode parent;
/**
*********************
* The first constructor.
*********************
*/
public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild,
HuffmanNode paraRightChild,HuffmanNode paraParent) {
character = paraCharacter;
weight = paraWeight;
leftChild = paraLeftChild;
rightChild = paraRightChild;
parent = paraParent;
}// Of HuffmanNode
/**
*********************
* To string.
*********************
*/
public String tostring() {
String resultString = "(" + character + ", " + weight + ")";
return resultString;
}// Of tostring
}// Of class HuffmanNode
/**
* The number of characters. 256 for ASCII.
*/
public static final int NUM_CHARS = 256;
/**
* The input text. It is stored in a string for simplicity.
*/
String inputText;
/**
* The length of the alphabet, also the number of leaves.
*/
int alphabetLength;
/**
* The alphabet.
*/
char[] alphabet;
/**
* The count of chars. The length is 2 * alphabetLength - 1 to include non-leaf
* nodes.
*/
int[] charCounts;
/**
* The mapping of chars to the indices in the alphabet.
*/
int[] charMapping;
/**
* Codes for each char in the alphabet. It should have the same length as
* alphabet.
*/
String[] huffmanCodes;
/**
* All nodes. The last node is the root.
*/
HuffmanNode[] nodes;
/**
*********************
* The first constructor.
*
* @param paraFilename
* The text filename.
*********************
*/
public Huffman(String paraFilename) {
charMapping = new int[NUM_CHARS];
readText(paraFilename);
}// Of the first constructor
/**
*********************
* Read text.
*
* @param paraFilename
* The text filename.
*********************
*/
public void readText(String paraFilename) {
try {
inputText = Files.newBufferedReader(Paths.get(paraFilename), StandardCharsets.UTF_8).lines()
.collect(Collectors.joining("\n"));
} catch (Exception ee) {
System.out.println(ee);
System.exit(0);
} // Of try
System.out.println("The text is:\r\n" + inputText);
}// Of readText
public static void main(String args[]) {
Huffman tempHuffman = new Huffman("E:/postgraduate/csdn/temp/huffmantext-small.txt");
}// Of main
}// Of class Huffman
输出:
问题:那个文件导入这行代码去深究,看的是真的迷,那里就大概讲了下它们在这里是用来干啥的。