哈夫曼树的编码与译码(优化)
来源:互联网 发布:魅族旗舰店淘宝 编辑:程序博客网 时间:2024/05/21 23:57
上次写的哈夫曼,这星期需要写哈夫曼的课程设计,就将之前写的哈夫曼树先优化了一下。
#include<stdio.h>#include<stdlib.h>#include<string.h>int n;int m;char h[100];//输入的字符串char whole_str[100];struct tree//树的结构{ char a; int weight; int parent; int Lchild; int Rchild;}ht[];struct coder//叶子编码{ char s; int data; char code[10];}hc[];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]); hc[i].s =ht[i].a ; hc[i].data =ht[i].weight ; } printf("\n将输入的字符串进行编码每个字母对应的权值和编码:\n"); for(i=1;i<=m;i++) printf("%c---%d--%s\n",hc[i].s ,hc[i].data ,hc[i].code ); printf("\n整个字符串的编码:\n"); for(i=0;(size_t)i<strlen(h)+1;i++) { for(j=1;j<m+1;j++) { if(h[i]==hc[j].s ) { printf("%s",hc[j].code ); strcat(whole_str,hc[j].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;(size_t)i<=strlen(whole_str);i++) { if(flag==1) { cd[10]="\0"; } k++; flag=0; cd[k]=whole_str[i]; cd[k+1]='\0'; for(j=1;j<=m;j++) { if(strcmp(hc[j].code,cd)==0 )//与结构体中的编码进行比较 { printf("%c",hc[j].s ); flag=1; k=-1; break; } } } printf("\n");}creat()//建树{ struct tree *ht; struct coder *hc; int i,j=1,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); hc[0].s =h[0]; hc[0].data =1; for(i=1;(size_t)i<strlen(h);i++) { if(h[i]=='\0') break; flag=0; for(k=0;k<=j;k++) { if(hc[k].s ==h[i]) { hc[k].data ++; flag=1; } } if(flag==0) { hc[j].s =h[i]; hc[j].data =1; j++; } } printf("\n"); m=j; n=2*m-1; for(i=0;i<m;i++)//初始化哈夫曼树 { ht[i+1].a =hc[i].s ; ht[i+1].weight =hc[i].data ; 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);.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
- 哈夫曼树的编码与译码(优化)
- 哈夫曼树的编码与译码
- C语言:哈夫曼树的编码与译码
- 哈夫曼树的构造,编码,译码
- 哈夫曼树的编码和译码
- 哈弗曼编码与译码的问题
- 指令的编码与译码原理
- 哈夫曼编码/译码的设计与实现
- 数据结构:哈夫曼树,哈夫曼编码与译码系统
- 哈夫曼树编码与译码解码运用
- 行程编码与译码
- 编码与译码-计算机网络
- 哈弗曼编码与译码
- 哈夫曼编码与译码
- 霍夫曼编码与译码
- 哈夫曼编码与译码
- Huffman编码与译码
- 汉明码编码与译码
- iOS- 全方位解析.crash文件崩溃报告
- Java线程篇(二):线程同步(synchronized和volatile)
- JSON
- 使用js将long类型转化为"yyyy-MM-dd hh:mm:ss"日期格式&jQuery自带日历控件的汉化教程
- 是程序员就去Leetcode刷题吧
- 哈夫曼树的编码与译码(优化)
- RAID磁盘阵列学习笔记
- LoadRunner字符串比较的常见错误
- 每日一vim(15)折叠
- 年鉴注册
- 2015年终总结
- sbt,scala,spark配置问题
- 如何解决iOS中更改系统定位权限时候系统设置崩溃的问题
- Eclipse 项目中的包路径变成文件夹目录形式了