简体   繁体   English

二进制搜索树实现(C ++)

[英]Binary Search Tree Implementation(C++)

Most of the code is from Weiss' "Data structures and algorithm analysis in C++", after I typed the code on CodeBlock(gnu,gcc), compiler reported no error, but then I tried to create an instance and test some functions it went wrong. 大部分代码来自Weiss的“ C ++中的数据结构和算法分析”,当我在CodeBlock(gnu,gcc)上键入代码后,编译器未报告任何错误,但随后我尝试创建一个实例并测试它执行的某些功能错误。 It seems there's something wrong with BST() or insert() because the program stucked instantly since running. BST()或insert()似乎有问题,因为该程序自运行以来立即卡住。 Would someone help me find out how to solve this problem? 有人可以帮我找出解决方法吗? Great thanks!!! 万分谢意!!!

#include <iostream>
using namespace std;
struct TreeNode
{
    int element;
    TreeNode*left,*right;
    TreeNode(const int&e,TreeNode*le=NULL,TreeNode*rt=NULL):element(e),left(le),right(rt){};
};

class BST
{
public:
    BST(){root=NULL;};
    ~BST(){ makeEmpty(root); };
// use public member function to call private member functions.
    void insert(const int & x){ insert(x, root); }
    void remove(const int & x){ remove(x, root); }
    TreeNode*findMin() const{ return findMin(root); }
    bool contain(const int & x) const{ return contain(x,root); }
    void printNodes() const{ printNodes(root); }

private:
    TreeNode*root;

    void makeEmpty(TreeNode*&);
    TreeNode* findMin(TreeNode*) const;
    void insert(const int &, TreeNode*) const;
    void remove(const int &, TreeNode*) const;
    bool contain(const int &, TreeNode*) const;
    void printNodes(TreeNode*) const;
};

void BST::makeEmpty(TreeNode*&t)
{
    if(t!=NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}
TreeNode* BST::findMin(TreeNode*t) const
{
    if(t->left==NULL)    return t;
    return findMin(t->left);
}
void BST::insert(const int & x, TreeNode* t) const
{
    if(t==NULL) t=new TreeNode(x,NULL,NULL);
    else if(x < t->element)    insert(x,t->left);
    else if(x > t->element)    insert(x,t->right);
    else;   /// duplicate, do nothing
}
void BST::remove(const int & x, TreeNode* t) const
{
    if(t==NULL) return;
    if(t->element > x)    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
    {
        TreeNode*oldNode=t;
        t=(t->left==NULL)?t->right:t->left;
        delete oldNode;
    }
}
bool BST::contain(const int & x, TreeNode*t) const
{
    if(t==NULL)    return false;
    else if(x<t->element)    return contain(x,t->left);
    else if(x>t->element)    return contain(x,t->right);
    else    return true;
}
void BST::printNodes(TreeNode*t) const
{
    if(t==NULL) return;
    cout<<t->element<<" ";
    printNodes(t->left);
    printNodes(t->right);
    cout<<endl;
};

Here's the code I wrote to test class BST: 这是我编写的用于测试BST类的代码:

int main()
{
    BST BinarySearchTree;
    int element,node;
    for(int i=0; i<5; i++)
    {
        cin>>element;
        BinarySearchTree.insert(element);
    }
    BinarySearchTree.printNodes();

    cout<<BinarySearchTree.findMin()->element<<endl;

    cin>>node;
    if(BinarySearchTree.contain(node)){ cout<<"item "<<node<<" is in BST."<<endl; }

    BinarySearchTree.remove(node);
    BinarySearchTree.printNodes();
    return 0;
}

Your class BST has only one member variable. 您的BST类只有一个成员变量。 TreeNode*root . TreeNode*root (There's no problem with that) (没有问题)

Your insert and remove functions presumably modify the BST , so you'll need to modify something relating to root . 你的insertremove功能想必修改 BST ,所以你需要修改有关的东西 root

(And your compiler will quickly point out that those functions shouldn't be const for the same reason.) (并且您的编译器会很快指出,出于相同的原因,这些函数不应为const 。)

void BST::makeEmpty(TreeNode*&t)
{
    if(t==NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}

This code is wrong, if t is NULL then you do t->left and t->right. 这段代码是错误的,如果t为NULL,则执行t-> left和t-> right。 That's called dereferencing a NULL pointer and is likely to make your program crash immediately. 这称为取消引用NULL指针 ,这可能会使您的程序立即崩溃。

This should be 这应该是

void BST::makeEmpty(TreeNode*&t)
{
    if (t!=NULL)
    {
        makeEmpty(t->left);
        makeEmpty(t->right);
        delete t;
    }
    t=NULL;
}

您对insert() root没有执行任何操作,因此它什么也不做(除了泄漏内存)。

如果您想交叉检查您的解决方案,则可以在这里找到它https://siddharthjain094.wordpress.com/2014/07/13/binary-search-tree-implementation-in-ccpp/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM