Data structure (Java language description) Huffman coding

Source: Internet
Author: User

Principle: Huffman code is based on the given weight as a leaf node, generate a Huffman tree, and then make the smallest weight.

The first generation has given the weight of all the leaf nodes, and then take all nodes in the smallest and second small node as the left and right children to generate a Huffman tree, calculate the weight of the parent node into the given weight of the forest, and the previous minimum and minor nodes removed from the forest, and then find the smallest and lesser nodes in various forests to generate weight tree .... Until eventually there was only one tree left.

The nodes of Huffman tree are indicated by the following nodes:

(right-heavy, left-to-child, parent-node, and then set an identifier to flag whether the node has been put into Huffman tree)

Package tree;
/********** Huffman tree Node class description *********/
public class Huffmannode {
private int weight;//Weights
private int flag;//is added to the Huffman tree
Relationship between the nodes of private huffmannode parent,lchild,rchild;//tree species
Public Huffmannode () {//Construct empty nodes
This (0);
}
public huffmannode (int weight) {//constructs an empty node with a weighted value only
This.weight=weight;
flag=0;
Parent=lchild=rchild=null;
}
public void setweight (int weight) {
This.weight=weight;
}
public int getweight () {
return weight;
}
public void Setflag (int flag) {
This.flag=flag;
}
public int Getflag () {
return flag;
}
public void SetParent (Huffmannode parent) {
This.parent=parent;
}
Public Huffmannode GetParent () {
return parent;
}
public void Setlchild (Huffmannode lchild) {
This.lchild=lchild;
}
Public Huffmannode Getlchild () {
return lchild;
}
public void Setrchild (Huffmannode rchild) {
This.rchild=rchild;
}
Public Huffmannode Getrchild () {
return rchild;
}
}

The structure of Huffman tree:

Using the given weights array to construct Huffman tree, first of all to give each weight to generate Huffman node substitution (sign for put Huffman Tree falg=0);

Calculate how many nodes will be in the final Huffman tree, by the leaf node is n, then the summary point is 2*n-1;

So the next access to n-1 nodes (subscript: n--m-1); First select the two minimum and minor points comparison (set a comparison function for all nodes in the HN traversal, locate and then set flag=1, and then find the smallest node in HN), and ultimately constitute the entire Huffman tree

Then the leaf node begins to read the Huffman code:

Package tree;
public class Huffmantree {
Public int[][] Huffmancoding (int[] w) {
int n=w.length;
int m=2*n-1;
Huffmannode[] Hn=new huffmannode[m];//generates an array of elements for Huffman tree nodes
int i;
for (i=0;i<n;i++) {
Hn[i]=new Huffmannode (W[i]);//generation of Huffman tree leaf nodes
}
for (i=n;i<m;i++) {
Huffmannode min1=selectmin (hn,i-1);
Min1.setflag (1);
Huffmannode min2=selectmin (hn,i-1);//The flag found above is marked as 1, so it will not be searched again
Min2.setflag (1);//Find the least-weighted node in all nodes join to Haffman
Hn[i]=new Huffmannode ();
Min1.setparent (Hn[i]);
Min2.setparent (Hn[i]);
Hn[i].setlchild (min1);
Hn[i].setrchild (min2);
Hn[i].setweight (Min1.getweight () +min2.getweight ());//Modify the parent node of the two nodes, and the weights
}
Int[][] Huffcode=new int[n][n];//allocating n character encoded storage space
for (int j=0;j<n;j++) {//array
int start=n-1;

From the beginning, the subscript of each node of the Huffcode array is filled by the back forward and the prefix of the encoding is guaranteed to be the same as XX ... huffcode[0][6]=1 huffcode[0][5]=0

Then Huffcode is 0000 0001 that is small Mark 1 of a Huffman node Huffman slowed
For (Huffmannode c=hn[j],p=c.getparent ();p!=null;c=p,p=p.getparent ()) {
if (P.getlchild (). Equals (c)) huffcode[j][start--]=0;
else huffcode[j][start--]=1;
}//a path to the same node.
huffcode[j][start--]=-1;//to the remaining path is padding-1
}//traversal of N-leaf node paths
Return huffcode;//returns Huffman encoded array
}
Private Huffmannode selectmin (huffmannode[] Hn,int end) {
Huffmannode Min=hn[end];
for (int i=0;i<=end;i++) {
Huffmannode H=hn[i];
if (H.getflag () ==0&&h.getweight () <min.getweight ())
Min=h;
}
return min;
}

public static void Main (string[] args) {
Int[] w={23,11,5,3,29,14,7,8};
Huffmantree t=new Huffmantree ();
Int[][] Hn=t.huffmancoding (w);
System.out.println ("Hover full Code:");
for (int i=0;iSystem.out.print (w[i]+ ":");
for (int j=0;jif (hn[i][j]==-1) {//value output has encoded bits of path (after several in n bits)
for (int k=j+1;kSystem.out.print (Hn[i][k]);
Break
}
}
System.out.println ();

}
}
}

Results:

The hover full code is:
23:01
11:001
5:11,111
3:11,110
29:10
14:110
7:1110
8:000

Huffman Tree for:

Data structure (Java language description) Huffman coding

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.