哈夫曼编解码

本文介绍了一种基于哈夫曼编码与译码的方法,包括构建哈夫曼树、生成哈夫曼编码及实现编码与译码的过程。通过具体的C语言代码示例,详细解释了如何对英文电文进行编码和译码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、编码

【题目描述】

给定一篇用于通信的英文电文,统计该电文中每个字符出现的频率,按频率左小右大的方法为这些字符建立哈夫曼(Huffamn)树,并编出每个字符的哈夫曼树码,输出该电文的哈夫曼码译文。

 

【输入】

输入文件huffman.in是一篇用于通信的英文电文。

【输出】

输出文件huffman.out输出该电文的哈夫曼码译文。

 

【输入输出样例1】

huffman.in                             

huffman.out

aaccdddbacbcddddddd

011011000011101001100010001111111

【数据限制】

2<=英文电文字符数<=10000000

统计以上abcd出现的个数。

a:3   b:2    c:4    d:10

构造出哈夫曼树

a:011         b:010     c  :00          d:1


二、译码

【题目描述】

给定2个输入文件,第1个输入文件是用于通信的英文电文,统计该电文中每个字符出现的频率,按频率左小右大的方法为这些字符建立哈夫曼(Huffamn)树,并编出每个字符的哈夫曼树码;第2个输入文件是已经按第1个输入文件的英文电文编好的哈夫曼码,输出该哈夫曼码的对应的英文电文。

 

【输入】

第1个输入文件为huffman.in是用于通信的英文电文, 第2个输入文件codeToTxt.in是已经按第1个输入文件编好的哈夫曼码。

【输出】

输出文件codeToTxt.out输出codeToTxt.in文件内容的英文电文。

 

【输入输出样例1】

huffman.in                            

codeToTxt.in

codeToTxt.out

aaccdddbacbcddddddd

011111011000011101001100010001111

adddaccdddbacbcdddd

【数据限制】

2<=英文电文字符数<=10000000


