1
I’m trying to insert a tree node.
I am using this code, but there are a number of small errors that I cannot understand why of the errors.
The last of them is related to valor1, which is not stated. However I need my node new receive a value to make comparisons of smaller and larger than struct registro to be added to the tree.
void insere_Arvore(nodo* raiz, struct registro_st registro){
if(raiz == NULL)
{
return 0;
}
nodo* novo = (nodo*)malloc(sizeof(nodo));
if(novo == NULL){
return 0;
}
novo->dado->valor = valor1;
novo->dir = NULL;
novo->esq= NULL;
if(*raiz = NULL)
{
*raiz = novo;
}
else{
nodo* atual = *raiz;
nodo* ant = NULL;
}
while(atual != NULL)
{
ant = atual;
if (valor1 == atual->dado->valor){
free(novo);
return 0;
}
if(valor1 > atual->dado->valor)
{
atual = atual->dir
}
else{
atual = atual->esq;
}
if(valor1 > ant->dado->valor)
{
ant->dir = novo;
}
else{
ant->esq = novo;
}
if(valor > ant->dado->valor){
ant->dir = novo;
}
else{
ant->esq = novo;
}
}
}
return 1;
}
Complete code:
typedef struct registro_st{ // sequência de objetos do mesmo tipo
char login[50];
char nome[50];
float valor;
struct registro *prox;
} registro;
typedef struct nodo_st{
registro *dado;
struct nodo_st *dir;
struct nodo_st *esq;
} nodo;
typedef struct Lista_st{
nodo *cabeca;
nodo *cauda;
int tamanho;
} lista;
void insere_Arvore(nodo* raiz, struct registro_st registro){
if(raiz == NULL)
{
return 0;
}
nodo* novo = (nodo*)malloc(sizeof(nodo));
if(novo == NULL){
return 0;
}
novo->dado->valor = valor1;
novo->dir = NULL;
novo->esq= NULL;
if(*raiz = NULL)
{
*raiz = novo;
}
else{
nodo* atual = *raiz;
nodo* ant = NULL;
}
while(atual != NULL)
{
ant = atual;
if (valor1 == atual->dado->valor){
free(novo);
return 0;
}
if(valor1 > atual->dado->valor)
{
atual = atual->dir
}
else{
atual = atual->esq;
}
if(valor1 > ant->dado->valor)
{
ant->dir = novo;
}
else{
ant->esq = novo;
}
if(valor > ant->dado->valor){
ant->dir = novo;
}
else{
ant->esq = novo;
}
}
}
return 1;
}
return 0;
Change: 'must be' Return;– user3629249
`For ease of readability and comprehension, constantly ignore the code. Discard after each open clamp, no power before each closing armour. Suggests each traverse level be 4 spaces
– user3629249
In C, heap allocation functions (malloc, calloc, realloc) return
void *
which can be assigned to any pointer, Casting only blocks the code.– user3629249
nodo* novo =
It’s very poor programming practice too weak to name variables the same as a type name– user3629249