BST与C ++中的OOP原理

时间:2017-10-21 01:13:59

标签: c++ binary-tree binary-search-tree

我对BST感到困惑。我试着学习如何在C ++上制作BST,所以这是我的代码。我确信我真的写了,但我不知道为什么它不起作用。我创建了一个BinarySearchTree类,并在此类中构建为私有。我试着在main中测试之前运行并构建代码。没有显示任何异常但是当我在main中调用函数insert时。然后运行并构建。它说Windows取消了运行错误的编程。我真的不明白什么是错的。如果有人能够清楚地解释我错在哪里。我真的很感激。谢谢你的帮助......

#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
class BinarySearchTree
{
private:
    struct TreeNode
    {
        int key; 
        TreeNode* left;
        TreeNode* right; 
    };
    TreeNode* root; //root pointer

public:
    void insert(int value)
    {
         TreeNode* leaf_node; 
         TreeNode* t,*curr_node; 
         t->key = value; 
         t->right = NULL;
         t->left = NULL;

         if(root == NULL)
         {
             root  = t; //t is root now
         }
         else
         {

             curr_node = root;

             while(curr_node != NULL)
             {

                    leaf_node = curr_node;
                    if(value > curr_node ->key)
                    {
                        curr_node = curr_node->right;
                    }
                    else
                    {
                        curr_node = curr_node->left;
                    }
             }
            if(value > leaf_node ->key)
            {
                leaf_node->right = t;
            }
            else
            {
                leaf_node->left = t;
            }

         }
    }
    void remove(int value)
    {

        TreeNode* curr_node,*parent;
        if (root == NULL)
        {
            cout<<"Tree is empty..!"<<endl;
            return;
        }
        //We keep a variable called found
        int found = false;
        curr_node = root;
        while(curr_node != NULL)
        {
            if(curr_node->key == value)
            {
                found = true;
               break;
            }

            parent = curr_node;
            if(value > curr_node->key)
            {
                curr_node = curr_node->right;
            }
            else
            {
                curr_node = curr_node ->left;
            }
        }
        if(!found)
        {
            cout<<"Value Not Found ..!"<<endl;
            return;
        }
        if(curr_node->right == NULL && curr_node->left == NULL)
        {
            if(parent->right == curr_node)
            {
                parent->right = NULL;
                delete curr_node;
            }
            else
            {
                parent->left = NULL;
                delete curr_node;
            }
            return;
        }

          if(curr_node->right!= NULL && curr_node->left == NULL)
        {
            if(parent->left == curr_node)
            {
                parent->left = curr_node->right;
                delete curr_node;
            }
            else
            {
                 parent->right= curr_node->right;
                delete curr_node;
            }
            return;
        }
        if(curr_node->right== NULL && curr_node->left != NULL)
        {
            if(parent->left == curr_node)
            {
                parent->left = curr_node->right;
                delete curr_node;
            }
            else
            {
                 parent->right= curr_node->right;
                delete curr_node;
            }
            return;
        }

        TreeNode* check_node = curr_node->right;
        if(check_node->left == NULL)
        {

            curr_node->key = check_node->key;
            curr_node->right = check_node->right;
            delete check_node;
        }
        else
        {
            TreeNode* successor,*parent;
            parent = check_node;
            successor = check_node->left;
            while(successor->left != NULL)
            {
                parent = successor;
                successor = successor->left;
            }
            curr_node->key =  successor->key;
            parent->left = successor->right;
            delete successor;
        }
    }
    void inorder()
    {
        //recursive
        inorder(root);
    }
    void inorder(TreeNode* temp)
    {
        if(temp == NULL) return;
        inorder(temp->left);
        cout<<temp->key<<" ";
         inorder(temp->right);
        cout<<temp->right<<" ";
    }
};
int main(int argc, char * argv[])
{
    //Testing The Code
    BinarySearchTree t;
    t.insert(8);
    t.insert(3);
    t.insert(1);
    t.insert(5);
    t.insert(10);
    t.insert(9);
    t.insert(12);
    t.insert(11);
    //t.remove(22);
    //t.remove(11);
    //t.remove(12);
    //t.remove(10);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

下面:

...
TreeNode* t, ...;
t->key = value;
...

你声明一个指针 - 指向内存中的某个随机点 - 然后写入该空间。这称为取消引用未初始化的指针,它会导致未定义的行为,这意味着效果的严重程度没有限制。

你必须构建这样的变量,如:

TreeNode* t = new TreeNode;
t->key = value;

更深层次的问题是你在测试任何代码之前编写了太多代码。在尝试使用它们之前,您应该尝试创建一个节点并为其分配值;你会犯同样的错误,但它会更容易找到。