C代码   收藏代码
  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. #include<string.h>  
  4. #include<conio.h>  
  5.   
  6. #define MAXNUM 60  
  7.   
  8. typedef struct  
  9. {  
  10.     char ch;  
  11.     int weight; //权值,这个字符出现的频率  
  12.     int parent;  
  13.     int left;  
  14.     int right;  
  15. }HuffNode;  
  16.   
  17. typedef struct  
  18. {  
  19.     char code[MAXNUM];  
  20.     int start;  
  21. }HuffCode;  
  22.   
  23. HuffNode ht[MAXNUM*2]; //存放哈夫曼树  
  24.   
  25. HuffCode hcd[MAXNUM];  //存放ht数组中对应的字符的编码  
  26.   
  27. int n;                 //字符的个数  
  28.   
  29. //初始化哈夫曼树ht  
  30. void initHt()  
  31. {  
  32.     FILE * fp;  
  33.     char ch;  
  34.     int i=0;  
  35.     //从文件document/character.txt中读出要编码的字符和权值  
  36.     if((fp=fopen("document/character.txt","r"))==NULL){  
  37.         printf("can not open the file character.txt");  
  38.         exit(0);  
  39.     }  
  40.     ht[i].left=ht[i].right=ht[i].parent=-1;  
  41.     while((ch=fgetc(fp))!=EOF){  
  42.         if(ch=='\n'){  
  43.             i++;  
  44.             ht[i].left=ht[i].right=ht[i].parent=-1;       
  45.         }  
  46.         else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))  
  47.             ht[i].ch=ch;  
  48.         else if(ch>='0'&&ch<='9')  
  49.             ht[i].weight=ht[i].weight*10+ch-'0';  
  50.     }  
  51.     n=i+1;  
  52.     if(fclose(fp)){  
  53.         printf("can not close the file character.txt");  
  54.         exit(0);  
  55.     }  
  56. }  
  57.   
  58. //构造哈夫曼树,看成有n棵树,选择权值最小的两棵树合并  
  59. void createHuffTree()  
  60. {  
  61.   
  62.     int i=0,k;  
  63.     int minI,minJ;  
  64.     int f=0;  
  65.     minI=minJ=-1; //minI<minJ  
  66.     for(k=n;k<2*n-1;k++){  
  67.         //寻找ht中权值最小且无父结点的两个结点  
  68.         i=0;  
  69.         f=0;  
  70.         while(ht[i].ch!='\0'){  
  71.             if(ht[i].parent==-1){  
  72.                 if(f==0){  
  73.                     minI=i;  
  74.                     f++;  
  75.                 }else if(f==1){  
  76.                     if(ht[i].weight<ht[minI].weight){  
  77.                         minJ=minI;  
  78.                         minI=i;  
  79.                     }else  
  80.                         minJ=i;  
  81.                     f++;  
  82.                 }else{  
  83.                     if(ht[i].weight<ht[minI].weight){  
  84.                         minJ=minI;  
  85.                         minI=i;  
  86.                     }else if(ht[i].weight<ht[minJ].weight)  
  87.                         minJ=i;  
  88.                 }  
  89.             }  
  90.             i++;  
  91.         }  
  92.         //合并两个结点  
  93.         ht[k].ch='#';  
  94.         ht[k].left=minI;  
  95.         ht[k].right=minJ;  
  96.         ht[k].weight=ht[minI].weight+ht[minJ].weight;  
  97.         ht[k].parent=-1;  
  98.         ht[minI].parent=ht[minJ].parent=k;  
  99.     }  
  100. }  
  101.   
  102. //将一个字符串反转  
  103. void reverse(char *str)  
  104. {  
  105.     int i,j;  
  106.     char ch;  
  107.     for(i=0,j=strlen(str)-1;i<j;i++,j--){  
  108.         ch=str[i];  
  109.         str[i]=str[j];  
  110.         str[j]=ch;  
  111.     }  
  112. }  
  113.   
  114. //哈夫曼编码,通过父节点从下往上找  
  115. void createHuffCode()  
  116. {  
  117.     int i,j,length;  
  118.     FILE * fp;  
  119.     for(i=0;i<n;i++){  
  120.         length=0;  
  121.         j=i;  
  122.         //给每个字符进行编码  
  123.         while(ht[j].parent!=-1){  
  124.             if(ht[ht[j].parent].left==j){  
  125.                 hcd[i].code[length++]=0+'0';  
  126.             }else  
  127.                 hcd[i].code[length++]=1+'0';  
  128.             j=ht[j].parent;  
  129.         }  
  130.   
  131.         hcd[i].start=hcd[i].code[length-1]-'0';  
  132.         hcd[i].code[length]='\0';  
  133.         reverse(hcd[i].code);  
  134.     }  
  135.     //把hcd字符编码写入文件document/code.txt中  
  136.     if((fp=fopen("document/code.txt","w"))==NULL){  
  137.         printf("can not open the file character.txt");  
  138.         exit(0);  
  139.     }  
  140.     for(i=0;i<n;i++){  
  141.         fputc(ht[i].ch,fp);  
  142.         fputs("    ",fp);  
  143.         fputs(hcd[i].code,fp);  
  144.         fputc('\n',fp);  
  145.     }  
  146.     if(fclose(fp)){  
  147.         printf("can not close the file character.txt");  
  148.         exit(0);  
  149.     }  
  150. }  
  151.   
  152. //哈夫曼解码,每次都从根节点开始搜索  
  153. int releaseHuffCode(char *str,char* code)  
  154. {  
  155.     int root=2*n-2;  
  156.     int length=0,i=0;  
  157.     while(code[i]){  
  158.         if(code[i]=='0'+0)  
  159.             root=ht[root].left;  
  160.         else if(code[i]=='0'+1)  
  161.             root=ht[root].right;  
  162.         else  
  163.             return 0;  
  164.         if(ht[root].left==-1 && ht[root].right==-1){  
  165.             str[length++]=ht[root].ch;  
  166.             root=2*n-2;  
  167.         }  
  168.         i++;  
  169.     }  
  170.     str[length]='\0';  
  171.     if(root==2*n-2)  
  172.         return 1;  
  173.     return 0;  
  174. }  
  175.   
  176. //用户输入编码字符  
  177. void encode()  
  178. {  
  179.     int i=0,j,f=1;  
  180.     char str[50];  
  181.     char code[500]={'\0'};  
  182.     printf("\n请输入要编码的字符串(length<50)\n");  
  183.     scanf("%s",str);  
  184.     while(str[i]){  
  185.         if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){  
  186.             for(j=0;j<n;j++)  
  187.                 if(str[i]==ht[j].ch){  
  188.                     strcat(code,hcd[j].code);  
  189.                     break;  
  190.             }  
  191.             i++;  
  192.         }else{  
  193.             f=0;  
  194.             break;  
  195.         }  
  196.     }  
  197.     if(f)  
  198.         puts(code);  
  199.     else  
  200.         printf("你输入的字符串错误!\n");  
  201.     printf("按任意键后重新选择!\n");  
  202.     getch();  
  203. }  
  204.   
  205. //用户输入解码字串  
  206. void  decode()  
  207. {  
  208.     char str[50];  
  209.     char code[500];  
  210.     printf("\n请输入要解码的字串(用0和1表示)\n");  
  211.     scanf("%s",code);  
  212.     if(releaseHuffCode(str,code))  
  213.         puts(str);  
  214.     else  
  215.         printf("你输入的字串错误!\n");  
  216.       
  217.     printf("按任意键后重新选择!\n");  
  218.     getch();  
  219. }  
  220.   
  221. //主函数  
  222. void main()  
  223. {  
  224.     int choice=1;  
  225.     initHt();  
  226.     createHuffTree();  
  227.     createHuffCode();  
  228.     while(choice){  
  229.        system("cls");   
  230.        printf("/****************哈夫曼编码与解码*********************/\n");  
  231.        printf(" 在document/character.txt 文件中存放着各个字母的权值\n");  
  232.        printf(" 程序从中读出各个字母的权值构造哈夫曼树并进行编码\n");  
  233.        printf(" 各个字符的编码存在document/code.txt文件中\n");  
  234.        printf("/*****************************************************/\n");  
  235.        printf("\n请输入你的选择:1 ---- 编码  2 ---- 解码  0 ---- 退出\n");  
  236.        scanf("%d",&choice);  
  237.        switch(choice){  
  238.        case 1:   
  239.            encode();  
  240.            break;  
  241.        case 2:   
  242.            decode();  
  243.            break;  
  244.        case 0:   
  245.            printf("谢谢使用!\n");  
  246.            break;  
  247.        default:  
  248.            choice=1;  
  249.            printf("你的输入错误!按任意键后重新输入!\n");  
  250.            getch();  
  251.            break;  
  252.        }  
  253.    }  
  254. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值