0

My Binary Tree code in C isn't running at all and I'm not sure exactly why. Is there anything blatantly wrong in the function? It runs with just one insert use, but any more and it stops working. It's just supposed to be a simple function that inserts ints at their right place along the tree.

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


typedef struct trees Tree;
struct trees {
    int data;
    Tree *left;
    Tree *right;
};

Tree *inicio=NULL; 


void insert(int n){
        Tree *novo = (Tree*)malloc(sizeof(Tree));
        Tree *aux;
        Tree *pai;

        novo->data=n;
        novo->left=NULL;
        novo->right=NULL;
    if(inicio==NULL){
            inicio = novo;
            return;
    } else {
        aux = inicio;
        pai = NULL;
        while(1){
            pai = aux;
            if(n>pai->data){
                aux=aux->right;
                if(aux==NULL){
                    pai->right=novo;
                    return;
            } else {
                aux=aux->left;
                if(aux==NULL){
                    pai->left=novo;
                    return;
                }

            }
            }
        }
    }   
}

int main() {
    insert(9);
    insert(8);

    printf("[%p] -> (%p, %d, %p)\n",inicio,inicio->left,inicio->data,inicio->right);    

    return 0;
}
4
  • 2
    Fix up your formatting and you may find the problem. In particular, use consistent indentation. Your if/else blocks are not grouped the way you intended. Commented Nov 28, 2019 at 1:42
  • I see the problem now, thanks! Commented Nov 28, 2019 at 1:56
  • 2
    There's little merit to keeping this post open — the problem is very specific rather than usefully generic. Please delete it. Commented Nov 28, 2019 at 1:59
  • 1
    I'll observe that it is more normal to pass and return pointers to the root of the tree to an insert function — if only so you are not constrained to a single list as you are with inicio as the global variable that holds the pointer to the start of the list. Commented Nov 28, 2019 at 2:00

1 Answer 1

1

to illustrate @kaylum point, here is the relevant part reformatted

while(1){
    pai = aux;
    if(n>pai->data){
        aux=aux->right;
        if(aux==NULL){
            pai->right=novo;
            return;
        } // end if(aux==NULL)
        else
        {  // else => aux != NULL
           aux=aux->left;
           if(aux==NULL){
               pai->left=novo;
               return;
           } // end if(aux==NULL)
        } // end else
    } // end if(n>pai->data)
} // end while

Please Note that else after return are pointless noise

while(1){
    pai = aux;
    if(n > pai->data){
        aux = aux->right;
        if(aux == NULL){
            pai->right = novo;
            return;
        }
        continue; // skip to next loop
    }
    // implying (n <= pai->data)
    aux = aux->left;
    if(aux == NULL){
        pai->left = novo;
        return;
    }
}

"Better" implementation would probably use pointer to pointer and reduce redundant code.

void insert(int n)
{
    Tree **pp = &inicio;

    while (*pp)
    {
        if ((*pp)->data < n)
            pp = &(*pp)->right;
        else
            pp = &(*pp)->left;
    }

    *pp = malloc(sizeof **pp);
    if (*pp)
    {
        (*pp)->data = n;
        (*pp)->left = (*pp)->right = NULL;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for adding the "better" implementation @WhozCraig, but I wanted to leave the exercise to DaviAtella
I understand. It is my experience if someone struggles with pointers, they're going to have nightmares with pointers to pointers. You're correct it's a much better implementation, and having both in the same answer allows future readers to see how both methods achieve the same result.
Good point, but I would hate gaining reputation from your addition

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.