欢迎来到天天文库
浏览记录
ID:38658997
大小:12.09 KB
页数:5页
时间:2019-06-17
《哈弗曼编码程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#include#defineN8#defineM2*N-1typedefstruct{floatweight;intlchild,rchild,parent;}codenode;typedefcodenodehuffmantree[M];typedefstruct{charch;intstart;charbits[N+1];}code;typedefcodehuffmancode[N];voidselect(huffmant
2、reeHT,intk,int*p1,int*p2){inti;floatsmall1=10000,small2=10000;for(i=0;i<=k;i++){if(HT[i].parent==-1)if(HT[i].weight3、{inti;for(i=0;i4、p1;HT[i].rchild=p2;HT[i].weight=HT[p1].weight+HT[p2].weight;}}voidhuffmancoding(huffmantreeHT,huffmancodeHC)//编码{inti,c,p;for(i=0;i=0){HC[i].start--;if(HT[p].lchild==c)HC[i].bits[HC[i].start]='0';elseHC[i].bits[5、HC[i].start]='1';c=p;p=HT[p].parent;}}}voidhuffmandecoding(huffmantreeHT,huffmancodeHC){intj=0,p;chars[100];unsignedinti=0;scanf("%s",&s);charch[N+1];while(i6、h[j]=HC[p].ch;j++;i++;}ch[j]=' ';puts(ch);}intmain(){inti,j,k=0;charch[]={'A','B','C','D','E','F','G','H'};huffmantreeHT;huffmancodeHC;printf("已知%d个字符为",N);for(i=0;i<=N;i++){printf("%c",ch[i]);HC[i].ch=ch[i];}printf("");creathfcode(HT);huffmancoding(HT,HC7、);printf("字符哈夫曼编码为:");for(i=0;i
3、{inti;for(i=0;i4、p1;HT[i].rchild=p2;HT[i].weight=HT[p1].weight+HT[p2].weight;}}voidhuffmancoding(huffmantreeHT,huffmancodeHC)//编码{inti,c,p;for(i=0;i=0){HC[i].start--;if(HT[p].lchild==c)HC[i].bits[HC[i].start]='0';elseHC[i].bits[5、HC[i].start]='1';c=p;p=HT[p].parent;}}}voidhuffmandecoding(huffmantreeHT,huffmancodeHC){intj=0,p;chars[100];unsignedinti=0;scanf("%s",&s);charch[N+1];while(i6、h[j]=HC[p].ch;j++;i++;}ch[j]=' ';puts(ch);}intmain(){inti,j,k=0;charch[]={'A','B','C','D','E','F','G','H'};huffmantreeHT;huffmancodeHC;printf("已知%d个字符为",N);for(i=0;i<=N;i++){printf("%c",ch[i]);HC[i].ch=ch[i];}printf("");creathfcode(HT);huffmancoding(HT,HC7、);printf("字符哈夫曼编码为:");for(i=0;i
4、p1;HT[i].rchild=p2;HT[i].weight=HT[p1].weight+HT[p2].weight;}}voidhuffmancoding(huffmantreeHT,huffmancodeHC)//编码{inti,c,p;for(i=0;i=0){HC[i].start--;if(HT[p].lchild==c)HC[i].bits[HC[i].start]='0';elseHC[i].bits[
5、HC[i].start]='1';c=p;p=HT[p].parent;}}}voidhuffmandecoding(huffmantreeHT,huffmancodeHC){intj=0,p;chars[100];unsignedinti=0;scanf("%s",&s);charch[N+1];while(i6、h[j]=HC[p].ch;j++;i++;}ch[j]=' ';puts(ch);}intmain(){inti,j,k=0;charch[]={'A','B','C','D','E','F','G','H'};huffmantreeHT;huffmancodeHC;printf("已知%d个字符为",N);for(i=0;i<=N;i++){printf("%c",ch[i]);HC[i].ch=ch[i];}printf("");creathfcode(HT);huffmancoding(HT,HC7、);printf("字符哈夫曼编码为:");for(i=0;i
6、h[j]=HC[p].ch;j++;i++;}ch[j]=' ';puts(ch);}intmain(){inti,j,k=0;charch[]={'A','B','C','D','E','F','G','H'};huffmantreeHT;huffmancodeHC;printf("已知%d个字符为",N);for(i=0;i<=N;i++){printf("%c",ch[i]);HC[i].ch=ch[i];}printf("");creathfcode(HT);huffmancoding(HT,HC
7、);printf("字符哈夫曼编码为:");for(i=0;i
此文档下载收益归作者所有