我在C ++中编写了一个简单的BST实现,但由于某种原因,我得到错误进程返回255(0xFF)。我尝试添加了很多打印行,但程序运行时程序崩溃了。有人可以指出可能导致这种错误的原因。
谢谢
#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;
}