molilover 2020-05-08 22:34 采纳率: 100%
浏览 141
已采纳

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

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

图片说明

以下是代码:

 #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 2020-05-09 10:37
    关注

    ......
    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);
    ......

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!