简体   繁体   中英

C Binary search tree insert not printing

I have the following binary search tree code: EDIT: CHANGED CODE TO USE POINTERS INSTEAD OF VALUES.

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

typedef struct BST BST;
struct BST {
    int data;
    struct BST* left;
    struct BST* right;
};

BST* bst = NULL;

void insert(BST *node, int num) {

    BST *tmp;
    if ((*node) == NULL){
        tmp = (BST*) malloc(sizeof(tmp));
        tmp->data = num;
        tmp->left = NULL;
        tmp->right = NULL;
        node = tmp;
    }
    else if (num < (*node)->left){
        insert((*node)->left, num);
    }
    else if (num >(*node)->right){
        insert((*node)->right);
    }
    return;

}

void search(BST *node, int num) {
    int depth = 0;
    if ((*node) == NULL){
        printf("Element not found");
    }
    else if (num = (*node)->data){
        printf("Depth of element in tree: %d\n", depth);
    }
    else if (num < (*node)->left){
        depth++;
        search((*node)->left, num);
    }
    else if (num >(*node)->right){
        depth++;
        search((*node)->right);
    }
    else
        return;
}

// Printing the elements of the tree - inorder traversal
void print(BST* bst) {
    if (bst == NULL) return;
    print(bst->left);
    printf("%d\n", bst->data);
    print(bst->right);
    return;
}

int main() {
    struct node* root = NULL;

    insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
    return 0;
}

When I run and compile this code I get no answers, but also nothing is printing. This was part of an assignment I have to to. I was given the print() method so I should how change that. I am guessing it has to do with the insert method which I was responsible for implementing. Any such reason as to why no output is being produced?

I was thinking maybe it has to do with the BST* bst point that I initially set equal to NULL. I feel like I never do anything with it, but I am not sure what it is I have to do.

I am relatively new to C, so I may have missed something.

Your code has quite a few problems. Let's start from (near) the top, and work our way down.

BST* bst = NULL;

While not exactly actively harmful to execution, you never use this at all.

void insert(BST *node, int num) {

If you want insert to be able to change the root pointer, you need to pass the address of the root pointer, which means insert will need to receive a pointer to a pointer to a BST, so this would become void insert(BST **node, int num) .

    if ((*node) == NULL){

This is actually written as if the change above had already taken place--it's attempting to dereference node , then compare the result to NULL which only makes sense if *node is a pointer (which requires that node be a pointer to a pointer).

        tmp = (BST*) malloc(sizeof(tmp));

I recommend against casting the return value from malloc . Doing so can/will stop the compiler from warning you when/if you've forgotten to #include <stdlib.h> so it knows that it returns a void * .

I'm going to skip ahead a bit to:

void search(BST *node, int num) {
    int depth = 0;

While you define and increment depth , you never actually use it.

Then we get to at least one really obvious reason you never see any output:

int main() {
    struct node* root = NULL;

    insert(root, 4);
    insert(root, 2);
    insert(root, 1);
    insert(root, 3);
    insert(root, 6);
    insert(root, 5);
    return 0;
}

Although you've defined print to print out the items in the tree, you never actually call it! Of course, if you change insert to take a pointer to a pointer, you'll need to change these calls to pass the address of root , like: insert(&root, 4);

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