简体   繁体   English

C二叉搜索树插入不打印

[英]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. 给了我print()方法,所以我应该如何改变它。 I am guessing it has to do with the insert method which I was responsible for implementing. 我猜想这与我负责实现的insert方法有关。 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. 我在想,也许这与我最初设置为NULL的BST* bst点有关。 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. 我是C语言的新手,所以我可能错过了一些东西。

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) . 如果希望insert能够更改根指针,则需要传递根指针的地址,这意味着insert将需要接收一个指向BST的指针,因此这将变为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). 实际上,这写起来好像是上面的更改已经发生一样-它正在尝试取消引用node ,然后将结果与NULL进行比较,这仅在*node是指针的情况下才有意义(这要求该node是指向指针的指针)。

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

I recommend against casting the return value from malloc . 我建议不要强制转换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 * . 如果/忘记了#include <stdlib.h> ,这样做可以/将阻止编译器警告您,以便知道它返回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. 在定义和增加depth ,您实际上从未使用过它。

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! 尽管您已经定义了print来打印出树中的项目,但是您从未真正调用它! 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); 当然,如果更改insert以将指针指向指针,则需要更改这些调用以传递root的地址,例如: insert(&root, 4);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM