哈夫曼树的编码和译码
来源:互联网 发布:微信开发 php java 编辑:程序博客网 时间:2024/05/01 16:38
<pre name="code" class="cpp">#include<stdio.h>#define maxnum 1000.0#define maxsize 100typedef struct{ char ch; float weight; int lchild,rchild,parent;}hufftree;typedef struct{ char coder[maxsize]; int start; char c;}huffcode;hufftree tree[maxsize];huffcode code[maxsize];void huffman(hufftree tree[],int m,int n){ int i,j; int p1,p2; float small1,small2; for(i=0;i<m;i++) { tree[i].lchild=-1; tree[i].parent=0; tree[i].rchild=-1; tree[i].weight=0.0; } printf("请输入前%d个结点的字符及权值,中间用空格隔开\n",n); for(i=0;i<n;i++) { printf("输入第%d个字符为和权值",i+1); getchar(); scanf("%c %f",&tree[i].ch,&tree[i].weight); } for(i=n;i<m;i++) { p1=0;p2=0; small1=small2=maxnum; for(j=0;j<i;j++) if(tree[j].parent==0) if(tree[j].weight<small1) { small2=small1; small1=tree[j].weight; p2=p1; p1=j; } else if(tree[j].weight<small2) { small2=tree[j].weight; p2=j; } tree[p1].parent=i; tree[p2].parent=i; tree[i].lchild=p1; tree[i].rchild=p2; tree[i].weight=tree[p1].weight+tree[p2].weight; }}void huffmancode(hufftree tree[],huffcode code[],int m,int n){ int i,j,c,p; huffcode temp; for(i=0;i<n;i++) { temp.start=n; temp.c=tree[i].ch; c=i; p=tree[i].parent; while(p!=0) { temp.start--; if(tree[p].lchild==c) temp.coder[temp.start]='0'; else temp.coder[temp.start]='1'; c=p; p=tree[p].parent; } code[i]=temp; } printf("输出每个字符的哈夫曼编码:\n"); for(i=0;i<n;i++) { printf("%c: ",code[i].c); for(j=code[i].start;j<n;j++) printf("%c",code[i].coder[j]); putchar('\n'); }}void transcode(hufftree tree[],int m,int n){ int i,j=0; char codes[maxsize]; char end='e'; i=m-1; printf("输入发送的编码(以'e'为结束标志):"); //gets(codes); scanf("%s",codes); printf("译码后的字符为"); while(codes[j]!='e') { if(codes[j]=='0') i=tree[i].lchild; else i=tree[i].rchild; if(tree[i].lchild==-1) { printf("%c ",tree[i].ch); i=m-1; } j++; }}int main(){int n=maxsize;int m=maxsize; printf(" 哈夫曼的编码以及译码\n"); printf("输入字符个数:"); scanf("%d",&n); printf("________________________________________\n"); m=2*n-1; huffman(tree,m,n); huffmancode(tree,code,m,n); printf("________________________________________\n"); transcode(tree,m,n); return 0;}
0 0
- 哈夫曼树的编码和译码
- 汉明码的编码和译码
- 哈夫曼树的构造,编码,译码
- 哈夫曼树的编码与译码
- 电文的编码和译码(哈夫曼树的应用)
- 优先级队列实现哈夫曼树的编码和译码
- 哈弗曼编码和译码
- 哈夫曼编码和译码
- 哈夫曼编码和译码
- 哈夫曼树构建,编码,译码的实现------数据结构
- C语言:哈夫曼树的编码与译码
- 哈夫曼树的编码与译码(优化)
- 《哈夫曼编码的实现和应用(编码和译码)》
- huffman编码和译码实现
- 哈弗曼编码和译码.cpp
- 卷积码编码和BCJR译码
- 高手急救::建立哈夫曼树和哈夫曼编码,最好还带译码的算法...求例
- 哈夫曼编码和译码(老师给的样题样解)
- Xcode高级调试技巧(1)
- Java解压缩ZIP文件
- 关于自己笔记本上的无线网卡可设置wifi热点的二三事儿!
- DP总结
- 英语数学词汇大全
- 哈夫曼树的编码和译码
- HDU-A+B Problem 的Java题解 用Java语言做ACM的注意事项
- c语言数据结构实现后缀表达式求值
- HDOJ 4548 美素数
- 【LeetCode从零单刷】Convert Sorted List to Binary Search Tree
- Sprintf--powerful function
- [温故而知新] 《深度探索c++对象模型》——对象方法成员
- 编译原理(六) LL(1)文法分析法(分析过程的C++实现)
- ListView学习