Copy the code code as follows:
//Implementation class of Huffman coding
public class HffmanCoding {
private int charsAndWeight[][];// [][0] is the character, [][1] stores the weight (number of times) of the character
private int hfmcoding[][];//Storage Huffman tree
private int i = 0; // loop variable
private String hcs[];
public HffmanCoding(int[][] chars) {
// TODO constructor
charsAndWeight = new int[chars.length][2];
charsAndWeight = chars;
hfmcoding = new int[2 * chars.length - 1][4];//Allocate space for Huffman tree
}
//Implementation of Huffman tree
public void coding() {
int n = charsAndWeight.length;
if(n==0)
return;
int m = 2 * n - 1;
//Initialize Huffman tree
for (i = 0; i < n; i++) {
hfmcoding[i][0] = charsAndWeight[i][1];//Initialize the weight of the Huffman tree
hfmcoding[i][1] = 0;//Initialize the root node of the Huffman tree
hfmcoding[i][2] = 0;//Initialize the left child of the Huffman tree
hfmcoding[i][3] = 0;//Initialize the right child of the Huffman tree
}
for (i = n; i < m; i++) {
hfmcoding[i][0] = 0;//Initialize the weights of the Huffman tree
hfmcoding[i][1] = 0;//Initialize the root node of the Huffman tree
hfmcoding[i][2] = 0;//Initialize the left child of the Huffman tree
hfmcoding[i][3] = 0;//Initialize the right child of the Huffman tree
}
//Construct Huffman tree
for (i = n; i < m; i++) {
int s1[] = select(i); // Find the node with the smallest weight whose parents are zero in the Huffman tree
hfmcoding[s1[0]][1] = i;// Pay parents for the minimum value of the Huffman tree
hfmcoding[s1[1]][1] = i;
hfmcoding[i][2] = s1[0];//Left child of new node
hfmcoding[i][3] = s1[1];//right child of new node
hfmcoding[i][0] = hfmcoding[s1[0]][0] + hfmcoding[s1[1]][0];//The weight of the new node is the sum of the weights of the left and right children
}
}
//Find the node with the smallest weight whose parent is zero
private int[] select(int w) {
// TODO Auto-generated method stub
int s[] = { -1, -1 }, j = 0; // s1 is the sequence number of the node with the smallest weight and zero parent, i is the loop variable
int min1 = 32767, min2 = 32767;
for (j = 0; j < w; j++) {
if (hfmcoding[j][1] == 0) {// Only search in nodes that have not yet constructed a binary tree (nodes with zero parents)
if (hfmcoding[j][0] < min1) {
min2 = min1;
s[1] = s[0];
min1 = hfmcoding[j][0];
s[0] = j;
} else if (hfmcoding[j][0] < min2) {
min2 = hfmcoding[j][0];
s[1] = j;
}
}
}
return s;
}
public String[] CreateHCode() {// Find the Huffman code based on the Huffman tree
int n = charsAndWeight.length;
int i, f, c;
String hcodeString = "";
hcs = new String[n];
for (i = 0; i < n; i++) {// Find the Huffman code based on the Huffman tree
c = i;
hcodeString = "";
f = hfmcoding[i][1]; // f is the root node of the Huffman tree
while (f != 0) {//Sequentially until the root node of the tree
if (hfmcoding[f][2] == c) {// Process the left child node
hcodeString += "0";
} else {
hcodeString += "1";
}
c = f;
f = hfmcoding[f][1];
}
hcs[i] = new String(new StringBuffer(hcodeString).reverse());
}
return hcs;
}
public String show(String s) {// Display encoding of string
String textString = "";
char c[];
int k = -1;
c = new char[s.length()];
c = s.toCharArray(); // Convert string to character array
for (int i = 0; i < c.length; i++) {
k = c[i];
for (int j = 0; j < charsAndWeight.length; j++)
if (k == charsAndWeight[j][0])
textString += hcs[j];
}
return textString;
}
// Huffman coding decompilation
public String reCoding(String s) {
String text = "";//Storage decompiled characters
int k = 0, m = hfmcoding.length - 1;//Start querying from the root node
char c[];
c = new char[s.length()];
c = s.toCharArray();
k = m;
for (int i = 0; i < c.length; i++) {
if (c[i] == '0') {
k = hfmcoding[k][2];//The value of k is the sequence number of the left child of the root node
if (hfmcoding[k][2] == 0 && hfmcoding[k][3] == 0)// Determine whether it is a leaf node, condition (both left and right children are zero)
{
text += (char) charsAndWeight[k][0];
k = m;
}
}
if (c[i] == '1') {
k = hfmcoding[k][3];//The value of k is the sequence number of the right child of the root node
if (hfmcoding[k][2] == 0 && hfmcoding[k][3] == 0)// Determine whether it is a leaf node, condition (both left and right children are zero)
{
text += (char) charsAndWeight[k][0];
k = m;
}
}
}
return text;
}
}