Reprint: Huffman tree Construction and Huffman Coding (C + + code implementation)

Source: Internet
Author: User

Qiqifanqi Original: http://blog.csdn.net/qiqifanqi/article/details/6038822

#include <stdio.h>#defineMAX 100#defineMAXVALUE 500typedefstruct{      intweight; intParent,lchild,rchild;} Node /*Huffman tree Node type*/ /*-----------------------------The following sections define Huffman encoded storage structure-------------------------*/typedefstruct{    intStart//Store starting position    Charbits[Ten+1];//storing coded bit strings}codetype;typedefstruct{    CharSymbol//Store CharactersCodeType Code;} element;voidGetminelementlocal (node t[],intNumint*minlocal1,int*minLocal2);/*---------------------------------------------------------------------------------------------------*//*Build Huffman tree. n is the number of leaf nodes for constructing Huffman tree, the array w stores the weights of each leaf, and the array T stores the built Huffman Tree*/voidHuffmanintNintW[],node t[]) {    intm =0 ; intMinLocal1, MinLocal2; inti =0 ; Node* nodes =NULL; if(n = =0)return ; M=2*n-1 ;  for(i =1; I <= N; i + +) {T[i].weight=W[i]; T[i].lchild= T[i].parent = T[i].rchild =-1 ; }     for(; I <= m; i + +)) {T[i].weight=0 ; T[i].lchild= T[i].parent = T[i].rchild =-1 ; }     for(i =1; I <= N-1; i + +) {MinLocal1= MinLocal2 =-2 ; Getminelementlocal (t, n+i, &minlocal1, &minLocal2); T[n+i].weight = T[minlocal1].weight +T[minlocal2].weight; T[n+i].lchild =MinLocal1; T[n+i].rchild =MinLocal2; T[minlocal1].parent= n+i; T[minlocal2].parent= n+i; } }voidGetminelementlocal (node t[],intNumint*minlocal1,int*MinLocal2) {    inti =0 ; intTemp1 =MAXVALUE; intTemp2 =MAXVALUE; *minlocal1 = *minlocal2 =0 ;  for(i =1; i < num; i + +){        if(T[i].weight <= temp1 && t[i].parent = =-1) {Temp1=T[i].weight; *minlocal1 =i; }    }     for(i =1; i < num; i + +){        if(T[i].weight <= temp2 && t[i].parent = =-1&& I! = *minLocal1) {Temp2=T[i].weight; *minlocal2 =i; }    }}/*---------------------------------------------------------------------------------------------------*//*According to Huffman Tree, to find Huffman coding table. Tree Store Huffman tree, table store Huffman code table, n for Huffman Leaf number*/voidSethufcode (node tree[],element table[],intN) {intI,j,s,f;/*and F indicate the location of the child and the parent in the tree, respectively*/CodeType C;/*when storing the code*/     for(i=1; i<=n;i++)/*the code of the second leaf tree[i]*/{printf ("%d:", Tree[i].weight); C.start=n+1; S=i;/*Leaf Tree[i] began to trace*/             while(Tree[s].parent! =-1)/*So far back to the roots .*/{f=tree[s].parent; c.bits[--c.start]= (s = = Tree[f].lchild)?'0':'1'; S=F; }             for(J=c.start; j<n+1; j + +) printf ("%c", C.bits[j]); printf ("/ N"); Table[i].code=c;/*when encoding is copied to the final location*/              } }/*---------------------------------------------------------------------------------------------------*/voidMain () {node t[ -]; inti,n,w[ -]; Element m[ -]; printf ("Please enter a node number:"); scanf ("%d",&N);  for(i=1; i<=n;i++) {printf ("Please enter%d node weights:", i); scanf ("%d",&W[i]); }    /*Constructing Huffman Tree*/Huffman (N, W, T); printf ("number weight parents left child right child/n");/*Output Huffman Tree*/     for(i=1;i< (2*N); i++) printf ("%-5d%-5d%-5d%-5d%-5d/n", I,t[i].weight,t[i].parent,t[i].lchild,t[i].rchild); Sethufcode (T,m,n); /*construct and Output Huffman code table*/ } 

Reprint: Huffman tree Construction and Huffman Coding (C + + code implementation)

Related Article

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.