Newbie level: Binary Search Tree Insert function not working

Discussion in 'C++' started by aragant, Apr 16, 2011.

  1. aragant

    aragant New Member

    Joined:
    Apr 16, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    HEY i am trying to resolve my problem here is the code and the error

    Function
    Code:
    void BinarySearchTree::Insert(int val)
    {
        BSTNode *node, *p;
        cout<<node->GetLeftChild();
        cout<<node->GetLeftChild();
        p=root;
        node->setData(val);
        node->SetLeftChild(0);
        node->SetRightChild(0);
        if(p==0)
        {
            root=node;
        }
        else
        {
            while(p)
            {
                if(p->getData()==val)
                    return ;
                else if(p->getData()<val)
                {
                    p->SetRightChild(node);
                }
                else
                    p->SetLeftChild(node);
            }
        }
    }
    driver code
    Code:
    int main()
    {
        int val=0;
        BinarySearchTree tree;
        cout<<"Enter the value to be inserted";
        cin>>val;
        cout<<tree.GetRoot();
        tree.Insert(val);
            return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice