创建哈夫曼树并进行哈夫曼编码与哈夫曼译码

来源:互联网 发布:银联数据待遇怎么样 编辑:程序博客网 时间:2024/06/17 01:27
  • 图例
    这里写图片描述 这里写图片描述 这里写图片描述 这里写图片描述
    以上图例解释:
    这里写图片描述
    c语言实现代码:
#include<stdio.h>#include<malloc.h>#include<string.h>#define N 100#define M 2*N-1typedef struct{        int weight,parent,lchild,rchild;}hnode;void select(hnode htree[] ,int i,int *s1,int *s2)//在前i项中找到权值最小的结点将其下标给s1,s2 {     *s1=0;       while(htree[*s1].parent!=-1)//找到待扫描结点第一个parent为-1的结点作为最小基准点         (*s1)++;    for(int j=0;j<=i;j++)    {        if(htree[j].weight<htree[*s1].weight&&htree[j].parent==-1)          {          *s1=j;            }    }//找出权值最小的结点的下标;      if(*s1>=0&&*s1<i-1)//找到第二个 parent=-1的结点作为最为次小基准点        {       (*s2)=(*s1)+1;        while(htree[*s2].parent!=-1)        (*s2)++;      }     else       {       (*s2)=(*s1)-1;       while(htree[*s2].parent!=-1)        (*s2)--;       }    for(int j=0;j<=i;j++)    {        if(htree[j].weight<htree[*s2].weight&&j!=*s1&&htree[j].parent==-1)          *s2=j;    }         if(htree[*s1].lchild=-1&&htree[*s2].lchild!=-1)         {            int a=*s1;            int b=*s2;            *s1=b;            *s2=a;                     }}void create_haffman_tree(hnode htree[],int value[],int n){    //初始化前n个结点        for(int i=0;i<n;i++)    {           printf("%d ",value[i]);    }    for(int i=0;i<n;i++)       {       htree[i].weight=value[i];       htree[i].parent=htree[i].lchild=htree[i].rchild=-1;      }      //初始化后n-1个结点       for(int i=n;i<2*n-1;i++)              htree[i].weight=htree[i].parent=htree[i].lchild=htree[i].rchild=-1;       for(int i=n;i<2*n-1;i++)//构造后n-1个结点;       {            int s1,s2;           select(htree,i-1,&s1,&s2);           htree[i].weight=htree[s1].weight+htree[s2].weight;           htree[i].lchild=s1;           htree[i].rchild=s2;           htree[s1].parent=htree[s2].parent=i;       } }void haffman_encoding(hnode htree[],char *haffmancode[],int n){    char *cd;    int start,c,p;    cd=(char *)malloc(n*sizeof(char));    cd[n-1]='\0';//首先存放编码结束符;     for(int i=0;i<n;i++)     {        start=n-1;        c=i;        p=htree[i].parent;        while(p!=-1)        {            --start;            if(htree[p].lchild==c)             cd[start]='0';             else             cd[start]='1';            c=p;p=htree[p].parent;         }        haffmancode[i]=(char *)malloc((n-start)*sizeof(char));        strcpy(haffmancode[i],&cd[start]);     } } void haffmandecoding(hnode htree[],char *haffmancode[],int n,char string[]){    printf("请输入二进制码串:\n");    char s[n];    scanf("%s",s);        int flag=0;        char tem[n];        printf("译码后的文件:\n");    for(int i=0;i<strlen(s);i++)    {        tem[flag]=s[i];        tem[++flag]='\0';        for(int j=0;j<n;j++)        {           if(!strcmp(tem,haffmancode[j]))                 {                    printf("%c",string[j]);                    flag=0;                 }            if(j==n-1&&i==strlen(s)-1)            {                printf("无法完成译码!");                return ;             }             }    }}int main(void){    char string[N];    int value[N];     printf("请输入字符集:\n");    scanf("%s",string);    int n=strlen(string);    hnode htree[n*2-1];     char *haffmancode[n];//存放每个字符的哈夫曼编码的头指针。     printf("请输入字符集所对应的权值\n");    for(int j=0;j<n;j++)        scanf("%d",&value[j]);    //getlen(string)个字符,最后生成的哈夫曼树将有2*getlen(string)-1个结点;    create_haffman_tree(htree,value,n);    printf("\n");    for(int i=0;i<n*2-1;i++)     haffman_encoding(htree,haffmancode,n);    for(int j=0;j<n;j++)    printf("%c的哈夫曼编码为:%s\n",string[j],haffmancode[j]);    haffmandecoding(htree,haffmancode,n,string);}
  • 源代码下载链接:
    哈夫曼树的创建,哈夫曼编码哈夫曼译码C语言实现代码
    哈夫曼树的创建哈夫曼编码哈夫曼译码C语言实现代码markdown书写的html格式
  • 如果需要将文件编码或译码,则应该加入文件操作,将文件的字符读入。其权重应该为在整个文件中该字符所出现的次数。
原创粉丝点击