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