Like Tree1Likes
  • 1 Post By EVOEx

Newbie level: Binary Search Tree Insert function not working

This is a discussion on Newbie level: Binary Search Tree Insert function not working within the C++ Programming forums, part of the General Programming Boards category; HEY i am trying to resolve my problem here is the code and the error 2 [main] PA4 2768 exception::handle: ...

  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.
    aragant likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21