简体   繁体   English

使用C中的递归插入二叉搜索树

[英]Inserting into binary search tree using recursion in C

I was making a program to make a binary search tree which takes input from the user. 我正在编写一个程序来制作一个二叉搜索树,该树接受来自用户的输入。 I have deliberately shown two search functions in my code below. 我在下面的代码中故意显示了两个搜索功能。

Problem : The search function2 works correctly but the search function1 does not work correctly (when used after commenting the other each time). 问题搜索功能2正常工作,但是搜索功能1无法正常工作(每次在互相注释后使用)。 Why is it so? 为什么会这样呢?

I tried doing the dry run and building the recursion stack, which works fine as per me. 我尝试进行空运行并构建递归堆栈,按照我的说法,该堆栈工作正常。 But somehow I think the search function 1 is not able to make that linking in the linked list to insert the element. 但是以某种方式我认为搜索功能1不能在链接列表中进行该链接以插入元素。 That is the reason I am not getting the right output when I try to do the inorder traversal 这就是为什么我尝试进行inorder traversal时没有得到正确的输出的原因

Any help will be really appreciated! 任何帮助将不胜感激!

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

struct node{
    int data;
    struct node *left;
    struct node *right;
};
struct node *root = NULL;

struct node *newNode(int data){
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->data = data;
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}

//SEARCH FUNCTION 1: this does not work correctly
void search(struct node *t,int data){
    if(t){
        if(data > t->data){
            search(t->right,data);
        }else{
            search(t->left,data);
        }
    }else{
        t = newNode(data);
    }
}

//SEARCH FUNCTION 2: this works fine and inserts the element correctly
void search(struct node *t, int data){
    if(data < t->data && t->left != NULL){
        search(t->left, data);
    }else if(data < t->data && t->left == NULL){
        t->left = newNode(data);
    }else if(data > t->data && t->right != NULL){
        search(t->right,data);
    }else{
        t->right = newNode(data);
    }
}

void insertNode(int data){
    if(!root){
        root = newNode(data);
        return;
    }
    search(root, data);
}

void inorder(struct node *t){
    if(t){
        if(t->left){
            inorder(t->left);   
        }
        printf("%d ->", t->data);
        if(t->right){
            inorder(t->right);  
        }
    }
}

int main(){
    int step, data;
    while(1){
        printf("1. Insert element\n");
        printf("2. Print tree\n");
        scanf("%d",&step);
        switch(step){
            case 1: printf("enter element to be inserted\n");
                scanf("%d",&data);
                insertNode(data);
                break;
            case 2:inorder(root); 
                printf("\n");
                break;
        }
    }
    return 0;
}

The problem is that the statement t = newNode(data) assigns to a local variable, so the result is lost immediately after returning from the function. 问题是语句t = newNode(data)分配给局部变量,因此从函数返回后结果立即丢失。

In this case one solution is double indirection, as you don't just want to modify the thing a pointer points to, but the pointer itself. 在这种情况下,一种解决方案是双重间接访问,因为您不仅要修改指针指向的内容,还需要修改指针本身。

void search(struct node **pt,int data)
{
    struct node *t = *pt;
    if (t) {
        if (data > t->data) {
            search(&t->right,data);
        } else {
            search(&t->left,data);
        }
    } else {
        *pt = newNode(data);
    }
}

1st search function: 第一搜索功能:

void search(struct node *t,int data){
    ...
    t = newNode(data);
}

but then in the 2nd search function you do: 但是在第二个搜索功能中,您要做的是:

void search(struct node *t, int data){
    ...
    t->right = newNode(data);
}

which will remember the assignment, while the first will not, since when you are going to recurse, the changes will be lost. 它将记住该分配,而第一个则不会,因为当您要递归时,所做的更改将丢失。


You see in the 2nd case, you are assign what newNode() returns to data member of a struct that is a pointer, while the struct is passed a pointer in this function. 您会在newNode()情况下看到,将newNode()返回的值分配给作为指针的结构的数据成员,而在此函数中向该结构传递了一个指针。 However, in the 1st case, you assign the result in a struct that is passed by one pointer only. 但是,在第一种情况下,您将结果分配给仅由一个指针传递的结构。 If a double pointer would be used, things would be differently. 如果使用双指针,情况将有所不同。

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

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