Why do we need to return the head pointer in a BST

2019-09-18 22:41发布

问题:

#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);
}

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??

Thanks

回答1:

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.