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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
1
2
3
4
5
6
7
8
9
10
int main()
{
	int val=0;
	BinarySearchTree tree;
	cout<<"Enter the value to be inserted";
	cin>>val;
	cout<<tree.GetRoot();
	tree.Insert(val);
        return 0;
}
1
2
3
BSTNode *node, *p;
cout<<node->GetLeftChild();
cout<<node->GetLeftChild();

node doesn't point to anything, so you can't access it's methods.
ok i edited my function and removed cout statements but the segmentation fault is still there

:(
It's not the problem of couts. The problem is that node points to garbage. What is it supposed to point to?
1
2
3
4
5
6
7
8
9
10
11
12
void BinarySearchTree::Insert(int val)
{
	BSTNode *node, *p;
        node = new BSTNode(/*shit*/);
	cout<<node->GetLeftChild();
	cout<<node->GetLeftChild();
	p=root;
	node->setData(val);
	node->SetLeftChild(0);
	node->SetRightChild(0);
	if(p==0)...
Topic archived. No new replies allowed.