哈夫曼树的编码与译码

来源:互联网 发布:霍华德生涯最好数据 编辑:程序博客网 时间:2024/05/21 20:08

哈夫曼树的编码与译码;
任意输入一段字符串对其进行编码与译码。
在计数这里用一个结构体数组,我觉得这样定义在对哈夫曼树赋值时比较方便,但是有同学问有没有更好方法,暂时没有想出来。

#include<stdio.h>#include<stdlib.h>#include<string.h>int n;int m;int l;int x;char h[100];char g[100];char v[100];struct tree{    char a;    int weight;    int parent;    int Lchild;    int Rchild;}ht[];struct coder{    int data;    char code[10];}hc[];struct  str{    char s;    int r;}b[100];void select(struct tree ht[],int k,int *s1,int *s2){       int i;    for(i=1; i<=k ; ++i)    {         if(ht[i].parent == 0)             break;    }      *s1 = i;      for(i=1; i<=k; ++i)    {          if(ht[i].parent==0 && ht[i].weight<ht[*s1].weight)              *s1 = i;      }      for(i=1; i<=k; ++i)    {          if(ht[i].parent==0 && i!=*s1)              break;      }      *s2 = i;      for(i=1; i<=k; ++i)    {          if(ht[i].parent==0 && i!=*s1 && ht[i].weight<ht[*s2].weight)              *s2 = i;      }   }coding(struct tree ht[],struct coder hc[])//编码{    int i,j,k,start,c,p;    char *cd;    cd=(char *)malloc(m*sizeof(char));    cd[n-1]='\0';    for(i=1;i<=m;i++)    {        start=n-1;        c=i;        p=ht[i].parent;        while(p!=0)        {            --start;            if(ht[p].Lchild ==c)                cd[start]='0';            else                cd[start]='1';            c=p;            p=ht[p].parent ;        }        strcpy(hc[i].code,&cd[start]);    }    printf("\n将输入的字符串进行编码每个字母对应的权值和编码:\n");    for(i=1;i<=m;i++)        printf("%c---%d--%s\n",ht[i].a ,ht[i].weight ,hc[i].code  );    printf("\n整个字符串的编码:\n");    for(i=0;i<x+1;i++)    {        for(j=0;j<l+1;j++)        {            if(h[i]==g[j])            {                for(k=0;k<m+1;k++)                {                    if(ht[k].a ==g[j])                    {                                                   printf("%s",hc[k].code );                        strcat(v,hc[k].code );                    }                }            }        }    }    printf("\n");}encoding(struct tree ht[],struct coder hc[])//译码{    int i,j,k=-1,flag=0;    char cd[10]={'\0'};//建立数组,将待翻译的字符串存入    printf("\n对比进行译码:\n");    for(i=0;i<=strlen(v);i++)    {               if(flag==1)        {            cd[10]="\0";                                }        k++;        flag=0;        cd[k]=v[i];        cd[k+1]='\0';        for(j=1;j<=m;j++)        {               if(strcmp(hc[j].code,cd)==0 )//与结构体中的编码进行比较            {                printf("%c",ht[j].a );                flag=1;                k=-1;                break;            }        }    }    printf("\n");}creat()//建树{    struct tree *ht;    struct coder *hc;    int i,j=1,t,k,s1,s2,flag;    ht=(struct tree*)malloc((n+1)*sizeof(struct tree));     hc=(struct coder*)malloc((n+1)*sizeof(struct coder));    printf("请输入字符串:");    gets(h);    g[0]=h[0];    b[0].s =h[0];    b[0].r =1;      for(i=1;i<100;i++)    {        x++;        if(h[i]=='\0')            break;        flag=0;        for(k=0;k<=j;k++)        {            if(b[k].s ==h[i])            {                b[k].r ++;                flag=1;            }        }        if(flag==0)        {            t=j;            b[t].s =h[i];            b[t].r =1;            g[++l]=h[i];            j++;        }    }    printf("\n");    m=j;    n=2*m-1;    for(i=0;i<m;i++)//初始化哈夫曼树    {        ht[i+1].a =b[i].s  ;        ht[i+1].weight =b[i].r ;        ht[i+1].parent =0;        ht[i+1].Lchild =0;        ht[i+1].Rchild =0;    }    for(i=m+1;i<n+1;i++)    {        ht[i].a ='#';        ht[i].weight =0;        ht[i].parent =0;        ht[i].Lchild =0;        ht[i].Rchild =0;    }    printf("初始化:\n");    for(i=1;i<n+1;i++)        printf("%2d. %3c  %3d  %3d  %3d  %3d  \n",i,ht[i].a  ,ht[i].weight ,ht[i].parent ,ht[i].Lchild ,ht[i].Rchild) ;    for(i=m+1;i<n+1;i++)//对应的字符串建立哈夫曼树    {        select(ht,i-1,&s1,&s2);        ht[i].weight =ht[s1].weight +ht[s2].weight ;        ht[i].Lchild =s1;        ht[i].Rchild =s2;        ht[s1].parent =i;        ht[s2].parent =i;    }    printf("\n进行编码:"\n);    for(i=1;i<n+1;i++)        printf("%2d. %3c  %3d  %3d  %3d  %3d  \n",i,ht[i].a  ,ht[i].weight ,ht[i].parent ,ht[i].Lchild ,ht[i].Rchild) ;    coding(ht,hc);//编码    encoding(ht,hc);//译码}main(){    creat();}

运行结果
初始化

这里写图片描述

这里写图片描述

这次写的比较乱,会再修改。

0 0
原创粉丝点击