主题:二叉查找树模块类的问题
仿照书上,写了这样一个模板,但是在测试的时候程序出现core dump。
#include<iostream>
using namespace std;
template <class Compare>
class BinarySearchTree
{
public:
BinarySearchTree()
{
// root = new Node(NULL,NULL,0);
}
BinarySearchTree(const BinarySearchTree &rhs)
{
// clone(rhs.root);
*this = rhs;
}
const BinarySearchTree & operator = ( const BinarySearchTree &rhs )
{
if ( this != &rhs)
{
makeempty();
root = clone(rhs.root);
}
return *this;
}
~BinarySearchTree()
{
makeempty();
}
void makeempty()
{
makeempty(root);
}
void print_tree() const
{
print_tree(root);
}
bool contain( const Compare &x) const
{
return contain(x,root);
}
const Compare & findMax() const
{
return findMax(root)->data;
}
const Compare & findMin() const
{
return findMin(root)->data;
}
void insert(const Compare & x)
{
insert(root,x);
}
void deleteT(const Compare &x)
{
deleteT(root,x);
}
private:
struct Node
{
Node *left_child;
Node *right_child;
Compare data;
Node(Node *left = NULL,Node *right = NULL,const Compare &d = Compare()):left_child(left),right_child(right),data(d){}
};
Node *root;
bool contain( const Compare & x,Node *t) const;
/* {
if( t == NULL )
{
return false;
}
else if( t->data < x )
{
return contain(x,t->right_child);
}
else if( t->data > x )
{
return contain(x,t->left_child);
}
else
{
return true;
}
}*/
Node * findMax(Node * r) const
{
Node *t = r;
while( NULL != t->right_child )
{
t = t->right_child;
}
return t;
}
Node * findMin(Node * r) const
{
Node * t = r;
while( NULL != t->left_child )
{
t = t->left_child;
}
return t;
}
void insert(Node * &r,const Compare& x)
{
if ( r == NULL )
{
r = new Node(NULL,NULL,x);
}
else if ( x < r->data)
{
insert(r->left_child,x);
}
else if( x > r->data)
{
insert(r->right_child,x);
}
else
{
}
}
void deleteT(Node * &r,const Compare & x)
{
if( r == NULL)
{
return ;
}
if( x < r->data)
{
deleteT(r->left_child,x);
}
else if( x>r->data)
{
deleteT(r->right_child,x);
}
else if( r->left_child != NULL && r->right_child != NULL )
{
r->data = findMin(r->right_child)->data;
deleteT(r->right_child,r->data);
}
else
{
Node *old = r;
r = (r->left_child != NULL)?r->left_child:r->right_child;
delete old;
}
}
void makeempty(Node * & t)
{
if( t != NULL)
{
makeempty(t->left_child);
makeempty(t->right_child);
delete t;
}
t = NULL;
}
void print_tree(Node *t) const
{
if( t!= NULL )
{
print_tree(t->left_child);
cout << t->data<<endl;
print_tree(t->right_child);
}
}
Node * clone(Node *t)
{
if(t == NULL)
{
return NULL;
}
return new Node(clone(t->left_child),clone(t->right_child),t->data);
}
};
template <class Compare>
bool BinarySearchTree<Compare>::contain(const Compare &x,Node *t) const
{
if( t == NULL )
{
return false;
}
else if( t->data < x )
{
return contain(x,t->right_child);
}
else if( t->data > x )
{
return contain(x,t->left_child);
}
else
{
return true;
}
}
int main()
{
BinarySearchTree<int> BST;
int i;
int data;
for(i = 0;i < 4;i++)
{
cin >> data;
BST.insert(data);
}
// BST.print_tree();
BinarySearchTree<int> BST1(BST);
// BST1.deleteT(3);
// BST1.print_tree();
BST1.print_tree();
return 0;
}
单步调试
在 BinarySearchTree<int> BST1(BST)后,
进行输出的时候,会发生segment fault。这是什么原因造成的啊。
是因为那clone函数吗?实在想不通了。
而且如果把
// clone(rhs.root);屏蔽掉,变成*this = rhs,同样会发生一样的情况。太诡异了。
#include<iostream>
using namespace std;
template <class Compare>
class BinarySearchTree
{
public:
BinarySearchTree()
{
// root = new Node(NULL,NULL,0);
}
BinarySearchTree(const BinarySearchTree &rhs)
{
// clone(rhs.root);
*this = rhs;
}
const BinarySearchTree & operator = ( const BinarySearchTree &rhs )
{
if ( this != &rhs)
{
makeempty();
root = clone(rhs.root);
}
return *this;
}
~BinarySearchTree()
{
makeempty();
}
void makeempty()
{
makeempty(root);
}
void print_tree() const
{
print_tree(root);
}
bool contain( const Compare &x) const
{
return contain(x,root);
}
const Compare & findMax() const
{
return findMax(root)->data;
}
const Compare & findMin() const
{
return findMin(root)->data;
}
void insert(const Compare & x)
{
insert(root,x);
}
void deleteT(const Compare &x)
{
deleteT(root,x);
}
private:
struct Node
{
Node *left_child;
Node *right_child;
Compare data;
Node(Node *left = NULL,Node *right = NULL,const Compare &d = Compare()):left_child(left),right_child(right),data(d){}
};
Node *root;
bool contain( const Compare & x,Node *t) const;
/* {
if( t == NULL )
{
return false;
}
else if( t->data < x )
{
return contain(x,t->right_child);
}
else if( t->data > x )
{
return contain(x,t->left_child);
}
else
{
return true;
}
}*/
Node * findMax(Node * r) const
{
Node *t = r;
while( NULL != t->right_child )
{
t = t->right_child;
}
return t;
}
Node * findMin(Node * r) const
{
Node * t = r;
while( NULL != t->left_child )
{
t = t->left_child;
}
return t;
}
void insert(Node * &r,const Compare& x)
{
if ( r == NULL )
{
r = new Node(NULL,NULL,x);
}
else if ( x < r->data)
{
insert(r->left_child,x);
}
else if( x > r->data)
{
insert(r->right_child,x);
}
else
{
}
}
void deleteT(Node * &r,const Compare & x)
{
if( r == NULL)
{
return ;
}
if( x < r->data)
{
deleteT(r->left_child,x);
}
else if( x>r->data)
{
deleteT(r->right_child,x);
}
else if( r->left_child != NULL && r->right_child != NULL )
{
r->data = findMin(r->right_child)->data;
deleteT(r->right_child,r->data);
}
else
{
Node *old = r;
r = (r->left_child != NULL)?r->left_child:r->right_child;
delete old;
}
}
void makeempty(Node * & t)
{
if( t != NULL)
{
makeempty(t->left_child);
makeempty(t->right_child);
delete t;
}
t = NULL;
}
void print_tree(Node *t) const
{
if( t!= NULL )
{
print_tree(t->left_child);
cout << t->data<<endl;
print_tree(t->right_child);
}
}
Node * clone(Node *t)
{
if(t == NULL)
{
return NULL;
}
return new Node(clone(t->left_child),clone(t->right_child),t->data);
}
};
template <class Compare>
bool BinarySearchTree<Compare>::contain(const Compare &x,Node *t) const
{
if( t == NULL )
{
return false;
}
else if( t->data < x )
{
return contain(x,t->right_child);
}
else if( t->data > x )
{
return contain(x,t->left_child);
}
else
{
return true;
}
}
int main()
{
BinarySearchTree<int> BST;
int i;
int data;
for(i = 0;i < 4;i++)
{
cin >> data;
BST.insert(data);
}
// BST.print_tree();
BinarySearchTree<int> BST1(BST);
// BST1.deleteT(3);
// BST1.print_tree();
BST1.print_tree();
return 0;
}
单步调试
在 BinarySearchTree<int> BST1(BST)后,
进行输出的时候,会发生segment fault。这是什么原因造成的啊。
是因为那clone函数吗?实在想不通了。
而且如果把
// clone(rhs.root);屏蔽掉,变成*this = rhs,同样会发生一样的情况。太诡异了。