回 帖 发 新 帖 刷新版面

主题:二叉查找树模块类的问题

仿照书上,写了这样一个模板,但是在测试的时候程序出现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,同样会发生一样的情况。太诡异了。

回复列表 (共2个回复)

沙发

指针操作,楼主还需要练习基本功。
1、构造函数,root没有赋值,所以是一个野指针。当输入数据并进行insert操作时,“else if ( x < r->data)”这里r->data,r指向一个随机位置,有错。修改方法:构造函数中设置root为NULL。
2、拷贝构造函数,root没有赋值,就开始执行“*this = rhs;”到makeempty函数里面delete t,因为t指向一个随机位置,有错。这个问题与上一问题是类似的,修改方法也是先把root设置为NULL,然后再执行“*this = rhs;”。

板凳

确实是忘了初始化了,

谢谢

我来回复

您尚未登录,请登录后再回复。点此登录或注册