赫夫曼树的代码 注意 我的代码都是在vs2010上运行通过的 在其他编译器上面不一定完全是好的 #define DEBU #include<iostream> using namespace std; typedef struct{ unsigned int weight; unsigned int parent,lchild,rchild; }HTNode,*HuffmanTree; typedef char * *HuffmanCode; typedef struct{ char text; int weight; }charWeight; int getLength(charWeight *cw) { #ifdef DEBUG //cout<<"求得cw长度为"<<cw[0].weight<<"/n"; #endif return cw[0].weight; } bool isDifferent(charWeight *cw,char x) { int i=1,j=getLength(cw); for(i=1;i<=j;i++) { if(x==cw[i].text) { #ifdef DEBUG cout<<"相同字符"<<x<<"/n"; #endif cw[i].weight++; return false; } } if(i>=j) { #ifdef DEBUG cout<<"发现新字符"<<x<<",添加到cw中"<<"/n"; #endif cw[j+1].text=x; cw[j+1].weight=1; cw[0].weight++; return true; } return -1; } void Select(HuffmanTree &HT, int i, int &s1, int &s2) { s1=0;s2=0;//初始 //while(HT[s1].parent!=0)s2=++s1; for(int num=1;num<=i;num++) { if(HT[num].parent==0&&s1==0)//parent==0进行比较 s1=num; else if(HT[num].parent==0&&s1!=0) if(HT[num].weight<HT[s1].weight) s1=num; } for(int num