Thread: Newbie level: Binary Search Tree Insert function not working

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    1

    Post Newbie level: Binary Search Tree Insert function not working

    HEY i am trying to resolve my problem here is the code and the error

    2 [main] PA4 2768 exception::handle: Exception: STATUS_ACCESS_VIOLATION
    2331 [main] PA4 2768 open_stackdumpfile: Dumping stack trace to PA4.exe.stackdump
    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Code:
    	BSTNode *node, *p;
    	cout<<node->GetLeftChild();
    	cout<<node->GetLeftChild();
    You use node uninitialized there. And then anything may happen.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 05-17-2010, 04:09 AM
  2. Binary Search Tree Insert
    By bleuz in forum C++ Programming
    Replies: 5
    Last Post: 04-30-2010, 09:53 AM
  3. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  4. level of a binary tree
    By BEN10 in forum C Programming
    Replies: 4
    Last Post: 01-09-2009, 12:38 PM