诛仙归云怎么破防?:求助有关哈夫曼树的问题!急!满意的答案再加100分!

来源:百度文库 编辑:高校问答 时间:2024/05/04 19:58:14
紧急求助哈夫曼树的问题,请各位帮帮忙!谢谢了!
问题如下(注意不是静态数组的方法):
1、用动态生成结点的方法构造哈夫曼树,按先根序列输出您生成的哈夫曼树的结点的权值。
2、用您构造哈夫曼树的程序,给26个英文字母编码,26个英文字母的权可按如下方法给出:在任一英文字典中,该字母开始的单词的数量占总单词量的比(不必很准确)。
这两个问题是放在一个程序里头的。

麻烦各位给我编一下,越快越好,谢谢!!满意的答案我会再追加100分!

#include <iostream>
#include <string>
using namespace std;
typedef struct{
char lett;
int freq;
int parent,left,right;
}ELEM,*HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree,int,int&,int&); //find min
void Create(HuffmanTree &ht,int *frq,char *s,int n)
{
int i,m;
int s1,s2;
HuffmanTree p;
m=2*n-1; //create 2*n-1 nodes for n charactors
ht=(HuffmanTree)malloc((m+1)*sizeof(ELEM));//assign memory
for(p=ht+1,i=1;i<=n;i++,p++,frq++,s++) {//initialize huffman trees
p->lett=*s;
p->freq=*frq;
p->parent=p->left=p->right=0;
}
for(i=n+1;i<=m;++i,++p){
p->freq=p->parent=p->right=p->left=0;
}
for(i=n+1;i<=m;++i)
{
Select(ht,i-1,s1,s2); // find the minvalue fromHT[1...i-1],s1,s2
ht[s1].parent=ht[s2].parent=i;
ht[i].freq=ht[s1].freq+ht[s2].freq;
ht[i].left=s1;
ht[i].right=s2;
}
}
void Coding(HuffmanTree ht,HuffmanCode &hc,int n){
int i,c,f,start;
char *cd;
hc=new char *[n+1];
cd=new char [n];
cd[n-1]='\0';
for(i=1;i<=n;i++){
start=n-1;
~Hw犦t*??OJ鉯丌??唉A?}@P基痘 x-,?
0报~BM?$?\?k47@[R徊囋8P槜隖胎堌歕qj銴8閺&覀?馗齨齀[n?q倚?野L蓦%?#W:鹇I痝?竖妆脡8 狚o蚒?
?[ i?? 駪z?A潅栞/? ]荒灧%Hn鹉f猜[??:C陌J?*u?肻?1C7= 鳇?袅迻?h?v唏矠并??a率楲?E曑*埳pY搈0 哔70r?XqBN? 3H
X裥TW
2r8V
]Az皌┿镳贞v???F锍答匴$ZWN牊6储[F??璪袓黼k吊嘅 H?缳? Am倓?虳閺?拧宼}秓出??C鈢?4繸彸?貃?萡费`Y?厾~8??峘?Jb総2h罘貘*?搀&o惃
?'N雦覔馦]H淳R堰,?朴噍3R9(i磛?q?X+еX集?]P泡i谴伄[c豩?T掜s#?g.?)靺?5罋冚hO忖g???幭婷劂嵳??鲁馐7, O?U?挘 榎q旹j棘7??禄囔? 枽a4厡?n<?痖?录詧<N?懩籱貥fb?_Tl?C??錂螤?謯骂|?=-OK駆p羏o膦湾捣PMyl鉨Z}?AZCC0研"?霯L髪x\?锑3?駉?萯::?娢>i衏S儭沧R禉Ty?崭拐郆臐/幨橾4鏔輂⑵C*u:4镬4煂f?邖&8尌?Hk嚤*鴓?x愀?伊 檤哑&.歖7嵤?$?鐦$r4/k?_??=e?)麽
疣軎4?,'儹怆Y蒙磈社=壭€ fc勌q朿?踾缣?x钷虪傜槩?Q6肉悓u緐??锹 (:頼昑?(M良o?爅Ke?憅霷qQ6W际?M??h???稄緜阗嚷鸿覮Uび?l==覙惭?临殒p???(蛧rL黬艨K斓鴓;?Z ?.?J椅峥蠢昲?弃铐?鹇4瞉翵l髎@嫸莁 
A郦蓦贲;寳?T ?鲖?坰[?ㄖ伦咇叕H头瓜)?N"?xt?榴% xC<陌?]?A X壕鬓d靸?@?G-:傩€熔+?7$帿蓟tX6貚#H昍搄< H哯Z h@鵟^?!S疡2Jzh7E孎睟墨駺薚Z9A^穯??,?5?у灾骛3婎V伺巛怱? a€"O狸?儏桥?oo?S?A??训 ck^??緺 \I/g_L魪覵闼L?跼Z毎踻?鋴n处?莹蛬? ^?藤 ?盶毳狘f豲??8?1R觃旆 n鄤[A?w?调!鹾萧凖{?i ?)?涛?蜨X?9@謓?L€L滒?佃W€軭??柠澼J概EN?R?鄻泚Eb>7犊
}
free(cd);
}
void Select(HuffmanTree ht,int i,int&s1,int&s2){
unsigned int sm1 , sm2; // using for compare of the frequency
s1 = 1;
s2 = 1;
int m=0; // temp minimum frequency
for(m=1;m<=i;m++)
{
if(ht[m].parent!=0) continue;
else
{
sm1=ht[m].freq;
s1=m;
break;
}
}
for(int j=m+1;j<=i;j++)
{
if(ht[j].parent!=0) continue;
else
{
if(sm1>ht[j].freq)
{
sm1=ht[j].freq;
s1=j;
}
}
}
for(m=1;m<=i;m++)
{
if(ht[m].parent!=0) continue;
else
{
sm2=ht[m].freq;
s2=m;
if(s2==s1) continue;
else break;
}
}
for(int k=m+1;k<=i;k++)
{

if(ht[k].parent!=0) continue;
else
{
if((ht[k].freq<sm2)&&(k!=s1))
{
sm2=ht[k].freq;
s2=k;
}
}
}
}
void View(HuffmanTree ht,HuffmanCode hc, char *c,int n){
//view the code table
int i;
for(i=1;i<=n;i++){
cout<<ht[i].lett<<"---"<<hc[i]<<endl;
}
cout<<endl;
}
void Encode(HuffmanTree ht,HuffmanCode hc,string s,int n){
//make a string to codes
int m=s.length();
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(s.at(i)==ht[j+1].lett)
cout<<hc[j+1];
}
}cout<<endl;
}
void Decode(HuffmanTree ht,HuffmanCode hc,string s1,int n)
{
int k=s1.length();
int i=0,m;
while(i!=k){
m=2*n-1;
while(ht[m].left!=0&&ht[m].right!=0){
if(s1[i]=='0')m=ht[m].left;
else if(s1[i]=='1')m=ht[m].right;
i++;
}
cout<<ht[m].lett;
}
cout<<endl;
}
void line() {cout<<"--------------------------------------------------------------"<<endl;}
void showMenu(){
line();
cout<<"----------------------*Huffman Tree*--------------------------"<<endl;
cout<<"----------------designed by Zz小虾米 2006.5.20---------------"<<endl;
line();
cout<<" 1.Create Huffman Tree"<<endl;
cout<<" 2.View Huffman Tree"<<endl;
cout<<" 3.Encode String"<<endl;
cout<<" 4.Decoding"<<endl;
cout<<" 0.exit this program"<<endl;
line();
}
void showTip(){
line();
cout<<"done successfully---------------------select 0 to 4 to continue"<<endl<<"\x1A";
}

int main(){
HuffmanTree ht;
HuffmanCode hc;
int n; //number of charactor
int *frq;//frequency
string i="-1";
string s,s1;
char *c;
int k=1;
showMenu();
cout<<"select 0 to 4 above for operation!\nTree has not been initialized,you must choose '1'at first!"<<endl;
line();
cout<<"\x1A";
cin>>i;
while(i!="0")
{ if(i=="1")
{ system("cls");
showMenu();
cout<<"you have chosen 1,now it will create a tree"<<endl;
cout<<"input total charactor number:"<<endl;
line();
cout<<"\x1A";
cin>>n;
frq=new int [n];
c=new char [n];
for(int i=0;i<n;i++){
system("cls");
showMenu();
cout<<"input charactor "<<i+1<<endl;
line();
cout<<"\x1A";
cin>>c[i];
system("cls");
showMenu();
cout<<"input frequency "<<i+1<<endl;
line();
cout<<"\x1A";
cin>>frq[i];
}
Create(ht,frq,c,n); //create huffman tree
Coding(ht,hc,n); //coding
showTip();
}
else if(i=="2")
{system("cls");
showMenu();
cout<<"you have chosen 2,the code table is:"<<endl;
line();
View(ht,hc,c,n);
showTip();
}

else if(i=="3")
{system("cls");
showMenu();
cout<<"you have chosen 3,please in put a string for coding"<<endl;
cout<<"input the string"<<endl;
line();
cout<<"\x1A";
cin>>s;
cout<<"after encode the result is : "<<endl;
line();
Encode(ht,hc,s,n);
showTip();
}

else if(i=="4")
{system("cls");
showMenu();
cout<<"you have chosen 4,please input codes for decoding"<<endl;
cout<<"input the codes"<<endl;
line();
cout<<"\x1A";
cin>>s1;
cout<<"after decode the result is : "<<endl;
line();
Decode(ht,hc,s1,n);
showTip();
}
else {
system("cls");
showMenu();
cout<<"invalid input,please input again!"<<endl;
line();
cout<<"\x1A";
}
cin>>i;
system("cls");
}
return 0;
}

//说明:可以输入的字符和其频率动态构造Huffman树,还有菜单选项。数组也是动态分配内存的。

我也想看看有谁能解出来,好学习学习呀!~