欢迎来到天天文库
浏览记录
ID:38476073
大小:28.50 KB
页数:3页
时间:2019-06-13
《静态存储哈弗曼》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、#include#include#includetypedefstruct{intweight;intparent,lchild,rchild;}HTnode,*Huffmantree;typedefchar**Huffmancode;voidselect(HuffmantreeHT,intk,int*s1,int*s2){//赫夫曼树HT中选paren为0且权值最小的两结点,s1,s2inti,j=1,min,e;while(HT[j].parent!=0)j
2、++;if(j>k){printf("无空闲结点!");exit(0);}elsemin=HT[j].weight;for(i=1;i<=k;i++)if((HT[i].weight3、4、j==*s1)j++;if(j>k){printf("无空闲结点!");exit(0);}elsemin=HT[j].weight;for(i=1;i<=k;i++)i5、f((HT[i].weight6、zeof(HTnode));//0号单元未用for(p=*HT+1,i=1;i<=n;i++,++p){(*p).weight=w[i];printf("HT[%d].weight=%d",i,(*p).weight);(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(;i<=m;i++,++p){(*p).weight=0;(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(i=n+1;i<=m;++i){select(*HT,7、i-1,&s1,&s2);(*HT)[s1].parent=i;(*HT)[s2].parent=i;(*HT)[i].lchild=s1;(*HT)[i].rchild=s2;(*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;printf("HT[%d]andHT[%d]-->HT[%d].weight",s1,s2,i,(*HT)[i].weight);}*HC=(Huffmancode)malloc((n+1)*sizeof(char*));char*cd;c8、d=(char*)malloc(n*sizeof(char));cd[n-1]=' ';printf("HuffmanTreeCodeisasfollows:");for(i=1;i<=n;++i){start=n-1;for(c=i,f=(*HT)[i].parent;f!=0;c=f,f=(*HT)[f].parent)if((*HT)[f].lchild==c)cd[--start]='0';elsecd[--start]='1';(*HC)[i]=(char*)malloc((n-start)*sizeof(9、char));strcpy((*HC)[i],&cd[start]);printf("HT[%d].weight=%dnode'sHuffmancodeis:%s",i,(*HT)[i].weight,(*HC)[i]);}free(cd);printf("");}voidmain(){HuffmantreeHT;HuffmancodeHC;intw[20],i,n;w[0]=0;printf("静态存储方式输入编码个数n=");scanf("%d",&n);printf("输入结点值:");for10、(i=1;i<=n;i++)scanf("%d",&w[i]);Huffmancoding(&HT,&HC,w,n);}
3、
4、j==*s1)j++;if(j>k){printf("无空闲结点!");exit(0);}elsemin=HT[j].weight;for(i=1;i<=k;i++)i
5、f((HT[i].weight6、zeof(HTnode));//0号单元未用for(p=*HT+1,i=1;i<=n;i++,++p){(*p).weight=w[i];printf("HT[%d].weight=%d",i,(*p).weight);(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(;i<=m;i++,++p){(*p).weight=0;(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(i=n+1;i<=m;++i){select(*HT,7、i-1,&s1,&s2);(*HT)[s1].parent=i;(*HT)[s2].parent=i;(*HT)[i].lchild=s1;(*HT)[i].rchild=s2;(*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;printf("HT[%d]andHT[%d]-->HT[%d].weight",s1,s2,i,(*HT)[i].weight);}*HC=(Huffmancode)malloc((n+1)*sizeof(char*));char*cd;c8、d=(char*)malloc(n*sizeof(char));cd[n-1]=' ';printf("HuffmanTreeCodeisasfollows:");for(i=1;i<=n;++i){start=n-1;for(c=i,f=(*HT)[i].parent;f!=0;c=f,f=(*HT)[f].parent)if((*HT)[f].lchild==c)cd[--start]='0';elsecd[--start]='1';(*HC)[i]=(char*)malloc((n-start)*sizeof(9、char));strcpy((*HC)[i],&cd[start]);printf("HT[%d].weight=%dnode'sHuffmancodeis:%s",i,(*HT)[i].weight,(*HC)[i]);}free(cd);printf("");}voidmain(){HuffmantreeHT;HuffmancodeHC;intw[20],i,n;w[0]=0;printf("静态存储方式输入编码个数n=");scanf("%d",&n);printf("输入结点值:");for10、(i=1;i<=n;i++)scanf("%d",&w[i]);Huffmancoding(&HT,&HC,w,n);}
6、zeof(HTnode));//0号单元未用for(p=*HT+1,i=1;i<=n;i++,++p){(*p).weight=w[i];printf("HT[%d].weight=%d",i,(*p).weight);(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(;i<=m;i++,++p){(*p).weight=0;(*p).lchild=0;(*p).parent=0;(*p).rchild=0;}for(i=n+1;i<=m;++i){select(*HT,
7、i-1,&s1,&s2);(*HT)[s1].parent=i;(*HT)[s2].parent=i;(*HT)[i].lchild=s1;(*HT)[i].rchild=s2;(*HT)[i].weight=(*HT)[s1].weight+(*HT)[s2].weight;printf("HT[%d]andHT[%d]-->HT[%d].weight",s1,s2,i,(*HT)[i].weight);}*HC=(Huffmancode)malloc((n+1)*sizeof(char*));char*cd;c
8、d=(char*)malloc(n*sizeof(char));cd[n-1]=' ';printf("HuffmanTreeCodeisasfollows:");for(i=1;i<=n;++i){start=n-1;for(c=i,f=(*HT)[i].parent;f!=0;c=f,f=(*HT)[f].parent)if((*HT)[f].lchild==c)cd[--start]='0';elsecd[--start]='1';(*HC)[i]=(char*)malloc((n-start)*sizeof(
9、char));strcpy((*HC)[i],&cd[start]);printf("HT[%d].weight=%dnode'sHuffmancodeis:%s",i,(*HT)[i].weight,(*HC)[i]);}free(cd);printf("");}voidmain(){HuffmantreeHT;HuffmancodeHC;intw[20],i,n;w[0]=0;printf("静态存储方式输入编码个数n=");scanf("%d",&n);printf("输入结点值:");for
10、(i=1;i<=n;i++)scanf("%d",&w[i]);Huffmancoding(&HT,&HC,w,n);}
此文档下载收益归作者所有