为什么我的 BST 中没有插入任何元素

Why no element gets inserted in my BST

我在这里尝试实现二进制搜索 tree.I 在全局 context.I 中声明的根节点遵循与我如何实现链接相同的想法 list.But 这种方法看起来不像 working.I 想不通这里有什么问题 code.Can 谁能帮我解决这个问题

#include<stdio.h>
struct node {

    int data;
    struct node *left;
    struct node *right;
};
void insert(int value);
void push(struct node *temp,struct node *newNode);


struct node *root;
int main(){
    root= NULL;
    int option,value;
    for(;;){
       printf("Please select an option from below : \n");
       printf("1 for insert\n");
       printf("2 for search\n");
       printf("please enter your option : ");
       scanf("%d",&option);
       printf("\n");
       switch(option){
           case 1:
               printf("you choose to insert\n");
               printf("input your value :");
               scanf("%d",&value);
               insert(value);
               printf("\n");
               break;
           case 2:
               printf("You choose to search\n");
               printf("enter your value : ");
               scanf("%d",&value);
               search(root,value);
               printf("\n");
           default:
               break;

       }
    }
}

void insert(int value){
    struct node *newNode = (struct node *)malloc(sizeof(struct node));

    newNode->data = value;
    newNode->left = NULL;
    newNode->right = NULL;


    push(root,newNode);

}
void push(struct node *root_node,struct node *newNode){

  if(root_node==NULL){
         root_node = newNode;
         printf("inserted\n\n\n");
    }else{
         if(root_node->data > newNode->data){
              push(root_node->left,newNode);
              printf("left\n");
         }else{
            push(root_node->right,newNode);
            printf("right\n");
         }

    }

}

push()中,root_node是一个局部变量:

void push(struct node *root_node,struct node *newNode){

当你这样做时:

root_node = newNode;

你只是在更新局部变量 "root_node",而不是全局变量:

struct node *root;

你的 push() 应该是这样的:

void push(struct node **root_node,struct node *newNode){
    if(*root_node==NULL){
        *root_node = newNode;
        printf("inserted\n\n\n");
    }else{
...

并调用:

push(&root, newNode);

这样,你传递了root的地址,在里面检查那个地址指向的是否为NULL。如果为null,则将newNode的地址赋值给指针dereferenced.

更多解释:

struct node *root是一个指针:类型为"pointer/address"的变量。并指向数据内存中的一个块,我们调用"A".

struct node *root_node 是一个指针,作为局部变量,它是一个 COPY,指向数据内存中的一个块,在你的情况下(因为你通过 "root") 指向 "A".

当您修改 root_node 时,您的 副本 不会修改 root。你所做的只能用来修改"A".

你必须传递root的地址,所以你实际上可以修改root的值,它是指向[=70的指针=].这样,你就有了 **root_node,它是指向 root 的指针,ACTUAL root,它指向 "A",你可以让它指向其他地方(如你所愿)。

我上传了一张图片来说明为什么你的解决方案是错误的:

如您所见,root 从未 更新。

(P.S.: 您可以将 "copy the pointer" 读作 "copy the variable",因为指针是一个变量,其值是一个地址。您可以使用 [=23= 取消引用该地址]).

下面几行不是更新全局变量root

    push(root,newNode);

}
void push(struct node *root_node,struct node *newNode){

  if(root_node==NULL){
         root_node = newNode;
         printf("inserted\n\n\n");

由于您希望 root 是全局的,一种选择是按以下方式更改它,以便使用全局 root

    push(newNode);

}
void push(struct node *newNode){

  if(root == NULL){
         root = newNode;
         printf("inserted\n\n\n");

等等。 (注意:这不是一个完整的解决方案。)