2
I’m having a hard time 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 well 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 it is, what is the right way to solve this function ?
The way the code is, change the value of the variable
a
does not resolve - (however using a reference reference would resolve and itnterferiria less with original design than my suggestion)– jsbueno