Run-time error

Hi,everybody!I am working on a program,which merges two binary search trees in a new one,but I receive the same error every time for parameters t1 and t2:
Run-Time Check Failure #3 - The variable 't2' is being used without being initialized.
Run-Time Check Failure #3 - The variable 't1' is being used without being initialized.
Here`s the code:
#include<iostream>
#include<stdlib.h>
using namespace std;
struct elem
{ int key;
elem *left;
elem *right;
} *t1=NULL, *t2=NULL, *t3=NULL;
void add(int t1, elem *&t2)
{
if(t2==NULL)
{
t2=new elem;
t2->key=t1;
t2->left=NULL;
t2->right=NULL;
}
else
{
if(t1<t2->key)
add(t1,t2->left);
else
if(t1>t2->key)
add(t1,t2->right);
}
}
void printnode(int n, int h)
{
for(int i=0;i<h;i++)
cout<<"\t";
cout<<n<<endl;
}

void show(elem *t, int h)
{
if(t==NULL)
return;
show(t->right,h+1);
printnode(t->key,h);
show(t->left,h+1);
}
void merge_tree(elem *t1 , elem *t2)
{
if(t2)
{
merge_tree(t1 , t2->left);
merge_tree(t1 , t2->right);

}
}
elem * merge_2_BST(elem *t1 , elem *t2)
{
if(t1==NULL)
return t2;
if(t2==NULL)
return t1;
merge_tree(t1,t2);
return t1;
}
void in_order_traversal(elem *head)
{
if(head != NULL)
{
in_order_traversal(head->left);

in_order_traversal(head->right);
}
}

void main()
{
int n=1,f=1;
elem *t1;
elem *t2;
elem *t3;
t3=merge_2_BST(t1 ,t2);
while(n!=0)
{
cout<<"\n Insert elements fot the 1 tree: ";
cin>>n;
if (n!=0)
{
add(n, t1);
}
}

cout<<"The first tree is: \n";
show(t1,0);


while(f!=0)
{
cout<<"\n Insert elements for the 2 tree: ";
cin>>f;
if (f!=0)
{
add(f, t2);


}
cout<<"The combined tree is:"<<endl;
in_order_traversal(t3);
system("pause");

}
}
In the very beginning of the code you declared global variables t1, t2, t3 that were initialized by NULL.

struct elem
{ int key;
elem *left;
elem *right;
} *t1=NULL, *t2=NULL, *t3=NULL;

However further in the code in main (that shall be declared as int main()) you defined local variables with the same names as the global variables and did not initialized them.

void main()
{
int n=1,f=1;
elem *t1;
elem *t2;
elem *t3;

This is the reason of the messages.
Last edited on
I fixed that but still got Error 't1' : 'elem *' differs in levels of indirection from 'int' d:\microsoft visual studio 2010 express\dggsgsgs\dggsgsgs\fgsfgddfg.cpp 74.This happens even when I have initialized the local variables t1,t2 and t3
It looks that you even do not understand an elementary thing. Reread my previous post.
Last edited on
The errors are gone,thank you,but in main, the function,which merges the trees, doesn`1t work: cout<<"The combined tree is:"<<endl;
in_order_traversal(t3);
show(t3,0);
Topic archived. No new replies allowed.