Data structure routines--Huffman tree

Source: Internet
Author: User

This paper is the basic Data Structure series (6): Tree and two-fork tree in the 15th class Huffman tree routines.

#include <stdio.h>#include <string.h>#define N //leaf knot points #define M 2*n-1 //Total number of nodes in tree //Huffman tree node structure typetypedef struct{CharData//Node value    DoubleWeight//Weight    intParent//Parent node    intLchild;//Left child knot point    intRchild;//Right child knot point} Htnode;//Each node Huffman encoded structure typetypedef struct{CharCd[n];//Store Huffman code    intStart;} Hcode;//Construction Huffman TreevoidCreateht (Htnode ht[],intN) {intI,k,lnode,rnode;Doublemin1,min2; for(i=0; i<2*n-1; i++)//All nodes related domain set initial value -1ht[i].parent=ht[i].lchild=ht[i].rchild=-1; for(I=n; i<2*n-1; i++)//Construction Huffman Tree{min1=min2=32767;//lnode and Rnode for two node positions with minimum weightlnode=rnode=-1; for(k=0; k<=i-1; k++)if(ht[k].parent==-1)//Find only in nodes that have not yet constructed a two-fork tree{if(ht[k].weight<min1)                    {min2=min1;                    Rnode=lnode;                    Min1=ht[k].weight;                Lnode=k; }Else if(ht[k].weight<min2)                    {min2=ht[k].weight;                Rnode=k;        }} ht[i].weight=ht[lnode].weight+ht[rnode].weight;        Ht[i].lchild=lnode;        Ht[i].rchild=rnode;        ht[lnode].parent=i;    ht[rnode].parent=i; }}//Implement Huffman codingvoidCreatehcode (Htnode Ht[],hcode hcd[],intN) {intI,f,c; Hcode HC; for(i=0; i<n; i++)//According to Huffman tree to find Huffman code{hc.start=n;        C=i; F=ht[i].parent; while(f!=-1)//sequential until root node{if(HT[F].LCHILD==C)//Handle left child nodehc.cd[hc.start--]=' 0 ';Else                    //Handle Right child nodehc.cd[hc.start--]=' 1 ';            C=f;        F=ht[f].parent; } hc.start++;//start Point Huffman encoding the first characterHCD[I]=HC; }}//Output Huffman codevoidDisphcode (Htnode Ht[],hcode hcd[],intN) {intI,k;Doublesum=0, m=0;intJprintf("Output Huffman code: \ n");//Output Huffman code     for(i=0; i<n; i++) {j=0;printf("%c:\t", Ht[i].data); for(K=hcd[i].start; k<=n; k++) {printf("%c", Hcd[i].cd[k]);        j + +;        } m+=ht[i].weight; Sum+=ht[i].weight*j;printf("\ n"); }printf("\ n Average length =%g\n",1.0*sum/m);}intMain () {intn=8I//n Indicates the number of initial strings    Charstr[]= {' A ',' B ',' C ',' d ',' E ',' F ',' G ',' h '};Doublefnum[]= {0.07,0.19,0.02,0.06,0.32,0.03,0.21,0.1};    Htnode Ht[m]; Hcode Hcd[n]; for(i=0; i<n;        i++) {Ht[i].data=str[i];    Ht[i].weight=fnum[i]; }printf("\ n");    Createht (Ht,n);    Createhcode (Ht,hcd,n); Disphcode (Ht,hcd,n);printf("\ n");return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure routines--Huffman tree

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.