Huffman 编码的学习
代码行数有点多,实在理解的有点慢,对代码看的也是一知半解,不是很透彻
1.Huffman 编码 (节点定义与文件读取)
对于哈夫曼树,说简单一点,其就是带权路径长度最短的树。
定义:给定N个权值作为N个叶子结点,构造一棵二叉树,若该树的带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树。哈夫曼树是带权路径长度最短的树,权值较大的结点离根较近。
2.代码
在这一段代码中,出现了几个新东西,一是newBufferedReader,try catch,Collectors等
2.1 newBufferedReader
BufferedReader类从字符输入流中读取文本并缓冲字符,以便有效地读取字符,数组和行,可以通过构造函数指定缓冲区大小也可以使用默认大小。对于大多数用途,默认值足够大由Reader构成的每个读取请求都会导致相应的读取请求由基础字符或字节流构成,建议通过BufferedReader包装Reader的实例类以提高效率。引用自:JAVA基础知识之BufferedReader流
2.2 try catch
如果try中的代码没有出错,则程序正常运行try中的内容后,不会执行catch中的内容
如果try中的代码一但出错,程序立即跳入catch中去执行代码,那么try中出错代码后的所有代码就不再执行了.
2.3 Collectors
java.util.stream.Collectors实现各种有用的缩减操作的Collector的实现,例如将元素累积到集合中,根据各种标准汇总元素等。Collectors可以帮我们完成的事情,例如:分组、排序(支持多字段排序)、最大值、最小值、平均值,简单的来说,以前我们在数据上面用sql去完成的聚合相关的操作,Collectors都可以完成。、
代码
package 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;
public class Huffman {
/**
* 哈夫曼树的节点
*/
class HuffmanNode {
/**
* 字符,仅对叶子节点有效
*/
char character;
/**
* 权重
*/
int weight;
/**
* 左孩子
*/
HuffmanNode leftChild;
/**
* 右孩子
*/
HuffmanNode rightChild;
/**
* 父母. 其对构建哈夫曼节点有帮助
*/
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
/**
*******************
* 重写toString
*******************
*/
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;
/**
* 输入文本. 为简单起见,它存储在字符串中。
*/
String inputText;
/**
* 字母的长度,也是叶子节点的数量。
*/
int alphabetLength;
/**
* 字母
*/
char[] alphabet;
/**
* 字符统计数量,它的长度等于2*字符表长度-1 非叶子节点
*/
int[] charCounts;
/**
* 字符到字母表中索引的对应点。
*/
int[] charMapping;
/**
* 字母表中每个字符的代码。 它应该具有与字母相同的长度。
*/
String[] huffmanCodes;
/**
* 所有节点,最后一个节点是根节点
*/
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
}
day28总结
今天的新的形式太多,理解的不是很透彻
1.前言
在之前的学习中,主要实现了哈夫曼树节点的构造以及数据文本的写入,今日继续对哈夫曼树的构造进行学习。
2.Huffman 编码 (建树)
在哈夫曼树的构造原理中,最核心的观点就是:所有叶子结点的带权路径长度之和最小。
然后根据这一原理,我们要将算法详细讲解为这种:
我们应该尽可能地让权值大的叶子结点靠近根结点,让权值小的叶子结点远离根结点
今日的主要代码部分为:字母表的映射构造,哈夫曼树的构造。
3.字母表的映射构造
Step 1-2:初始化等数据准备工作
public void constructAlphabet() {
// Initialize.
Arrays.fill(charMapping, -1);
// The count for each char. At most NUM_CHARS chars.
int[] tempCharCounts = new int[NUM_CHARS];
// The index of the char in the ASCII charset.
int tempCharIndex;
// Step 1. Scan the string to obtain the counts.
char tempChar;
for (int i = 0; i < inputText.length(); i++) {
tempChar = inputText.charAt(i);
tempCharIndex = (int) tempChar;// 将char强制转换为int,例如a对应的ASCII值为97
System.out.print("" + tempCharIndex + " ");
tempCharCounts[tempCharIndex]++;
} // Of for i
// Step 2.进行遍历,以确定数组大小
alphabetLength = 0;
for (int i = 0; i < 255; i++) {
if (tempCharCounts[i] > 0) {
alphabetLength++;
} // Of if
} // Of for i
Step 3:建立各种数组
// Step 3. Compress to the alphabet
alphabet = new char[alphabetLength];
charCounts = new int[2 * alphabetLength - 1];
int tempCounter = 0;
for (int i = 0; i < NUM_CHARS; i++) {
if (tempCharCounts[i] > 0) {
alphabet[tempCounter] = (char) i;
charCounts[tempCounter] = tempCharCounts[i];
charMapping[i] = tempCounter;
tempCounter++;
} // Of if
Step 3:构建树
// Step 3. Construct the tree.
int tempLeft, tempRight, tempMinimal;
for (int i = alphabetLength; i < 2 * alphabetLength - 1; i++) {
// Step 3.1 Select the first minimal as the left child.
tempLeft = -1;
tempMinimal = Integer.MAX_VALUE;
for (int j = 0; j < i; j++) {
if (tempProcessed[j]) {
continue;
} // Of if
if (tempMinimal > charCounts[j]) {
tempMinimal = charCounts[j];
tempLeft = j;
} // Of if
} // Of for j
tempProcessed[tempLeft] = true;
// Step 3.2 Select the second minimal as the right child.
tempRight = -1;
tempMinimal = Integer.MAX_VALUE;
for (int j = 0; j < i; j++) {
if (tempProcessed[j]) {
continue;
} // Of if
if (tempMinimal > charCounts[j]) {
tempMinimal = charCounts[j];
tempRight = j;
} // Of if
} // Of for j
tempProcessed[tempRight] = true;
System.out.println("Selecting " + tempLeft + " and " + tempRight);
// Step 3.3 Construct the new node.
charCounts[i] = charCounts[tempLeft] + charCounts[tempRight];
nodes[i] = new HuffmanNode('*', charCounts[i], nodes[tempLeft], nodes[tempRight], null);
// Step 3.4 Link with children.
nodes[tempLeft].parent = nodes[i];
nodes[tempRight].parent = nodes[i];
在建立节点关系的过程里面,依次遍历第一个最小的值作为左孩子,将第二个最小的值作为右孩子,这样两个相加的权值就是最低的。在找到这两个最小的值作为节点后,通过HuffmanNode方法创建一个新节点,作为这两个节点的父母,命名为*,权值为两个孩子的权值和,传入左右孩子节点,最后一步链接这个新节点的两个孩子。整个建树的过程就是从下至上,又最小的两个权值节点构建父节点
通过递归来寻找根节点
public HuffmanNode getRoot() {
return nodes[nodes.length - 1];
}// Of getRoot
day29总结
这次的代码实现由于之前考试学习了霍夫曼树的构造方式,所以理解起来能够比昨天更透彻
1.前言
主要继续完成哈夫曼树的部分,主要实现具体应用中,编码以及译码的实现。
public void generateCodes() {
huffmanCodes = new String[alphabetLength];
HuffmanNode tempNode;
for (int i = 0; i < alphabetLength; i++) {
tempNode = nodes[i];
// Use tempCharCode instead of tempCode such that it is unlike
// tempNode.
// This is an advantage of long names.
String tempCharCode = "";
while (tempNode.parent != null) {
if (tempNode == tempNode.parent.leftChild) {
tempCharCode = "0" + tempCharCode;
} else {
tempCharCode = "1" + tempCharCode;
} // Of if
tempNode = tempNode.parent;
} // Of while
huffmanCodes[i] = tempCharCode;
System.out.println("The code of " + alphabet[i] + " is " + tempCharCode);
} // Of for i
}// Of generateCodes
/**
*********************
* Encode the given string.
*
* @param paraString
* The given string.
*********************
*/
public String coding(String paraString) {
String resultCodeString = "";
int tempIndex;
for (int i = 0; i < paraString.length(); i++) {
// From the original char to the location in the alphabet.
tempIndex = charMapping[(int) paraString.charAt(i)];
// From the location in the alphabet to the code.
resultCodeString += huffmanCodes[tempIndex];
} // Of for i
return resultCodeString;
}// Of coding
/**
*********************
* Decode the given string.
*
* @param paraString
* The given string.
*********************
*/
public String decoding(String paraString) {
String resultCodeString = "";
HuffmanNode tempNode = getRoot();//
for (int i = 0; i < paraString.length(); i++) {
if (paraString.charAt(i) == '0') {
tempNode = tempNode.leftChild;
System.out.println(tempNode);
} else {
tempNode = tempNode.rightChild;
System.out.println(tempNode);
} // Of if
if (tempNode.leftChild == null) {
System.out.println("Decode one:" + tempNode);
// Decode one char.
resultCodeString += tempNode.character;
// Return to the root.
tempNode = getRoot();
} // Of if
} // Of for i
return resultCodeString;
}// Of decoding
2.总结
在整个哈夫曼树的三天代码中,特别是从22天的二叉树开始,自身的知识储备就开始跟不上了,深深的感觉到了自身代码量以及关于java知识积累的不足。目前我感觉到了JAVA与C程序编写上的差别,但是不知道该怎么去形容。有必要对java的一些知识听一听讲解,边学边抄代码。很多基础知识实在是缺失,直接只抄和理解代码过程,并不能从根本解决问题