写的程序原先可以用,但现在出现了下面的错误,查了其他的帖,但看不太懂,希望有大神帮下忙
以下是代码:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
class MyTreeNode
{
public:
char date;
float weight;
bool isleaf;
MyTreeNode *LNode;
MyTreeNode *RNode;
MyTreeNode(char date,float weight,bool isleaf)
{
this->date=date;
this->weight=weight;
this->isleaf=isleaf;
this->LNode=NULL;
this->RNode=NULL;
}
void child(MyTreeNode *LNode,MyTreeNode *RNode)
{
this->LNode=LNode;
this->RNode=RNode;
}
};
MyTreeNode *create_h_tree(int count,char *nr,float *weight)
{
vector<MyTreeNode*> forest;
for(int i=0;i<count;i++)
{
MyTreeNode *temp=new MyTreeNode(nr[i],weight[i],true);
forest.push_back(temp);
}
for(int i=0;i<count-1;i++)
{
MyTreeNode *A=NULL;
MyTreeNode *B=NULL;
int index=0;
float min=999999;
for(int j=0;j<count-1;j++)
{
MyTreeNode *temp=forest[j];
if(temp->weight<min)
{
min=temp->weight;
A=temp;
index=j;
}
}
forest.erase(forest.begin()+index);
index=0;
min=999999;
for(int j=0;j<count-i-1;j++)
{
MyTreeNode *temp=forest[j];
if(temp->weight<min)
{
min=temp->weight;
B=temp;
index=j;
}
}
forest.erase(forest.begin()+index);
MyTreeNode *newTree=new MyTreeNode('#',A->weight+B->weight,false);
newTree->LNode=A;
newTree->RNode=B;
forest.push_back(newTree);
}
return forest[0];
}
void gencode(MyTreeNode *root,string code)
{
if(root==NULL)
return;
if(root->isleaf)
{
cout<<root->date<<":"<<code<<endl;
}
else
{
gencode(root->LNode,code+"0");
gencode(root->RNode,code+"1");
}
}
void decode(MyTreeNode *root,int count,int *code)
{
MyTreeNode *curr=root;
int i=0;
while(i<count)
{
if(curr->isleaf)
{
cout<<curr->date;
curr=root;
continue;
}
int c=code[i];
i++;
if(c==0)
{
curr=curr->LNode;
}
else
{
curr=curr->RNode;
}
}
cout<<curr->date<<endl;
}
int main()
{
const int count=4;
char nr[count]={'A','B','C','D'};
float weight[count]={7,5,2,4};
MyTreeNode *root=create_h_tree(count,nr,weight);
cout<<"哈弗曼编码为:======================="<<endl;
gencode(root,"");
const int length=10;
int code[length]={1,0,1,1,1,0,0,1,1,0};
cout<<"=====================要解码的序列为:"<<endl;
for(int i=0;i<length;i++)
{
cout<<code[i];
}
cout<<endl;
cout<<"解码结果为:";
decode(root,length,code);
cout<<"====================================="<<endl;
return 0;
}