molilover
molilover
采纳率100%
2020-05-08 22:34

萌新刚入场,求大神看看到底是什么引起的错误

已采纳

写的程序原先可以用,但现在出现了下面的错误,查了其他的帖,但看不太懂,希望有大神帮下忙

图片说明

以下是代码:

 #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;
}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • weixin_42446592 weixin_42446592 1年前

    ......
    for(int j=0;j {
    MyTreeNode *temp=forest[j];
    if(temp->weight {
    min=temp->weight;
    A=temp;
    index=j;
    }
    }
    //问题出在这里,forest的size通过这句在减,最后导致越界了,这个例子里,forest的的初始size为4,,但是当forest的size=2时,有个过程:j=2,sizieof(forest)=2,时候导致MyTreeNode *temp=forest[j]中的j为2,forest[2]越界;
    forest.erase(forest.begin()+index);
    ......

    点赞 1 评论 复制链接分享