0

I'm trying to do a function that inserts values into a binary tree. The first while loop in insertbin(...) just completely ignores the x value when it's equal to NULL after it has been moved to the next element. Is there something wrong in my condition?

I've tried using a prev node to check the condition but it still didn't work.

#include <stdlib.h>
#include <stdio.h>
#include "Queue_arr.h"
tree* createtree() {
    tree*mytree = (tree*)malloc(sizeof(tree));
    mytree->root = NULL;
    return mytree;
}

void insertbin(tree* T, int data) {
    treenode *x, *y, *z;
    int flag = 1;
    z = (treenode*)malloc(sizeof(treenode));
    y = NULL;
    x = T->root;
    z->key = data;
    while (x != NULL) //While the tree isn't empty
    {
        y = x;
        if (z->key < x->key) //If the data is smaller than the existing key to the left
            x = x->sonleft;
        else //Else, to the right
            x = x->sonright;
    }
    z->father = y;
    if (y == NULL) //If y is the root
        T->root = z;
    else
        if (z->key < y->key) //If the data is smaller than the existing key to the left
            y->sonleft = z;
        else //Else, to the right
            y->sonright = z;
}

void insertscan(tree *T) //Scans the data to insert to the tree via insertbin(...)
{
    int data;
    printf("Enter a number (Enter a negative number to stop): \n");
    scanf("%d", &data);
    while (data >= 0)
    {
        insertbin(T, data);
        scanf("%d", &data);
    }

}



void main()
{
    tree* T;
    T = createtree();
    insertscan(T);
}
  • 2
    Please remove any code not related to the problem at hand. It helps if you can just hard code the calls to `insertbin()` and get rid of all the user input stuff. The simpler your [mre], the better. – John Kugelman Oct 01 '19 at 16:01
  • 1
    `malloc` leaves memory uninitialized https://stackoverflow.com/a/1538427/3365922 so you might have bad `z->sonleft` and `z->sonright` – fas Oct 01 '19 at 16:07
  • Should I change it to `calloc` than? – Amit Peretz Oct 01 '19 at 16:08
  • 1
    `calloc` will work for pretty much any implementation you are likely to see, although technically it is not guaranteed to initialize a pointer to `NULL`. – Ian Abbott Oct 01 '19 at 16:10

1 Answers1

0

I changed the malloc in the z definition to calloc and that solved it. Must've been because malloc doesn't fill your values with NULL.

(thanks user3365922)

  • The memory allocated by `calloc` will have all bytes set to 0. If the memory contents are to be interpreted as containing objects of floating point or pointer types, then technically there is no guarantee that those floating point objects will compare equal to 0.0 or that those pointer type objects will compare equal to `NULL`. However, on the most common platforms you are likely to encounter, floating point objects with all bits zero will compare equal to 0.0, and pointer type objects with all bits zero will compare equal to `NULL`. – Ian Abbott Oct 01 '19 at 16:38
  • Don't use `calloc()` to work around the lack of initialization. Null pointers aren't necessarily all-bits-zero. You need to explicitly set the fields to `NULL`. – John Kugelman Oct 01 '19 at 19:14