0

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. It seems there's something wrong with BST() or insert() because the program stucked instantly since running. 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:

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;
}
4
  • 3
    Not sure how insert, or remove are supposed to be const functions. They will certainly change the object at some point. You want your TreeNode defined like it is under makeEmpty. Commented Apr 25, 2013 at 5:51
  • 1
    Can you post the code wit which you test this BST? That will help me to run and see whats the issue.. Commented Apr 25, 2013 at 5:57
  • 1
    The code makes little sense. I don't think it can be fixed, it needs to be rewritten, and slowly. Typing lots of code from a book when you don't properly understand what you are doing is very unlikely to be a useful thing to do. Work more slowly. Start learning with something easier. Commented Apr 25, 2013 at 7:14
  • I changed the parameter Treenode* in private insert() and remove() to Treenode* & and it worked well. Thanks you guys~ Commented Apr 25, 2013 at 10:40

4 Answers 4

4

Your class BST has only one member variable. 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.

(And your compiler will quickly point out that those functions shouldn't be const for the same reason.)

Sign up to request clarification or add additional context in comments.

Comments

2
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. That's called dereferencing a NULL pointer and is likely to make your program crash immediately.

This should be

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

Comments

1

You did nothing with root in insert(), so it does nothing (except for leaking memory).

4 Comments

I did use a public member function insert() to call the private function insert(x, root)
but root is passed by value, so the fact that you are changing t in the private function have no effect outside it.
Well I've changed the parameter Treenode* in private insert() to Treenode* &, guess this worked out then?
I think it will. It will be super-ugly though. (implicit) pass by reference can easily make the calling code unreadable. BTW, don't take const int& parameters. there is no need for it. simple int is much better.
0

if you want to cross-check your solution, you may find it here https://siddharthjain094.wordpress.com/2014/07/13/binary-search-tree-implementation-in-ccpp/

1 Comment

Can you post the code instead of linking to an outside site? That way in case the link dies, we still have the answer here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.