欢迎来到天天文库
浏览记录
ID:43391981
大小:315.40 KB
页数:28页
时间:2019-10-01
《哈夫曼树小论文》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、哈夫曼树小论文哈夫曼树及其编码#include#include#include#include#defineLEN8#defineMAXLEAF6〃最大叶子结点数目#defineMAXNODE(MAXLEAF*2)・1typedeffloatElemType;typedefstructcode*//*thisstructurestorestheinformationof/*存放编码的起始位置右至左(高位至/*存放huffman编码*/intstart;低位)
2、*/intbit[LEN];}HCode;typedefHCodeHuffCode[MAXLEAF];typedefstruct/*huffmantree结点的结构*/{intparent;intLChild;intRChild;ElemTypeweight;}HNode;typedefHNodeHuffman[MAXLEAF*2-1];voidcreateHuffmanTree(Huffmanh,intleaves,ElemType*weight){inti,j;for(i=0;i3、antree*/{(h+i)->parent=-l;(h+i)->LChild=-l;(h+i)->RChild=-l;(h+i)->weight=O;}for(i=0;iweight=*(weight+i);}/*上一个循环叶子已经带权,下面这个循环用來生成新根*新根数量为n-1*/for(i=0;i4、_pos=0;/*ml存放最小的权值对应下标m2存放次小的权值对应下标*/for(j=0;jweightparent==・1){m2=m1;m1=(h+j)->weight;m2_pos=ml_pos;m1_pos=j;}elseif((h+j)->weightparent==-1){m2=(h+j)->weight;m2_pos=j;(h+leaves+i)->parent=-1;//生成新根,无双亲・1(h+leaves+i)->LC5、hild=ml_pos;〃新根左孩了在数组中的下标(h+leaves+i)->RChild=m2_pos;//新根右孩子在数组屮的下标(h+ml_pos)->parent=leaves+i;//原根的父亲位置(h+m2_pos)->parent=leaves+i;//原根的父亲位置(h+leaves+i)->weight=m2+ml;}}voidhuffmancode(Huffmanh,HuffCodecode,intleaves){inti,j,p,c;HCodehf;/*从叶子结点开始向上冋溯从而计算出huffmancode*/f6、or(i=0;i7、p].LChild==c){hf.bit[hf.start]=O;}elsehf.bit[hf.start]=l;—hf.start;C=p;p=h[c].parent;}for(j=hf.start+1;j8、ntleaves){inti;for(i=0;i9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
3、antree*/{(h+i)->parent=-l;(h+i)->LChild=-l;(h+i)->RChild=-l;(h+i)->weight=O;}for(i=0;iweight=*(weight+i);}/*上一个循环叶子已经带权,下面这个循环用來生成新根*新根数量为n-1*/for(i=0;i4、_pos=0;/*ml存放最小的权值对应下标m2存放次小的权值对应下标*/for(j=0;jweightparent==・1){m2=m1;m1=(h+j)->weight;m2_pos=ml_pos;m1_pos=j;}elseif((h+j)->weightparent==-1){m2=(h+j)->weight;m2_pos=j;(h+leaves+i)->parent=-1;//生成新根,无双亲・1(h+leaves+i)->LC5、hild=ml_pos;〃新根左孩了在数组中的下标(h+leaves+i)->RChild=m2_pos;//新根右孩子在数组屮的下标(h+ml_pos)->parent=leaves+i;//原根的父亲位置(h+m2_pos)->parent=leaves+i;//原根的父亲位置(h+leaves+i)->weight=m2+ml;}}voidhuffmancode(Huffmanh,HuffCodecode,intleaves){inti,j,p,c;HCodehf;/*从叶子结点开始向上冋溯从而计算出huffmancode*/f6、or(i=0;i7、p].LChild==c){hf.bit[hf.start]=O;}elsehf.bit[hf.start]=l;—hf.start;C=p;p=h[c].parent;}for(j=hf.start+1;j8、ntleaves){inti;for(i=0;i9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
4、_pos=0;/*ml存放最小的权值对应下标m2存放次小的权值对应下标*/for(j=0;jweightparent==・1){m2=m1;m1=(h+j)->weight;m2_pos=ml_pos;m1_pos=j;}elseif((h+j)->weightparent==-1){m2=(h+j)->weight;m2_pos=j;(h+leaves+i)->parent=-1;//生成新根,无双亲・1(h+leaves+i)->LC
5、hild=ml_pos;〃新根左孩了在数组中的下标(h+leaves+i)->RChild=m2_pos;//新根右孩子在数组屮的下标(h+ml_pos)->parent=leaves+i;//原根的父亲位置(h+m2_pos)->parent=leaves+i;//原根的父亲位置(h+leaves+i)->weight=m2+ml;}}voidhuffmancode(Huffmanh,HuffCodecode,intleaves){inti,j,p,c;HCodehf;/*从叶子结点开始向上冋溯从而计算出huffmancode*/f
6、or(i=0;i7、p].LChild==c){hf.bit[hf.start]=O;}elsehf.bit[hf.start]=l;—hf.start;C=p;p=h[c].parent;}for(j=hf.start+1;j8、ntleaves){inti;for(i=0;i9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
7、p].LChild==c){hf.bit[hf.start]=O;}elsehf.bit[hf.start]=l;—hf.start;C=p;p=h[c].parent;}for(j=hf.start+1;j8、ntleaves){inti;for(i=0;i9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
8、ntleaves){inti;for(i=0;i9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
9、i].parent);printf(”LChild=%・3d“,h[i].LChild);printf(nRChild=%-3dH,h[i].RChild);voidprinthuffcode(HuffCodehcode,charcharacters[])inti,j;for(i=0;i10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
10、){printf("%c的huffman编码:",charactersfi]);for(j=hcode[i].start;j
此文档下载收益归作者所有