So I've implemented the binary search tree but i get an error from the insertion function
this is my code:

#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;

struct NOD{
	int inf;
	struct NOD *left;
	struct NOD *right;
};
NOD *root;
bool Erase(int nr);
void Insertion(int nr);
int Search(NOD* node,int target);
void List();
void Preorder(NOD *ROOT);
void Inorder(NOD *ROOT);
int Height(NOD *node);
int Dimension(NOD *nod);
int Height_Nod(NOD *node, int target);


void Insertion(int nr)
{
	NOD *nod1,*nod2,*nod3;

	nod1=new NOD;

	nod1->inf=nr;
	nod1->left=NULL;
	nod1->right=NULL;

	if(root==NULL)
	{
		root=nod1;
	}
	else
	{
		nod2=root;
		while(nod2!=NULL)
			if(nr<nod2->inf)
			{
				nod3=nod2;
				nod2=nod2->left;
			}
			else
			{
				nod3=nod2;
				nod2=nod2->right;
			}
	}
	if(nr<nod2->inf)
		nod3->left=nod1;
	else
		nod3->right=nod1;
}

void Preorder(NOD *root)
{
	if(root)
	{
		printf(" %d-",root->inf);
		Preorder(root->left);
		Preorder(root->right);
	}
}
void Inorder(NOD *root)
{
	if(root)
	{
		Inorder(root->left);
		printf(" %d-",root->inf);
		Inorder(root->right);
	}
}

bool Erase(int nr)
{
	NOD *tmp,*tmp1,*tmp2;

	tmp1=root;
	while(tmp1->inf != nr)
	{
		if(tmp1->inf>nr)
			tmp1=tmp1->left;
		else
			tmp1=tmp1->right;
	}
	tmp=tmp1;
	if(tmp1->right==NULL)
	{
		tmp1->inf=tmp1->left->inf;
		tmp1->left=tmp1->left->left;
		delete tmp1->left;
	}
	else
		if(tmp1->left==NULL)
		{
			tmp1->inf=tmp1->right->inf;
			tmp1->right=tmp1->right->right;
			delete tmp1->right;
		}
		else
		{
			tmp=tmp1->left;
			tmp2=tmp1;
			while(tmp->left)
			{
			tmp2=tmp;
			tmp=tmp->left;
			}
			tmp1->inf=tmp->inf;
			tmp2->left=NULL;
			delete tmp;
		}
		return 0;
}
void List()
{
	printf("Preorder:");
	Preorder(root);
	printf("Inorder:");
	Inorder(root);
	printf("Height of tree:");
	Height(root);
	printf("Dimension of tree");
	Dimension(root);
}
int Search(NOD* node,int target)
{
	if(node==NULL)
		return(false);
	else
	{
		if(target==node->inf)
			return(true);
		else
			if(target<node->inf)
				return(Search(node->left,target));
			else return(Search(node->right,target));
	}
}

int Dimension(NOD *nod)
{
	if(nod==NULL)
		return(0);
	else
		return(Dimension(nod->left)+1+Dimension(nod->right));
}

int Height(NOD *nod)
{
	if(nod==NULL)
		return(0);
	else
	{
		int Height_pe_stanga=Height(nod->left);
		int Height_pe_dreapta=Height(nod->right);
		if(Height_pe_stanga>Height_pe_dreapta) 
			return(Height_pe_stanga+1);
		else 
			return(Height_pe_dreapta+1);
	}
}
int Height_Nod(NOD *node, int target)
{
	int height=0;
	if (node==NULL)
		return(0);
	else
	{
		if(target<node->inf)
		{
        ++height;
		Search(node->left,target);
		return height;
		}
		else 
		{
			++height;
			Search(node->right,target);
			return height;
		}
	}
}


 
int main()
{
	
	int nr,nr_noduri;

	cout<<"How many nodes?";
	cin>>nr_noduri;
	for(int i=1;i<=nr_noduri;i++)
	{
		cout<<"Enter Node for insertion:";
        cin>>nr;
		Insertion(nr);
		List();
	}

	cout<<endl;
	cout<<"How many nodes do you want to erase?";
	cin>>nr_noduri;
	for(int i=1;i<=nr_noduri;i++)
	{
		cout<<"Enter node to be erased:";
        cin>>nr;
		Erase(nr);
		List();
	}

system("pause");
return 0;
}

to be more precise, Im having problems with this instruction in particular

if(nr<nod2->inf)
		nod3->left=nod1;
	else
		nod3->right=nod1;

the error is

The variable 'nod2' is being used without being initialized

Because you use your nod2 in the loop at line 39 and it becomes Null after it gets out of the loop in line 51. You cannot use it again to get inf in line 52. May be what you mean in line 52 is if(nr<nod3->inf) instead?

if the comparison in line 33 is true, lines 37-51 are skipped and nod2 in line 52 hasn't been given something to point to (which would happen in line 39)

I think the problem is in line 40, I think what you need is an opening brace at the end there and a closing brace below line 55. lines 52-55 should be within the else statement on line 37 and outside the while loop on line 40.

Oh zeofshildpad is correct. However, nod2 will still be Null after the loop, so you need to fix that part as well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.