-1
I’m not getting the insertion recursively and I don’t know what I’m doing wrong..
Precise follow the following prototypes:
struct Arvore {
int info;
struct Arvore *esq, *dir;
};
typedef struct Arvore Arvore;
void inserir(Arvore* a, int info);
I’m trying to do it this way:
void inserir(Arvore *a, int info) {
if (a == NULL) {
a = (Arvore *) malloc(sizeof(Arvore));
a->info = info;
a->esq = NULL;
a->dir = NULL;
} else if (info < a->info) {
inserir(a->esq, info);
} else {
inserir(a->dir, info);
}
}
But it doesn’t work, does anyone know how I can solve it? I’ve done a lot of research and I can’t find examples the way I need to implement them. (With the function prototype being void)
The way it is there, besides not working, you have a beautiful memory leak, since you don’t keep any reference to the allocated memory. As you said the return of the function should be
void
, an alternative would be to pass a pointer to pointer, setting the function as follows:void inserir(Arvore ** a, int info)
. And then it would be necessary to adapt the body of function.– Vander Santos
post a complete code :(
– arfneto