简体   繁体   中英

(Binary tree) Code doesn't work but returns no error when compiling

When compiling this code, the compiler doesn't return any warnings or errors but the code simply doesn't work.

The function inserirDado is supposed to recursively create nodes and store values on them, at node.valor, applying the conditions I set before.

void inserirDado(struct node **no, int numero)
{
     if(*no == NULL) { //Se nao houver um nodo anterior, o primeiro numero se torna RAIZ.

          * no = (struct node *) malloc(sizeof(struct node));
          (*no)->direita = NULL;
          (*no)->esquerda = NULL;
          (*no)->valor = numero;

     }else{ //Caso contrario, a definicao do numero, se entrara no nodo esquerdo ou direito.
          if (numero < (*no)->valor) {
               inserirDado(&(*no)->esquerda, numero);
          }
          else
          {
               inserirDado(&(*no)->direita, numero);
          }
          
     }
}

At emOrdem , the functions calls itself until it reaches the leaves, then it should print the values stored at node.valor:

void emOrdem(struct node *no)
    {
         if(no != NULL)
         {
              emOrdem(no->esquerda);
              printf("%i", no->valor);
              emOrdem(no->direita);
    
         }
    }

The complete code is:

#include <stdio.h>
#include <stdlib.h>

struct node
{
     int valor;
     struct node *esquerda;
     struct node *direita;
};

void inserirDado(struct node **no, int numero);

void emOrdem(struct node *no);

int main(void) {

    struct node **arvore1;
    inserirDado(arvore1, 4);
    inserirDado(arvore1, 2);
    inserirDado(arvore1, 3);
    inserirDado(arvore1, 10);
    emOrdem(*arvore1);

}

//Funcao de colocar um numero aleatoria dentro de um Node.
//Ao fazer isso com varios numeros, serao criados nodos com descendentes.

void inserirDado(struct node **no, int numero)
{
     if(*no == NULL) { //Se nao houver um nodo anterior, o primeiro numero se torna RAIZ.

          * no = (struct node *) malloc(sizeof(struct node));
          (*no)->direita = NULL;
          (*no)->esquerda = NULL;
          (*no)->valor = numero;

     }else{ //Caso contrario, a definicao do numero, se entrara no nodo esquerdo ou direito.
          if (numero < (*no)->valor) {
               inserirDado(&(*no)->esquerda, numero);
          }
          else
          {
               inserirDado(&(*no)->direita, numero);
          }
          
     }
}

void emOrdem(struct node *no)
{
     if(no != NULL)
     {
          emOrdem(no->esquerda);
          printf("%i", no->valor);
          emOrdem(no->direita);

     }
}

If you compile this code with GCC 10, with switches -W -Wextra -Wall (which isn't all warnings, by the way), you get:

<source>: In function 'main':
<source>:18:5: warning: 'arvore1' is used uninitialized in this function [-Wuninitialized]
   18 |     inserirDado(arvore1, 4);
      |     ^~~~~~~~~~~~~~~~~~~~~~~

GodBolt

and that shows you where the problem is: You're trying to initialize the place arvore1 is pointing to instead of initializing it .

Please also read:

Why should I always enable compiler warnings?

You must allocate buffer and assign it to arvore1 before passing that to inserirDado .

int main(void) {

    struct node **arvore1 = malloc(sizeof(struct node*)); // add malloc()
    inserirDado(arvore1, 4);
    inserirDado(arvore1, 2);
    inserirDado(arvore1, 3);
    inserirDado(arvore1, 10);
    emOrdem(*arvore1);

}

Another option is to change arvore1 from "a pointer to pointer" to "a pointer", and pass pointer to that to inserirDado .

int main(void) {

    struct node *arvore1 = NULL;
    inserirDado(&arvore1, 4);
    inserirDado(&arvore1, 2);
    inserirDado(&arvore1, 3);
    inserirDado(&arvore1, 10);
    emOrdem(arvore1);

}

(1) Actually you should receive SEG Fault because you haven't initialized arovore1 to NULL.

(2) The important thing to mention here is, we use double pointers to get rid of return values. And what you have done here is a bit contradicting that.

--> Basically we'll create a node (arvore1) and then send the address of the node (&arvore1) to the insertNode (inserirDado) function and inside that, we update corresponding node in that address to the newly created node (temp). I have attached the working code here. Please refer this and incase any doubts you can comment them down.

#include <stdio.h>
#include <stdlib.h>

struct node
{
     int valor;
     struct node *esquerda;
     struct node *direita;
};

void inserirDado(struct node **no, int numero);

void emOrdem(struct node *no);

int main(void) {

    struct node *arvore1 = NULL;
    inserirDado(&arvore1, 4);
    emOrdem(arvore1);

}

//Funcao de colocar um numero aleatoria dentro de um Node.
//Ao fazer isso com varios numeros, serao criados nodos com descendentes.

void inserirDado(struct node **no, int numero)
{
     if(*no == NULL) { //Se nao houver um nodo anterior, o primeiro numero se torna RAIZ.

          struct node *temp = (struct node *) malloc(sizeof(struct node));
          (temp)->direita = NULL;
          (temp)->esquerda = NULL;
          (temp)->valor = numero;
          *no = temp;

     }else{ //Caso contrario, a definicao do numero, se entrara no nodo esquerdo ou direito.
          if (numero < (*no)->valor) {
               inserirDado(&(*no)->esquerda, numero);
          }
          else
          {
               inserirDado(&(*no)->direita, numero);
          }
          
     }
}

void emOrdem(struct node *no)
{
     if(no != NULL)
     {
          emOrdem(no->esquerda);
          printf("%d", no->valor);
          emOrdem(no->direita);

     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM