插入节点后,为什么需要在BST中返回头指针? [英] Why do we need to return the head pointer in a BST after inserting node?

查看:93
本文介绍了插入节点后,为什么需要在BST中返回头指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

typedef struct BTreeNode BTNode;
struct BTreeNode
{
int value;
struct BTreeNode *left_child,*right_child;
};

BTNode* insert(int input_value, BTNode **head_node)
{
    BTNode *temp,*head;
    temp = malloc(sizeof(BTNode));
    temp->value = input_value;
    temp->left_child = NULL;
    temp->right_child = NULL;
    head = *head_node;
    while(1)
    {
        if(head == NULL)
        {
            head = temp;
//          break;
            return head;
        }
        if(temp->value > head->value)
        {
            if(head->right_child == NULL)
            {
                head->right_child=temp;
            }
            else
                head = head->right_child;
        }
        else if(temp->value < head->value)
        {
            if(head->left_child == NULL)
            {
                head->left_child=temp;
            }
            else
                head = head->left_child;
        }
        else
        {
            break;
        }
    }
    return *head_node;
}

int insert_wor(int input_value, BTNode **head_node)
{
    BTNode *temp,*head;
    temp = malloc(sizeof(BTNode));
    temp->value = input_value;
    temp->left_child = NULL;
    temp->right_child = NULL;
    head = *head_node;
    while(1)
    {
        if(head == NULL)
        {
            head = temp;
//          break;
            return 1;
        }
        if(temp->value > head->value)
        {
            if(head->right_child == NULL)
            {
                head->right_child=temp;
            }
            else
                head = head->right_child;
        }
        else if(temp->value < head->value)
        {
            if(head->left_child == NULL)
            {
                head->left_child=temp;
            }
            else
                head = head->left_child;
        }
        else
        {
            return -1;
        }
    }
    return 1;
}

void printtree(BTNode **head_node)
{
    BTNode *head;
    head = *head_node;
    if(head == NULL)
    {
//      printf("Print exit\n");
        return;
    }
    else
    {
        printf("%d\n",head->value);
        printtree(&(head->left_child));
        printtree(&(head->right_child));
    }
}

int main()
{
    BTNode *root=NULL,*root_wor=NULL;
    root=insert(23,&root);
    root=insert(32,&root);
    root=insert(230,&root);
    root=insert(3,&root);
    root=insert(2,&root);
    root=insert(50,&root);
    printtree(&root);
    insert_wor(24,&root_wor);
    insert_wor(42,&root_wor);
    insert_wor(45,&root_wor);
    insert_wor(12,&root_wor);
    insert_wor(87,&root_wor);
    insert_wor(123,&root_wor);
    printtree(&root_wor);
}

在上面的代码中,我编写了两个不同的函数以在BST中插入节点. 1)在一个函数中,我在其中插入一个节点后返回了头指针. 2)在第二个函数中,我没有返回头指针. 当我尝试打印BST时,第一个可以正常工作,但我无法打印 第二个BST. 我的疑问是根据指针的概念,即使我们在成员函数中进行更改,它也应反映在主函数中,但是在这种情况下(在第二种方法中,当我传递头并在成员函数中对其进行更改时,没有反映在主要功能)没有发生? 我觉得我有时会感到困惑.有人可以帮忙澄清一下吗?

In the above code I have written two different functions to insert node in a BST. 1)In one function I am returning the head pointer after inserting a node in it. 2)In the second function I am not returning the head pointer. when I am trying to print the BST first one is working fine but I am not able to print second BST. My doubt is as per the concept of pointers even though we make changes in the member function it should reflect in the main function, but in this case(In the second method when I am passing head and making changes to it in member function they are not reflecting in main function) that is not happening?? I think I am getting confused at some point. Could anyone please help out in clarifying this??

谢谢

推荐答案

在第二个版本中,head = *head_node; 复制头部指针.稍后更改head时,原始指针(由指向)将永远不会更新.如果您始终使用*head_node而不是其副本,或者如果在返回之前分配了*head_node = head;,则insert_wor()函数也将起作用.

In the second version, head = *head_node; copies the head pointer. When you change head later on, the original pointer (pointed to by head_node) won't ever get updated. If you always used *head_node instead of a copy of it, or if you assigned *head_node = head; before returning, then the insert_wor() function would work as well.

这篇关于插入节点后,为什么需要在BST中返回头指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