資源描述:
《huffman編碼c實(shí)現(xiàn)》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、#includestdio.h>#includestdlib.h>#includestring.h>#includemalloc.h>#defineN20#defineM2*N-1#defineMin1000/*最小值*/typedefstruct{??charch;/*權(quán)值對(duì)應(yīng)的字符*/??intweight;/*權(quán)值*/??intparent;/*雙親位置*/??intLchild;/*左孩子位置*/??intRchild;/*右孩子位置*/}HTNode,Huffmantree[M+1];charhc[N+1][N
2、+1];voidOutputHuffmancode(Huffmantreeht,intn);voidCrtHuffmancode(Huffmantreeht,intn);voidOutputHuffmantree(Huffmantreeht,intn);voidselect(Huffmantreeht,inta,int*s1,int*s2){??inti;??intc1=Min;??intc2;??for(i=1;i=a;i++)??{??if(ht.parent==0&&ht.weightc1)????{????*s1
3、=i;????c1=ht.weight;????}??}??c2=Min;??for(i=1;i=a;i++)??{??if(ht.parent==0&&(*s1!=i)&&c2>ht.weight)????{????*s2=i;????c2=ht.weight;????}??}}voidCrtHuffmantree(Huffmantreeht,intw[],charelem[],intn){??inti;??intm;??ints1,s2;??m=2*n-1;????ht=(HTNode*)malloc((m)*siz
4、eof(HTNode));????for(i=1;i=n;i++)??{??ht.ch=elem[i-1];??ht.weight=w[i-1];??ht.parent=0;??ht.Lchild=0;??ht.Rchild=0;??}??for(i=n+1;i=m;i++)??{??ht.ch='