在我的BST实现中出现运行时错误的可能原因是什么? 我在C++中编写了一个简单的BST实现,但是由于某种原因,我得到了错误过程255(0xFF)。我已经尝试添加了很多打印行,但是程序一运行就崩溃了。有人能指出造成这种错误的可能原因吗

在我的BST实现中出现运行时错误的可能原因是什么? 我在C++中编写了一个简单的BST实现,但是由于某种原因,我得到了错误过程255(0xFF)。我已经尝试添加了很多打印行,但是程序一运行就崩溃了。有人能指出造成这种错误的可能原因吗,c++,binary-search-tree,C++,Binary Search Tree,多谢各位 #include<iostream> using namespace std; class node { int data; node* left; node* right; public: node( ) { left = NULL; right = NULL; } friend class bsTree; }; class bsTree { node* _root; /

多谢各位

#include<iostream>

using namespace std;

class node
{
    int data;
    node* left;
    node* right;
public:
    node( )
    {
        left = NULL;
        right = NULL;
    }
    friend class bsTree;
};

class bsTree
{
    node* _root; //pointer to root node
    int n;

public:

    bsTree()
    {
        _root = NULL;
        n = 0;
    }

    int size(){ return n; }

    node* root(){ return _root; }

    bool empty()
    {
        if( n == 0 ) return true;
        return false;
    }

    bool isExternal( node* loc )
    {
        if( loc->left == NULL && loc->right == NULL )
            return true;
        return false;
    }

    void addRoot( int info )
    {
        node* newnode = new node();
        newnode->data = info;
        _root = newnode;
    }

    void insert( node*, int );
    //void erase( int );
    node* search( node*, int );
};

node* bsTree :: search( node* loc ,int value)
{
    if( isExternal( loc ) )
        return loc;
    if( loc->data <= value )
        return search( loc->left, value );
    else
        return search( loc->right, value );
    return loc;
}

void bsTree :: insert( node* loc, int info )
{
    node* newnode = new node();
    newnode->data = info;
    node* parent = search( loc, info );
    if( isExternal( parent ) )
    {
        if( parent->data <= info )
            parent->left = newnode;
        else
            parent->right = newnode;
    }
    else
        return insert( parent->left, info );
}

int main()
{
    bsTree mybst;
    mybst.addRoot( 50 );
    mybst.insert( mybst.root(), 30 );
    mybst.insert( mybst.root(), 60 );
    mybst.insert( mybst.root(), 40 );
    mybst.insert( mybst.root(), 20 );
    mybst.insert( mybst.root(), 10 );
    mybst.insert( mybst.root(), 70 );
    int enter;
    cin >> enter;
    node* w = (mybst.search( mybst.root(),enter));
    cout << w;
    return 0;
}
#包括
使用名称空间std;
类节点
{
int数据;
节点*左;
节点*右;
公众:
节点()
{
左=空;
右=空;
}
好友类bsTree;
};
类B树
{
node*\u root;//指向根节点的指针
int n;
公众:
b树()
{
_root=NULL;
n=0;
}
int size(){return n;}
node*root(){return\u root;}
bool empty()
{
如果(n==0),则返回true;
返回false;
}
布尔isExternal(节点*loc)
{
如果(loc->left==NULL&&loc->right==NULL)
返回true;
返回false;
}
void addRoot(内部信息)
{
node*newnode=newnode();
新建节点->数据=信息;
_根=新节点;
}
无效插入(节点*,int);
//无效擦除(int);
节点*搜索(节点*,int);
};
node*bsTree::search(node*loc,int值)
{
如果(isExternal(loc))
返回loc;
if(loc->data left,value);
其他的
返回搜索(loc->right,value);
返回loc;
}
void bsTree::insert(节点*loc,int-info)
{
node*newnode=newnode();
新建节点->数据=信息;
节点*父节点=搜索(loc,info);
如果(isExternal(父级))
{
如果(父->数据左=新节点;
其他的
父->右=新节点;
}
其他的
返回插入(父项->左,信息);
}
int main()
{
b树mybst;
加根(50);
插入(mybst.root(),30);
插入(mybst.root(),60);
插入(mybst.root(),40);
插入(mybst.root(),20);
插入(mybst.root(),10);
插入(mybst.root(),70);
输入;
cin>>进入;
node*w=(mybst.search(mybst.root(),enter));

cout和错误是…?在您的搜索方法中,进程返回255(0xFF),如果您的父项只有一个子项,您将传递nullptr进行搜索,然后尝试访问left或right,这将导致错误。但是程序甚至没有运行。第一个错误可能是搜索没有正确处理“not found”。即“isExternal()”使用loc…而不检查null ptr。说真的,在gdb中运行,当SEGFULT发生时,爬升堆栈“向上”,然后检查东西…这个null ptr很容易找到。小荣誉-这段代码可以编译。