0

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);

     }
}
einpoklum
  • 118,144
  • 57
  • 340
  • 684
TheKenjiMT
  • 39
  • 4

3 Answers3

1

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?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • Gonna do that. What you said matched with the other guy, still didn't work. Returns no error, just a warning: control reaches end of non-void function [-Wreturn-type]. – TheKenjiMT Jun 20 '20 at 22:47
  • @TheKenjiMT: There might be some other issue. I only focused on this specific error. Note you may ask another question - but please take the time to carefully inspect your code generally and that non-void function your compiler is warning you about. – einpoklum Jun 21 '20 at 20:53
1

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);

}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Tried both solutions, didn't work. I only receive a warning: control reaches end of non-void function [-Wreturn-type]. – TheKenjiMT Jun 20 '20 at 22:45
  • @TheKenjiMT The code [seems working](https://wandbox.org/permlink/wKZMwAtUmLc4DJpL). What do you expect? Also please make sure that you are compiling and running your latest intended code. – MikeCAT Jun 21 '20 at 18:25
  • After changing "struct node" to "typedef struct node" and initializing as NULL, it's working properly. I have no clue why as I don't know if there's a difference between adding typedef or not. Thanks for the help. – TheKenjiMT Jun 21 '20 at 19:43
1

(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);

     }
}
Sai Sreenivas
  • 1,690
  • 1
  • 7
  • 16