实验一哈夫曼编码
《实验一哈夫曼编码》由会员分享,可在线阅读,更多相关《实验一哈夫曼编码(12页珍藏版)》请在装配图网上搜索。
1、实验一 哈夫曼编码n实验目的n掌握哈夫曼编码原理n理解数据压缩的实现过程 实验要求根据哈夫曼编码算法,对下列信源实现哈夫曼编码。求出编码后的平均码长。 05.0,10.0,15.0,20.0,25.0,25.0 6,5,4,3,2,1 XXXXXXX 实验内容与步骤1、实验内容n 对上面信源进行哈夫曼编码。n 显示出每个符号的分配代码及平均码长2、实验步骤(1)初始化,根据符号概率的大小按由大到小顺序对符号进行排序。 (2)把概率最小的两个符号组成一个节点。(3)重复步骤(1)(2),直到概率和为1(4)从根节点开始到相应于每个符号的“树叶”,概率大的标“0”,概率小的标“1”(5)从根节点开
2、始,对符号进行编码。 实验结果要求1、哈夫曼编码程序。2、实验报告(含流程图) #include #include #define N 15#define M 2*N-1typedef struct float weight;int parent,lchild,rchild;HTNode;typedef struct char data;float weight; char codeN;HTCode; 参考程序 void Init(HTCode hc,int *n) int i;printf(n input n=);scanf(%d,fflush(stdin); /*修改的地方*/printf
3、(n input %d character n,*n);for(i=1;i=*n;i+) printf(n the %d charactor is:,i);scanf(%c,fflush(stdin); /* 修改的地方*/for(i=1;i=*n;i+) printf(n the %d charactor weight is:,i);scanf(%f, 参考程序(续) void Select(HTNode ht,int k,int *s1,int *s2) int i;for(i=1;i=k;i+) if(hti.parent=0) *s1=i;break;for(i=1;i=k;i+) i
4、f(hti.parent=0参考程序(续) for(i=1;i=k;i+) if(hti.parent=0break;for(i=1;i=k;i+)if(hti.parent=0printf(n this is *s1:%dn,*s1); printf(n this is *s2:%dn,*s2); 参考程序(续) void HuffmanCoding(HTNode ht,HTCode hc,int n) char cdN;int i,j,m,c,f,s1,s2,start;m=2*n-1;for(i=1;i=m;i+) if(i=n) hti.weight=hci.weight;else h
5、ti.weight=0.0;hti.parent=hti.lchild=hti.rchild=0;for(i=n+1;i=m;i+) Select(ht,i-1,hts1.parent=i;hts2.parent=i; hti.lchild=s1;hti.rchild=s2;hti.weight=hts1.weight+hts2.weight; 参考程序(续) for(i=1;i=m;i+) printf(n the %d charactor weight is %f:,i,hti.weight);printf(n the %d charactor lchild is %d:,i,hti.lc
6、hild);printf(n the %d charactor rchild is %d:,i,hti.rchild);printf(n the %d charactor parent is %d:,i,hti.parent);for(i=0;iN;i+) cdi= ;cdn-1=0;for(i=1;i=n;i+) start=n-1;for(c=i,f=hti.parent;f;c=f,f=htf.parent) if(htf.lchild=c) cd-start=0; else cd-start=1;strcpy(hci.code, 参考程序(续) void main() int i,m,
7、n,wN+1;float l=0.0;float arg,*point=HTNode htM+1;HTCode hcN+1;Init(hc,HuffmanCoding(ht,hc,n);for(i=1;i=n;i+)printf(n the code of %c is :%s,hci.data,hci.code);for(i=1;i=n;i+) l+=strlen(hci.code)*hci.weight; /* 修改的地方*/* l+=strlen(hci.code); */ /* l=l/n; 修改的地方*/printf(n the average length of the huffman coding is %f,l); 参考程序(续)
- 温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。