什么是我的程序的分段错误(二进制搜索树)

时间:2017-08-04 02:08:04

标签: c++ binary-search-tree

运行

后程序将崩溃

以下是我的整个计划。

它有分段错误,但我不知道如何纠正

运行

后程序将崩溃

以下是我的整个计划。

它有分段错误,但我不知道如何纠正

#include<iostream>
using namespace std;
 class BinarySearchTree
{
    public:
    struct Node
    {
        int element;
        Node *left;
        Node *right;
        Node(const int &ele,Node *lt,Node *rt)
            :element{ele},left{lt},right{rt} {}
    };
    Node *root;
    void insert(const int &ele)
    {
        insert(ele,root);
    }
    void in_order()
    {
        inorder(root);
    }
    void post_order()
    {
        postorder(root);
    }
    void desc_order()
    {
        descorder(root);
    }
    void remove(int x)
    {
        remove(x,root);
    }
    void pre_order()
    {
        preorder(root);
    }

    BinarySearchTree()
    {
        root==NULL;

    }
    ~BinarySearchTree()
    {
        makeEmpty(root);
    }
    void makeEmpty(Node* & t)
    {
        if(t!= NULL)
        {
            makeEmpty(t->left);
            makeEmpty(t->right);
            delete t;

        }
        t=NULL;
    }

    void insert(const int &x,Node * & t)
    {
        if(t==NULL)
            t=new Node{x,NULL,NULL};
        else if(x<t->element)
            insert(x,t->left);
        else if(x>t->element)
            insert(x,t->right);
    }

    void inorder(Node *r)
    {
        if(r!=NULL)
        {

            postorder(r->left);
            cout<<r->element<<" ";
            postorder(r->right);
        }
    }

    void postorder(Node *r)
    {
        if(r!=NULL)
        {
            postorder(r->left);
            postorder(r->right);
            cout<<r->element<<" ";
        }
    }

    void preorder(Node *r)
    {
        if(r!=NULL)
        {
            cout<<r->element<<" ";
            postorder(r->left);
            postorder(r->right);
        }
    }

    void descorder(Node *r)
    {
        if(r!=NULL)
        {
            postorder(r->right);
            cout<<r->element<<" ";
            postorder(r->left);
        }

    }

    Node *findmin(Node*t)
    {
        if(t==NULL)
            return NULL;
        if(t->left==NULL)
            return t;
        return findmin(t->left);
    }

    void remove(int x, Node * &t)
    {
        if(t==NULL)
            return;
        if(x<t->element)
            remove(x,t->left);
        else if(t->element<x)
            remove(x,t->right);
        else if(t->left!=NULL&&t->right!=NULL)
        {
            t->element=findmin(t->right)->element;
            remove(t->element,t->right);
        }
        else
        {
            Node *oldNode=t;
            t=(t->left!=NULL)?t->left:t->right;
            delete oldNode;
        }
    }
};

int main()
{
    BinarySearchTree BST;
    int N;
    cin>>N;
    int ele;
    for(int i=0; i<N; i++)
    {
        cin>>ele;
        BST.insert(ele);

    }

    cout<<"PRE_ORDER:";
    BST.pre_order();
    cout<<endl;
    cout<<"IN_ORDER:";
    BST.in_order();
    cout<<endl;
    cout<<"POST_ORDER:";
    BST.post_order();
    cout<<endl;
    cout<<"DESCENDING_ORDER:";
    BST.desc_order();
    cout<<endl;

    int dele;
    cin>>dele;
    BST.remove(dele);

    cout<<"NEW TREE AFTER DELETING "<<dele<<endl;
    cout<<"PRE_ORDER:";
    BST.pre_order();
    cout<<endl;
    cout<<"IN_ORDER:";
    BST.in_order();
    cout<<endl;
    cout<<"POST_ORDER:";
    BST.post_order();
    cout<<endl;
    cout<<"DESCENDING_ORDER:";
    BST.desc_order();

    return 0;
}

为什么我的程序无法运行? 真诚地感谢您的帮助

1 个答案:

