简体   繁体   English

创建二分搜索树而不更新指针

[英]creating a binary search tree not updating the pointer

I am creating a simple binary search tree.When I am calling the add method using head pointer,changes made in the method are not reflected to this head pointer.It still points null. 我正在创建一个简单的二进制搜索树。当我使用头指针调用add方法时,在该方法中所做的更改不会反映到该头指针。它仍然指向null。

struct node *add(struct node *root,int data)
{
    if (root==NULL)
    {
        root=(struct node *) malloc(sizeof(struct node));
        root->data=data;
        root->left=NULL;
        root->right=NULL;
        return root;
    }
    else
    {   
        if (data<=root->data)
        {
            root->left=add(root->left,data);
        }
        else
        {
            root->right=add(root->right,data);
        }
        return root;
    }

}

I am calling the function as 我将函数称为

struct node *head=NULL;
add(head,1);
add(head,3);
add(head,15);

In my understanding, on calling the add method, root=head, so head would point to the same memory location where root is pointing and should be updated with the changing value of root accordingly. 以我的理解,在调用add方法root = head时,head会指向root所指向的相同内存位置,因此应该使用root的更改值进行相应的更新。

UPDATE UPDATE

head=add(head,1);

When you pass a pointer (node* here), you just copy the value of the memory address it's pointing to, you can change the contents of this address in the function, but the pointer outside of it would still contain the same address. 当您传递一个指针(这里是node *)时,您只需复制它指向的内存地址的值,就可以在函数中更改该地址的内容,但是指针之外的指针仍将包含相同的地址。

Initially you have head = NULL , it's not pointing anywhere. 最初,您有head = NULL ,它没有指向任何地方。 When you call the function you create a local variable called root , and copy the value of the pointer (NULL) into that. 调用该函数时,将创建一个名为root的局部变量,并将指针(NULL)的值复制到该变量中。 You then allocate some space and change root to point there, but changes to the local variable would be lost once you leave the function, and head outside will continue holding the value NULL . 然后,您分配一些空间并更改root以指向该位置,但是一旦离开函数,对局部变量的更改将丢失,并且head将继续保留值NULL You actually lost the memory you allocated as no one points there any longer (valgrind could have told you that). 您实际上失去了分配的内存,因为那里再也没有人分了(Valgrind可能已经告诉过您)。

If you passed &head to the function instead (the type would then be node** , note that you'd have to use *root inside the function this way), the changes would be made directly on the outside variable (thanks to the fact c would actually pass the address where main() allocated it on the stack directly to the function). 如果改为将&head传递给函数(类型将为node** ,请注意,您必须以这种方式在函数内部使用*root ),更改将直接在外部变量上进行(由于事实c实际上会将main()在堆栈上将其分配的地址直接传递给该函数)。
By the way, in c++, Passing the value by reference would emulate the same thing internally and would be even simpler (you'll be able to keep your code referring to root as is). 顺便说一句,在c ++中,通过引用传递值将在内部模拟相同的事物,甚至会更简单(您将能够使代码照原样引用root )。

Alternatively, you can just return the value of the new head pointer from the function. 另外,您也可以从函数中返回新的头指针的值。 This would limit you to a single return value though (which is fine in this case) 不过,这会将您限制为单个返回值(在这种情况下可以)

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

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