According to the weights of each leaf node, the optimal tree is constructed, and the Havermann encoding consisting of 0 and 1 is determined according to the minimum band full path.
First constructs a tree element in a wrapper class tnode, such as weights, left node, right node, and constructs Havermann encoded value in Tnode class, and determines whether the node is a leaf node
PackageHoffman Code;/** * * @authorLuckid *@version2014_10_11 **//*** Define Huffman encoded nodes*/ Public classTnode {/** * * @paramweight *@paramLeft *@param Right*/ /*protected*/ intWeight//Weighted ValueTnode left=NULL;//left nodeTnode right=NULL;//Right NodeString code=NULL;//Hoffman Code/*** Constructs an instance of a Huffman encoding node. Setting left and right subtrees and weights*/ PublicTnode (intWeight,tnode Left,tnode right) { This. weight=weight; This. left=Left ; This. right=Right ; } /** * * @authorLiu Yanbing as * @date 2015-02-08 1943 **/ PublicTnode (intweight) { This. weight=weight; } /*** Determine if this node is a leaf node *@returnBoolean is the leaf node returns true*/ Public Booleanisleaf () {return(left==NULL&&right==NULL); } /*** Returns the weight of this node *@returnThe weight of NT Ben node*/ Public intgetweight () {returnweight; } /*** Return to the left child node of this node *@returnTnode left child node*/ Publictnode GetLeft () {returnLeft ; } /*** Return to the right child node of this node *@returnTnode Right child node*/ Publictnode getRight () {returnRight ; } /*** Set the Huffman encoding of the node *@paramstr String is set by the Huffman encoding*/ Public voidSetcode (String str) {code=str; } /*** Get the Huffman Code of the node *@returnString is set by the Huffman encoding*/ PublicString GetCode () {returnCode; } }
In the tree class, build an optimal tree. First read the weights from the keyboard, here with 1,3,5,7 as the test data, the reading of the test data is placed in an array linked list. Then using the iterator to find the smallest leaf node, and then recursive construction of an optimal tree, in the construction of the optimal tree, a recursive need to find the minimum weight of the leaf node and the weight of the sub-small leaf node, two nodes constitute a subtrees optimal tree. Proceed recursively until only one data is left in the list of arrays, as the root node of the entire optimal tree.
The optimal subtree for the test data composition should be as follows:
/*** Define optimal binary tree*/ /*protected*/Tnode Root;//two fork root node /*protected*/list<integer>leaflist=NewArraylist<integer> ();//weights for storing leaf nodes /*protected*/list<tnode>templist=NewLinkedlist<tnode> ();//temporary queue for the node to be combined /*protected*/Tnode[]leafarr=NULL;//To store a band-weighted node /*** Keyboard read each weight value*/ Public voidgetleafweight () {Scanner SC=NewScanner (system.in); System.out.println ("Please enter the weights for each leaf node and press 0 for the end:"); while(Sc.hasnextint ()) {intI=Sc.nextint (); if(i==0) Break; Leaflist.add (NewInteger (i)); } SC=NULL; return ; } /*** Find the node in the temporary queue with the least weight removed from the queue and return *@returntnode node with the lowest weight value*/ Publictnode min () {Iterator<tnode>iter=Templist.iterator (); Tnode Minnode=Iter.next (); intMin=minnode.getweight ();//Find the smallest nodetnode Tempnode; while(Iter.hasnext ()) {Tempnode=Iter.next (); if(Tempnode.getweight () <min) {min=tempnode.getweight (); Minnode=Tempnode; }} templist.remove (Minnode); //the smallest node moves out of the staging queue//dispose of rubbishIter=NULL; Tempnode=NULL; returnMinnode; } /*** Create leaf nodes based on weights and join temporary queues*/ Public voidMakeleafnode () {Leafarr=Newtnode[leaflist.size ()]; for(intI=0;i<leaflist.size (); i++) {tnode node=Newtnode (Leaflist.get (i). Intvalue ()); Leafarr[i]=node; Templist.add (node); } } /*** Construct optimal two-fork tree based on weight value*/ Public voidMakebesttree () {makeleafnode (); //Create a leaf node based on weights and join a temporary queueTnode minnode1=NULL; Tnode MinNode2=NULL; Tnode node=NULL; while(Templist.size ()!=1) {//constructing the optimal treeminnode1=min (); MinNode2=min (); Node=NewTnode (Minnode1.getweight () +minnode2.getweight (), minnode1,minnode2); Templist.add (node); } Root=node; }
If the optimal tree is constructed, the optimal binary tree can be extended by Huffmancode class to realize Havermann coding. Recursively call the Huff () method in the Huffmancode class to create a Havermann tree structure and assign a value to each leaf node, where the left dial hand tree is assigned a value of 0 and the right subtree is assigned a value of 1
PackageHoffman Code;/** * * @authorLiu Yanbing as * @date 2015-02-08 1943 **//*** Expand the optimal binary tree Implementation Huffman code * Huffman encoding Class*/ Public classHuffmancodeextendstree{ PublicHuffmancode () {init (); } /*** Initialize node values and construct optimal binary tree*/ Public voidinit () {Super. Getleafweight (); Super. Makebesttree (); } /*** Generate Huffman encoded recursive function * *@paramT Tnode current traversal node *@params String currently traversed to this Huffman encoding*/ /*protected*/ Public voidHuff (tnode t,string s) {if(T.isleaf ()) {T.setcode (s); } Else{Huff (t.left,s+ "0"); Huff (T.right,s+ "1"); } } /*** Generate Huffman encoded external calling function*/ Public voidMakehuffmancode () {Huff (root,""); } /*** View all Huffman encoded values*/ Public voidViewhuffmancode () { for(inti=0;i<leafarr.length;i++) {System.out.println (leafarr[i].weight+":"+Leafarr[i].getcode ()); } } }
Test class:
package Huffman code; public class application { * * @author Liu Yanbing as * @date 2015-02 -08 1943 * */ public static void main (String args[]) {// TODO auto-generated method stub HUFFM Ancode hu=new Huffmancode (); Hu.makehuffmancode (); Hu.viewhuffmancode (); }}
The test results are as follows:
Java Implementation Havermann encoding