哈夫曼编码
来源:互联网 发布:当网络写手挣钱吗 编辑:程序博客网 时间:2024/06/01 19:05
#include<stdio.h>#include<stdio.h>#include<string.h>#include<malloc.h>#define N 20#define M 2*N-1typedef struct{ double weight; char data[5]; int parent,lchild,rchild;}HTNode;typedef struct{ char cd[N]; int start;}HCode;void CreateHT(HTNode ht[],int n){ int i,j,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=32767; lnode=rnode=-1; for(k=0;k<=i-1;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[i].weight=ht[lnode].weight+ht[rnode].weight; ht[i].lchild=lnode;ht[i].rchild=rnode; ht[lnode].parent=i;ht[rnode].parent=i; }}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--]='0'; } else { hc.cd[hc.start--]='1'; } c=f; f=ht[f].parent; } hc.start++; hcd[i]=hc; }}void DispHCode(HTNode ht[],HCode hcd[],int n){int i,k;int sum=0,m=0,j;printf("哈夫曼编码:\n"); //for (i=0;i<n;i++){j=0;printf( "%s:",ht[i].data);for (k=hcd[i].start;k<=n;k++){printf("%c",hcd[i].cd[k]);j++;} printf("\n");}}int main(){ HTNode ht[M]; HCode hcd[N]; char *str[]={"a","b","c","d","e","f","g","h"}; int i,n=8; double no[]={0.07,0.19,0.02,0.06,0.32,0.03,0.21,0.10}; for (i=0;i<n;i++){ strcpy(ht[i].data,str[i]);ht[i].weight=no[i];}CreateHT(ht,n);CreateHCode(ht,hcd,n);DispHCode(ht,hcd,n);}
阅读全文
0 0
- 信源编码---哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 哈夫曼编码
- 嵌入式数据库架构特点
- win10如何修改锁屏(欢迎界面)的用户名
- C/C++中函数参数传递详解
- springboot入门(一)--快速搭建一个springboot框架
- 8-9-函数重载overload
- 哈夫曼编码
- deadLock
- Sublime配置Terminal插件
- 自己的第一篇博客
- 泛型编程与模板
- hadoop搭建之静态ip配置
- NOIP模拟(20171023)T2 一样远
- ubuntu16.04下使用matplotlib出现`TypeError: Couldn't find foreign struct converter for 'cairo.Context'`解决
- Swift