Comparing int with NULL in C - is this manual wrong?

I look at this tutorial http://www.learn-c.org/en/Binary_trees , and in the insert function it compares int with NULL. It seems to work fine on the tut site, but it did not work for me, and my research tells me that this should not work. My code is below.

  • Am I wrong here or is the lesson wrong?
  • If the tutorial is wrong, is there a way to dynamically set the value for the first BST node? I thought about checking if the left and right were zero, but that would just reset the first node. Having a third pointer for a node value seems wasteful, but perhaps the only way?

    #include <stdio.h>
    #include <malloc.h>
    struct bstNode {
        int val;
        struct bstNode *left;
        struct bstNode *right;
    };
    
    int insert(struct bstNode *head, int val);
    
    void printDFS(struct bstNode *head);
    
    int main() {
        struct bstNode *bstTree = malloc(sizeof(struct bstNode));
        insert(bstTree, 8);
        insert(bstTree, 5);
        insert(bstTree, 98);
        insert(bstTree, 2);
        insert(bstTree, 15);
        insert(bstTree, 65);
        insert(bstTree, 15);
        printDFS(bstTree);
    }
    
    int insert(struct bstNode *head, int val) {
        //This is the problem statement, it contains random data when I debug as it uninitialized
        if (head->val == NULL) {
            head->val = val;
            return 0;
        }
    
        if (val < head->val) {
            if (head->left != NULL) {
                return insert(head->left, val);
            } else {
                head->left = malloc(sizeof(struct bstNode));
                head->left->val = val;
                return 0;
            }
        } else {
            if (head->right != NULL) {
                return insert(head->right, val);
            } else {
                head->right = malloc(sizeof(struct bstNode));
                head->right->val = val;
                return 0;
            }
        }
    }
    
    void printDFS(struct bstNode *head) {
        if (head->left != NULL) printDFS(head->left);
        printf("%d ", head->val);
        if (head->right != NULL) printDFS(head->right);
    }
    
+4
source share
2 answers

. .

  • , NULL 0. (void *)0, . , int, .
  • , node 0 val. malloc, ( ).

, , 0 , , 0 .

, 0 , .

-, node calloc malloc. calloc 0. val, 0, left right NULL.

, malloc, calloc realloc .

int main() {
    struct bstNode *bstTree = calloc(1, sizeof(struct bstNode));
    if (bstTree == NULL) {
        perror("calloc failed");
        exit(1);
    }

-, /, NULL 0.

    if (head->val == 0) {

.

, .

insert int, . , 0. void.

, node , , . , , node NULL. , , , :

void insert(struct bstNode **head, int val) {
    if (*head == NULL) {
        *head = malloc(sizeof(struct bstNode));
        if (*head == NULL) {
            perror("malloc failed");
            exit(1);
        }
        (*head)->val = val;
        (*head)->left = NULL;
        (*head)->right = NULL;
        return;
    }

    if (val < (*head)->val) {
    ...

:

struct bstNode *bstTree = NULL;
insert(&bstTree, 8);
...
+4
if (head->val == NULL) {
    head->val = val;
    return 0;
}

, tuto , val NULL, insert int. :

struct bstNode *new_bstNode(int val, struct bstNode *left, struct bstNode *right)
{
    struct bstNode *elem = malloc(sizeof(*elem));
    if (elem == NULL)
        return NULL;
    elem->val = val;
    elem->left = left;
    elem->right = right;
    return elem;
}

struct bstNode *insert(struct bstNode *head, int val) {
    if (head == NULL)
        return new_bstNode(val, NULL, NULL);

    if (val < head->val) {
        if (head->left != NULL)
            return insert(head->left, val);
        else
            return head->left = new_bstNode(val, NULL, NULL);
    } else {
        if (head->right != NULL)
            return insert(head->right, val);
        else
            return head->right = new_bstNode(val, NULL, NULL);
    }
}

,

int main() {
    struct bstNode *bstTree = insert(bstTree, 8);
    insert(bstTree, 5);
    insert(bstTree, 98);
    insert(bstTree, 2);
    insert(bstTree, 15);
    insert(bstTree, 65);
    insert(bstTree, 15);
    printDFS(bstTree);
}
-1

Source: https://habr.com/ru/post/1661319/


All Articles