代码来自闵老师”日撸 Java 三百行(21-30天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975721
package datastructure.tree;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/**
* Huffman tree, encoding, and decoding. For simplicity, only ASCII
* characters are supported.
*
*
* @author WX873
*
*/
public class Huffman {
/**
* ******************************************************
* An inner class of Huffman nodes.
* @author WX873
* ******************************************************
*/
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 of each character.
*/
HuffmanNode parent;
/**
* ***************************************************
* The first constructor of HuffmanNode.
* @param paraCharacter
* @param paraWeight
* @param paraLeftChild
* @param paraRightChild
* @param paraParent
* ***************************************************
*/
public HuffmanNode(char paraCharacter, int paraWeight, HuffmanNode paraLeftChild, HuffmanNode paraRightChild, HuffmanNode paraParent) {
// TODO Auto-generated constructor stub
character = paraCharacter;
weight = paraWeight;
leftChild = paraLeftChild;
rightChild = paraRightChild;
parent = paraParent;
}//The first constructor of the HuffmanNode.
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 index. 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[] huffmanCode;
/**
* All nodes. The last node is the root.
*/
HuffmanNode[] nodes;
/**
* ******************************************************
* The first constructor.
*
* @param paraFilename
* ******************************************************
*/
public Huffman(String paraFilename) {
// TODO Auto-generated constructor stub
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) {
// TODO: handle exception
System.out.println(ee);
System.exit(0);
}//of try
System.out.println("The text is:\r\n" + inputText);
}//of readText
/**
* ******************************************************
* The entrance of the program.
*
* @param args Not used now
* ******************************************************
*/
public static void main(String args[]) {
Huffman tempHuffman = new Huffman("E:\\Datasets\\UCIdatasets\\temp\\HuffmanTest.txt");
}//of main
}//of Huffman