I'm having difficulty adding an element to a binary search tree. The function returns 1 if the element to be inserted is already in the tree and 0 otherwise. The function return works fine but when printing the tree the new element does not appear in the new tree . Here is my code:
int adiciona (Abin a, int x)
{
int n;
if (a == NULL)
{
Abin novi = (Abin)malloc (sizeof (struct sbin));
novi->valor = x;
novi->esq = NULL;
novi->dir = NULL;
n = 0;
}
else
{
if (x == a->valor)
{ n = 1; }
if (x > a->valor)
{ n = adiciona (a->dir , x); }
if (x < a->valor)
{ n = adiciona (a->esq , x); }
}
return n;
}
Is the error in my code? If so, what is the right way to solve this function?