Java implementation Havermann Coding and anti-coding instance sharing (Havermann algorithm) _java

Source: Internet
Author: User

Copy Code code as follows:

Implementation class of Havermann encoding
public class Hffmancoding {
private int charsandweight[][];//[][0] is a character, [][1] holds the weighted value of the character (number of times)
private int hfmcoding[][];//Storage Haverman
private int i = 0;//loop variable
Private String hcs[];
Public hffmancoding (int[][] chars) {
TODO Construction Method
Charsandweight = new Int[chars.length][2];
Charsandweight = chars;
hfmcoding = new Int[2 * chars.length-1][4];//allocate space for Havermann tree
}
The realization of Havermann tree
public void Coding () {
int n = charsandweight.length;
if (n = = 0)
Return
int m = 2 * n-1;
Initialize Haverman
for (i = 0; i < n; i++) {
Hfmcoding[i][0] = charsandweight[i][1];//initializes the Havermann tree's weighted value
HFMCODING[I][1] = 0;//Initialize the root node of the Havermann tree
HFMCODING[I][2] = 0;//Initializes the left child of the Havermann tree
HFMCODING[I][3] = 0;//initialization of the right child of the Havermann tree
}
for (i = n; i < m; i++) {
Hfmcoding[i][0] = 0;//initializes the Havermann tree's weighted value
HFMCODING[I][1] = 0;//Initialize the root node of the Havermann tree
HFMCODING[I][2] = 0;//Initializes the left child of the Havermann tree
HFMCODING[I][3] = 0;//initialization of the right child of the Havermann tree
}
Building Haverman
for (i = n; i < m; i++) {
int s1[] = select (i);//Find the smallest node in the Havermann tree with a parent zero weight
HFMCODING[S1[0]][1] = i;//for Havermann tree minimum pay parent
HFMCODING[S1[1]][1] = i;
HFMCODING[I][2] = left child of s1[0];//new node
HFMCODING[I][3] = right child of s1[1];//new node
Hfmcoding[i][0] = hfmcoding[s1[0]][0] + hfmcoding[s1[1]][0];//The weight of the new node is the sum of the right values of the children
}
}
Find the smallest node of weight with a parent zero
Private int[] Select (int w) {
TODO auto-generated Method Stub
int s[] = {-1,-1}, J = 0;//S1 minimum weight with zero-parent node ordinal, I is a loop variable
int min1 = 32767, min2 = 32767;
for (j = 0; J < W; J + +) {
if (hfmcoding[j][1] = = 0) {//(node with zero parent) is found only in nodes that have not yet constructed a two-fork tree
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 () {//Huffman code based on Huffman tree
int n = charsandweight.length;
int I, F, C;
String hcodestring = "";
HCS = new String[n];
for (i = 0; i < n; i++) {//According to Huffman tree to find Huffman code
c = i;
hcodestring = "";
f = hfmcoding[i][1]; f Havermann The root node of the tree
while (f!= 0) {//sequential until root node
if (hfmcoding[f][2] = = c) {//Handle 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) {//String display encoding
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;
}
Havermann Encoding Reverse compilation
public string recoding (string s) {
String text = "";//back-compiled characters
int k = 0, M = hfmcoding.length-1;//start Query from 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 = value of hfmcoding[k][2];//k is the ordinal number of the left child of the root node
if (hfmcoding[k][2] = = 0 && hfmcoding[k][3] = = 0)//judge is not a leaf node, conditions (left and right children are 0)
{
Text + = (char) charsandweight[k][0];
k = m;
}
}
if (c[i] = = ' 1 ') {
K = hfmcoding[k][3];//k value is the number of the right child on the root node
if (hfmcoding[k][2] = = 0 && hfmcoding[k][3] = = 0)//judge is not a leaf node, conditions (left and right children are 0)
{
Text + = (char) charsandweight[k][0];
k = m;
}
}
}
return text;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.