<span style="font-family:Arial, Helvetica, sans-serif;"> </span>
<span style="font-family:Arial, Helvetica, sans-serif;"></span><pre name="code" class="cpp">#include<iostream> #include<string> using namespace std; struct huffTree<span style="white-space:pre"> </span>//树上的节点 { int parent;<span style="white-space:pre"> </span>//父亲 int lchild;<span style="white-space:pre"> </span>//左孩子 int rchild;<span style="white-space:pre"> </span>//右孩子 int weight;<span style="white-space:pre"> </span>//权重 string flag;<span style="white-space:pre"> </span>//标志 }; struct Lowest_Node<span style="white-space:pre"> </span>//第0级节点的字符与频度(叶子节点,真正代表字母的节点) { char ch; int ch_num; };
<span style="white-space:pre"> </span> void coding(int length, huffTree tree[],int n,int &a,int &b) {<span style="white-space:pre"> </span><span style="font-family: Arial, Helvetica, sans-serif;">//确定每个字符的huffman编码,输出参数为a、b</span>
<span style="white-space:pre"><span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span>//调用的函数为 codeing(length,huff,k,min1,min2)</span></span>
<span style="white-space:pre"><span style="font-family:Arial, Helvetica, sans-serif;"><span style="white-space:pre"> </span>//用&引用 修改后得到min1,min2 为当前未遍历(看flag)节点中权值最小的两个节点的[i]值</span></span>
int i; int r,s; r=s=length;//节点个数最大不会超过字符串的长度 for(i=0;i<n;i++) { if((tree[i].weight<r)&&(tree[i].parent==-1)) { r=tree[i].weight; a=i; } } for(i=0;i<n;i++) { if((tree[i].weight<s)&&(i!=a)&&(tree[i].parent==-1)) { s=tree[i].weight; b=i; } } } //计算每个字符出现的频度并排序 void frequency(string str) <span style="white-space:pre"> </span>//这里new了一个Node[]数组,用Node[i].ch记录字符名,用Node[i].ch_num记录该字符出现的次数
<span style="white-space:pre"> </span>//and 一个 char_type记录出现过的字符种数 { int length=str.length();//长度 Lowest_Node *node=new Lowest_Node[length];//声明最0级节点 int i,j;//循环因子 for(i=0;i<length;i++) node[i].ch_num=0;//初始化频度 int char_type_num=0;//初始为0种字符 for(i=0;i<length;i++)//循环整个字符串 { for(j=0;j<char_type_num;j++) <span style="white-space:pre"> </span>//这个逻辑有点难得理解。。没出现过,跳出循环,选择在新的Node[i].ch里填上 if(str[i]==node[j].ch||(node[j].ch>='a'&&node[j].ch<='z'&&str[i]+32==node[j].ch)) break;//该字符没有出现过,跳出循环 if(j<char_type_num)//该字符重复出现,对应的记数器加1 node[j].ch_num++; else//新出现的字符,记录到ch[j]中,对应计数器加1 { if(str[i]>='A'&&str[i]<='Z') node[j].ch=str[i]+32; else node[j].ch=str[i]; node[j].ch_num++; char_type_num++;//字符的种类数加1 } } //按频度从大到小排序 for(i=0;i<char_type_num;i++) { for(j=i;j<char_type_num;j++)<span style="white-space:pre"> </span>//冒泡排序啦 { if(node[j].ch_num<node[j+1].ch_num)//如果前一个小于后一个,交换 { int temp;//临时频度 char ch_temp;//临时字符 temp=node[j].ch_num; ch_temp=node[j].ch; node[j].ch_num=node[j+1].ch_num; node[j].ch=node[j+1].ch; node[j+1].ch_num=temp; node[j+1].ch=ch_temp; } } } for(i=0;i<char_type_num;i++)//打印字符频度 cout<<"字符"<<node[i].ch<<"出现了"<<node[i].ch_num<<"次"<<endl; huffTree *huff=new huffTree[2*char_type_num-1];//此变量的声明需位于确定char_type_num值后 huffTree temp; string *code=new string[2*char_type_num-1];//存放各个字符的编码 for(i=0;i<2*char_type_num-1;i++)//节点初始化 { huff[i].lchild=-1; huff[i].parent=-1; huff[i].rchild=-1; huff[i].flag=-1;<span style="white-space:pre"> </span>//用-1 表示未遍历 } for(j=0;j<char_type_num;j++)//将排序后的第0级节点权重赋给树节点 { huff[j].weight=node[j].ch_num; } int min1,min2; for(int k=char_type_num;k<2*char_type_num-1;k++)//赋值0级之上的节点 { coding(length,huff,k,min1,min2); huff[min1].parent=k; huff[min2].parent=k; huff[min1].flag="0";<span style="white-space:pre"> </span>//由分支“0”访问 huff[min2].flag="1";<span style="white-space:pre"> </span>//由分支“1”访问 huff[k].lchild=min1; huff[k].rchild=min2; huff[k].weight=huff[min1].weight+huff[min2].weight; } for(i=0;i<char_type_num;i++) { temp=huff[i]; while(1) { code[i]=temp.flag+code[i]; temp=huff[temp.parent]; if(temp.parent==-1) break; } } cout<<"字符串的每个字符huffman编码为:"<<endl; for(i=0;i<char_type_num;i++) cout<<node[i].ch<<" "<<code[i]<<endl; cout<<"整个字符串的huffman编码为:"<<endl; for(i=0;i<length;i++) { for(j=0;j<char_type_num;j++) { if(str[i]==node[j].ch) cout<<code[j]; } } //释放内存 delete[] node; node=NULL; delete[] huff; huff=NULL; delete[] code; code=NULL; } int main() { int length=0;//字符串长度 string str; //目标字符串 cout<<"请输入一个字符串:"; cin>>str; frequency(str);//求各个元素的频度 return 0; }