1
I’m trying to implement the method of inserting a binary search tree. This is the struct I’m using
typedef struct tipoNo
{
int chave;
struct tipoNo *esq, *dir; //apontadores da esquerda e direita
} tipoNo;
and my method of insertion is this :
tipoNo* inserir(tipoNo *r, int chave)
{
if (r != NULL)
{
if (chave > r->chave)
r->dir = inserir(r->dir, chave);
else
r->esq = inserir(r->esq, chave);
}
else
{
r = (tipoNo *)malloc(sizeof(tipoNo));
r->chave = chave;
r->dir = NULL; r->esq = NULL;
}
return r;
}
When I call the insert method in main I get the error of incompatible types
"incompatible types when assigning to type +type typeNo'} from type ' typeNo *' {aka ːstruct typeNo *'} Arv = insert(&Arv, 5);"
, I know Arv needs to be a pointer to receive a pointer but I couldn’t do this because when I did this implementation creating Arv as *Arv in main, I got the same error
"expected ẓtypeNo *' {aka ẓstruct typeNo *'} but argument is of type Type No **' {aka +struct typeNo **'} typeNo* insert(typeNo *r, int key);"
tipoNo arv;
criar(&arv);
arv = inserir(&arv, 5);
If I do not put Arv to receive the return of the insert method, it gives segfault and when I put, I have this error. I know I need Arv to receive the return of the input method so that the tree I’m working with has the 'entered' values but I can’t get Arv to receive it.
Welcome to Stackoverflow, if possible adjust the title of your question. Ask your question. Code snippets in the title are not well viewed.
– Filipe L. Constante
in this command
arv = inserir(&arv, 5);
the variablearv
is declared astipoNo
, but the functioninserir
is declared astipoNo*
...you cannot assign a pointer to a variable that is not a pointer– zentrunix