



全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
#includestdio.h#includestdlib.h#includestring.h#includemalloc.h#define N 20#define M 2*N-1#define Min 1000 /*最小值*/typedef structchar ch; /*权值对应的字符*/int weight; /*权值*/int parent; /*双亲位置*/int Lchild; /*左孩子位置*/int Rchild; /*右孩子位置*/HTNode,HuffmantreeM+1;char hcN+1N+1;void OutputHuffmancode(Huffmantree ht, int n);void CrtHuffmancode(Huffmantree ht, int n);void OutputHuffmantree(Huffmantree ht,int n);void select(Huffmantree ht,int a,int *s1,int *s2)int i;int c1=Min;int c2;for(i=1;i=a;i+) if (ht.parent=0&ht.weightc1) *s1=i; c1=ht.weight; c2=Min;for(i=1;i=a;i+) if (ht.parent=0&(*s1!=i)&c2ht.weight) *s2=i; c2=ht.weight; void CrtHuffmantree(Huffmantree ht,int w,char elem,int n)int i;int m;int s1,s2;m=2*n-1;ht=(HTNode *)malloc(m)*sizeof(HTNode);for(i=1;i=n;i+) ht.ch=elemi-1; ht.weight=wi-1; ht.parent=0; ht.Lchild=0; ht.Rchild=0; for(i=n+1;i=m;i+) ht.ch=0; ht.weight=0; ht.parent=0; ht.Lchild=0; ht.Rchild=0; /*初始化完毕*/for(i=n+1;i=m;i+) select( ht,i-1,&s1,&s2); /*返回最小值和次小值的位置*/ ht.weight=hts1.weight+hts2.weight; hts1.parent=i; hts2.parent=i; ht.Lchild=s1; ht.Rchild=s2;/*建立树完毕*/ OutputHuffmantree( ht,m); printf(now begin crthuffman coden); CrtHuffmancode( ht, n); printf(crthuffman code endn); OutputHuffmancode(ht, n);void OutputHuffmantree(Huffmantree ht,int n) int i; printf(nnumber-weight-parent-Lchild-Rchild-huffman charn n); for(i=1;i=n;i+) printf(%dt%dt%dt%dt%dt%cn,i,ht.weight,ht.parent,ht.Lchild,ht.Rchild,ht.ch);void CrtHuffmancode(Huffmantree ht, int n) /*建立编码*/ int i,c,p; int start; char *cd; cd=(char *) malloc(n+1)*sizeof(char); memset(cd,0,sizeof(cd); for(i=1;i=n;i+) start=n; cdstart=0; c=i; p=ht.parent; while(p!=0) start-; if(htp.Lchild=c) cdstart=0; else cdstart=1; c=p; p=htp.parent; /cdstart = 0; printf(cd is %sn start is %dn, cd+start, start); sprintf(hc, %s, cd+start); /*将已存在的编码复制到code中*/ free(cd);void OutputHuffmancode(Huffmantree ht,int n) int i; printf(nweight_char-weight-huffmancoden n);for(i=1;i=n;i+) printf( %ct%4dt%sn,ht.ch,ht.weight,hc); int main()int n = 6;/*记录了权值个数*/
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年年产12万吨直纺差别化工业丝项目节能评估报告
- 2025年中国热转移用聚酯薄膜项目投资计划书
- 上海市人民医院呼吸科实验室技师晋升实验师科研能力考核
- 石家庄市人民医院脑功能监测支持考核
- 朔州市人民医院头颈部神经监测考核
- 中国氟氢酸项目创业计划书
- 鄂尔多斯市中医院血管外科论文写作能力考核
- 中国三水醋酸钠项目商业计划书
- 牡丹江市中医院药物治疗方案评估考核
- 中国己二腈项目商业计划书
- 教育培训机构师资力量共享协议
- 工业机器人基础中职完整全套教学课件
- 2025年港股通开通25道测试题及答案
- 物业三轮清扫车管理制度
- DB42∕T 2303-2024 森林碳汇计量监测技术规范
- 《材料的回收》教案-2025-2026学年冀人版(2024)小学科学三年级上册
- 小朋友捕鱼游戏活动方案
- 2025年上海市中考语文真题(解析版)
- 生态水利工程概论课件
- 牛奶的理化指标
- 农村惠农政策课件
评论
0/150
提交评论