哈夫曼编码译码
来源:互联网 发布:和平网络电视tv破解版 编辑:程序博客网 时间:2024/05/22 06:44
课程设计:
已知信源的概率分布如下
构造其二元的Huffman编码器和译码器。
以下是源程序,在VC6.0编译器下通过。
# include <stdio.h># include <string.h># include <iostream.h># define N 50 //哈夫曼树叶子结点数# define M 2*N-1 //树中结点数typedef struct{char data[5];double weight;int parent;int lchild;int rchild;}HTNode;typedef struct{char cd[N]; //存放哈夫曼码,N值为最大码长,可在定义宏中更改int start;}HCode;void CreateHT(HTNode ht[],int n) //构造哈夫曼树{int i,k,lnode,rnode;double min1,min2;for(i=0;i<2*n-1;i++)ht[i].parent=ht[i].lchild=ht[i].rchild=-1;for(i=n;i<2*n-1;i++){min1=min2=1.0;lnode=rnode=-1; //lnode和rnode为最小权重的两个结点位置for(k=0;k<i;k++) //在还没构造二叉树的结点中查找if(ht[k].parent==-1){if(ht[k].weight<min1){min2=min1;rnode=lnode;min1=ht[k].weight;lnode=k;}else if(ht[k].weight<min2){min2=ht[k].weight;rnode=k;}}ht[lnode].parent=i;ht[rnode].parent=i;ht[i].weight=ht[lnode].weight+ht[rnode].weight;ht[i].lchild=lnode;ht[i].rchild=rnode;}}void CreateHCode(HTNode ht[],HCode hcd[],int n) //构造哈夫曼编码{int i,f,c;HCode hc;for(i=0;i<n;i++){hc.start=n;c=i;f=ht[i].parent;while(f!=-1){if(ht[f].lchild==c) //处理左孩子hc.cd[hc.start--]='1';else //处理右孩子hc.cd[hc.start--]='0';c=f;f=ht[f].parent;}hc.start++;hcd[i]=hc;}}void TransHCode(HTNode ht[],HCode hcd[], int n) //译码{ int m=2*n-1; int i,j=0,k; char c[50]={'0','1','0','1','1','0','1','1','1','1','1','0','0'}; //初始化输入码,可更改,为'0,'1'级组合 cout<<"/t假设要译的码为 : "; while(c[j]!='/0') { cout<<c[j]; j++; } cout<<endl; j=0; //重置为0 cout<<"/t译码结果为 : "; while(c[j]!='/0') { i=m-1; while(ht[i].lchild!=-1&&ht[i].rchild!=-1) { if(c[j]=='0') i=ht[i].rchild; else if(c[j]=='1') i=ht[i].lchild; j++; } cout<<ht[i].data<<i+1<<" "; } cout<<endl<<endl<<endl;}void DispHCode(HTNode ht[],HCode hcd[],int n) //编码输出{int i,j,k;cout<<"/t信源 "<<"概率 "<<"编码"<<endl;for(i=0;i<n;i++){j=0;cout<<"/t "<<ht[i].data<<i+1<<" "<<ht[i].weight<<" ";for(k=hcd[i].start;k<=n;k++){cout<<hcd[i].cd[k];j++;}cout<<endl;}/******************************************************************************此处代码供调试用,可删除********************for(i=0;i<2*n-1;i++){cout<<"ht["<<i<<"].lchild "<<ht[i].lchild<<" ";cout<<"ht["<<i<<"].rchild "<<ht[i].rchild<<" ";cout<<"ht["<<i<<"].parent "<<ht[i].parent<<" ";cout<<"ht["<<i<<"].weight "<<ht[i].weight<<" "<<endl;}*************************************************************/cout<<endl;}void interface() //界面初始化{cout<<endl<<endl;cout<<"/t***********************************************************"<<endl; cout<<"/t****** 哈夫曼编码译码 ********"<<endl; cout<<"/t****** XXXXX大学 XXXXXXXXXX学院 XXXXXXX班 ********"<<endl; cout<<"/t****** 组成员: AAAA BBBB CCCC ********"<<endl; cout<<"/t****** 指导老师:TTTT ********"<<endl; cout<<"/t****** NNNN年N月N日 ********"<<endl; cout<<"/t***********************************************************"<<endl;cout<<endl<<endl;}void main(){interface(); //调用函数导入界面/***************************初始化***********************************/int n=6; //权值个数,此处也即信源概率个数,可根据情况更改//char *ch=new char[N];double fnum[]={0.32,0.22,0.18,0.16,0.08,0.04}; //权值(信源概率分布)HTNode ht[M];HCode hcd[N];for(int i=0;i<n;i++){strcpy(ht[i].data,"S");ht[i].weight=fnum[i];} /*********************************************************************/CreateHT(ht,n); //调用函数构造哈夫曼树CreateHCode(ht,hcd,n); //调用函数构造哈夫曼编码DispHCode(ht,hcd,n); //调用函数输出哈夫曼编码 TransHCode(ht,hcd,n); //调用函数译码}
- 哈夫曼编码译码
- 哈夫曼编码/译码
- 哈夫曼编码与译码
- 哈夫曼编码与译码
- 哈夫曼编码和译码
- 哈夫曼编码和译码
- 数据结构课程设计-哈夫曼编码译码
- 哈夫曼编码/译码(实验文档)
- 数据结构实验,哈夫曼编码/译码系统
- 哈夫曼编码译码(数据结构课程设计)
- C语言实现哈夫曼编码与译码
- 南邮 OJ 1022 哈夫曼编码与译码
- 数据结构:哈夫曼树,哈夫曼编码与译码系统
- C语言实现哈夫曼编码与译码
- 哈夫曼编码/译码的设计与实现
- 哈夫曼编码/译码系统(树应用)
- 哈夫曼编码/译码系统(树应用)
- 【数据结构】哈夫曼树及哈夫曼编码译码
- 微处理器寄存器结构
- 使用newrbe 神经网络模拟sin逼近
- 设置DOS环境变量
- 深入Linux网络核心堆栈
- DELPHI求乱序数组最小值快速算法(Delphi MMX优化算法应用之三)
- 哈夫曼编码译码
- 转载—SQL操作全集
- setenv怎么用
- MSSQL遍历数据表
- 全新wordpress博客,来这里体验!
- 使用Struts+Hibernate上传大对象(BLOB)
- Servlet笔记
- GetExitCodeThread判断线程是否执行完
- IIS下出现server application error的解决方法