An algorithm for Huffman coding

Source: Internet
Author: User

An algorithm for Huffman coding

Refer to the book "Data structure (c language Edition)" published by Tsinghua University Press and implemented in Java

//Data structureClass huffmannode{ Public intWeight//Weight     Public intParent,lchild,rchild;//parent node, child node subscript position in array     Public Huffmannode(intWeightintParentintLchild,intRchild) { This. Weight = weight; This. parent = parent; This. lchild = Lchild; This. rchild = Rchild; }}/** * @param str_weight_arr array of stored word Fu Quan values * @param number of n characters * @return Calculated Huffman encoding * */     PublicStringhuffmancoding(int[] Str_weight_arr,intN) {if(n<1|| Str_weight_arr.length <n)return NULL;//The number of nodes of the Huffman tree to be constructed is twice times minus 1 of the number of leaf nodes        intNode_num = n2-1;//Create Huffman tree node, array size is node_num+1, we start with subscript 1huffmannode[] Huffmantree =Newhuffmannode[node_num+1];//Put the value in Str_weight_arr into the first n nodes, the first n elements are the leaf nodes of the Huffman tree        The remaining elements are the locations of non-leaf nodes, the ones we need to dynamically add below         for(intI=1; i<=n;i++) Huffmantree[i] =NewHuffmannode (Str_weight_arr[i],0,0,0);//Initialize the remaining elements         for(inti=n+1; i<=node_num;i++) Huffmantree[i] =NewHuffmannode (0,0,0,0);//Establish a Huffman tree, starting with the element labeled N+1, and building the subtree one by one. I's child nodes are the least weighted from 1 to i-1.        //Two nodes, when the traversal is finished, the element labeled Node_num is the root node of the Huffman tree         for(inti=n+1; i<=node_num;i++) {//In the Huffmantree between 1 and i-1 to find the lowest weight and no parent node of the two elements subscript            int[] Child_arr = Selectmintwo (Huffmantree,1, I-1);//Judging under Child_arr non-null            if(Child_arr = =NULL)return NULL;//Constructs a new subtree, the root node subscript is I, the left and right sub-tree position is child_arr[0],child_arr[1]Huffmantree[i].lchild = child_arr[0]; Huffmantree[i].rchild = child_arr[1]; Huffmantree[i].weight = huffmantree[child_arr[0]].weight +huffmantree[child_arr[1]].weight; huffmantree[child_arr[0]].parent = i; huffmantree[child_arr[1]].parent = i; }//end for        //At this point, the weight of these non-leaf nodes increases gradually from the position of subscript n+1 to Node_num.         ///next to generate Huffman encodingstring[] Huffmancode =Newstring[n+1];all elements in the//code_temp array are the Huffman encoding of a letter        Char[] Code_temp =New Char[n];//Huffmancode full, subscript starting from 1         for(intI=1; i<=n;i++) {intStart = n1;//What we require is the Huffman encoding of the given string, and the method passes in an array of weights corresponding to the character one by one in the string            //So, in order, start with each node, and look up until you find a node with 0 and 1 to record the path of the period            //From the leaf to the root of the reverse coding f==0 when the root node has been found             for(intC=1, f=huffmantree[i].parent;f!=0; c=f,f=huffmantree[f].parent) {The node of the//c position is not the left subtree of the F-position node, which is the right subtree of the F-position node, without running .                if(Huffmantree[f].lchild = = c) Code_temp[--start] =' 0 ';ElseCode_temp[--start] =' 1 '; }//The Huffman Code of the I-character is found, the first position of the code_temp array is truncated to the last character            //Assign value to Huffmancode[i]Huffmancode[i] = string.copyvalueof (code_temp, start, Code_temp.length-start); The String result =""; for(String s:huffmancode) result+=s;returnResult }Private int[]Selectmintwo(huffmannode[] Huffmantree,intBeginintEnd) {if(begin<1|| End >huffmantree.length)return NULL;intMAX1 = begin;intMAX2 = begin;intMax_weight = Huffmantree[begin].weight;//Find the lowest weight of the element where the subscript         for(inti=begin;i<=end;i++) {if(huffmantree[i].weight<max_weight && Huffmantree[i].parent = =0) Max1 = i; }//Looking for the second small weight of the element where the subscript         for(inti=begin;i<=end;i++) {if(huffmantree[i].weight<max_weight && Huffmantree[i].parent = =0&& max1! = max2) Max2 = i; }return New int[]{max1,max2}; }}

An algorithm for 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.