为什么我们需要在插入节点后在BST中返回头指针?

时间:2013-11-24 08:30:47

标签: c pointers binary-search-tree

#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。 我怀疑是按照指针的概念,即使我们在成员函数中进行更改它应该反映在main函数中,但是在这种情况下(在第二种方法中,当我传递头并在成员函数中对它进行更改时,它们是没有反映在主要功能中)没有发生? 我想我在某些方面感到困惑。有人可以帮忙澄清一下吗??

由于

1 个答案:

答案 0 :(得分:0)

在第二个版本中,head = *head_node; 复制头部指针。稍后更改head时,原始指针(由head_node指向)将不会更新。如果您始终使用*head_node而不是其副本,或者在返回之前分配了*head_node = head;,那么insert_wor()功能也可以使用。