答案 0 :(得分:0)

您的代码中的错误很少,如下所示导致问题

与作业的平等比较

BinarySearchTree()
{
    root==NULL;

}

将其更改为

root = NULL 

在那之后完美地运作

输出

4
1
2
3
4
PRE_ORDER:1 4 3 2 
IN_ORDER:1 4 3 2 
POST_ORDER:4 3 2 1 
DESCENDING_ORDER:4 3 2 1 

更正后的代码

#include<iostream>
using namespace std;
class BinarySearchTree
{
public:
    struct Node
    {
        int element;
        Node *left;
        Node *right;
        Node(const int &ele,Node *lt,Node *rt)
        :element{ele},left{lt},right{rt} {}
    };
    Node *root;
    void insert(const int &ele)
    {
        insert(ele,root);
    }
    void in_order()
    {
        inorder(root);
    }
    void post_order()
    {
        postorder(root);
    }
    void desc_order()
    {
        descorder(root);
    }
    void remove(int x)
    {
        remove(x,root);
    }
    void pre_order()
    {
        preorder(root);
    }


    BinarySearchTree()
    {
        root = NULL;

    }
    ~BinarySearchTree()
    {
        makeEmpty(root);
    }
    void makeEmpty(Node* & t)
    {
        if(t!= NULL)
        {
            makeEmpty(t->left);
            makeEmpty(t->right);
            delete t;

        }
        t=NULL;
    }

    void insert(const int &x,Node * & t)
    {
        if(t==NULL)
            t=new Node{x,NULL,NULL};
        else if(x<t->element)
            insert(x,t->left);
        else if(x>t->element)
            insert(x,t->right);
    }



    void inorder(Node *r)
    {
        if(r!=NULL)
        {

            postorder(r->left);
            cout<<r->element<<" ";
            postorder(r->right);
        }
    }

    void postorder(Node *r)
    {
        if(r!=NULL)
        {
            postorder(r->left);
            postorder(r->right);
            cout<<r->element<<" ";
        }
    }

    void preorder(Node *r)
    {
        if(r!=NULL)
        {
            cout<<r->element<<" ";
            postorder(r->left);
            postorder(r->right);
        }


    }

    void descorder(Node *r)
    {
        if(r!=NULL)
        {
            postorder(r->right);
            cout<<r->element<<" ";
            postorder(r->left);
        }

    }
    Node *findmin(Node*t)
    {
        if(t==NULL)
            return NULL;
        if(t->left==NULL)
            return t;
        return findmin(t->left);
    }

    void remove(int x, Node * &t)
    {
        if(t==NULL)
            return;
        if(x<t->element)
            remove(x,t->left);
        else if(t->element<x)
            remove(x,t->right);
        else if(t->left!=NULL&&t->right!=NULL)
        {
            t->element=findmin(t->right)->element;
            remove(t->element,t->right);
        }
        else
        {
            Node *oldNode=t;
            t=(t->left!=NULL)?t->left:t->right;
            delete oldNode;
        }


    }


};

int main()
{
    BinarySearchTree BST;
    int N;
    cin>>N;
    int ele;
    for(int i=0; i<N; i++)
    {
        cin>>ele;
        BST.insert(ele);

    }

    cout<<"PRE_ORDER:";
    BST.pre_order();
    cout<<endl;
    cout<<"IN_ORDER:";
    BST.in_order();
    cout<<endl;
    cout<<"POST_ORDER:";
    BST.post_order();
    cout<<endl;
    cout<<"DESCENDING_ORDER:";
    BST.desc_order();
    cout<<endl;


    int dele;
    cin>>dele;
    BST.remove(dele);

    cout<<"NEW TREE AFTER DELETING "<<dele<<endl;
    cout<<"PRE_ORDER:";
    BST.pre_order();
    cout<<endl;
    cout<<"IN_ORDER:";
    BST.in_order();
    cout<<endl;
    cout<<"POST_ORDER:";
    BST.post_order();
    cout<<endl;
    cout<<"DESCENDING_ORDER:";
    BST.desc_order();



    return 0;
}