简体   繁体   English

为什么在二进制树中为插入操作传递“ T *&root”?

[英]Why is “T *&root” being passed for insert operation in a binary tree?

I was creating a custom binary tree and has stumbled in the following code for correct insertion in the tree from this site . 我正在创建一个自定义二进制树,并且偶然发现了以下代码,以便从该站点正确插入该树中。

void treeInsert(TreeNode *&root, string newItem) {
    if ( root == NULL ) {
        root = new TreeNode( newItem );
        return;
    }
    else if ( newItem < root->item ) {
        treeInsert( root->left, newItem );
    }
    else {
        treeInsert( root->right, newItem );
    }
}

Why do you need to passed TreeNode *&root instead of TreeNode *root ? 为什么需要传递TreeNode *&root而不是TreeNode *root

If you pass a pointer, rather than a reference to a pointer, modifications that you do to that pointer itself would be local to your treeInsert function. 如果传递指针而不是引用指针,则对该指针本身所做的修改将是treeInsert函数的局部treeInsert This prevents you from inserting the root - the only case when you must modify the pointer itself (the third line in your source code). 这样可以防止您插入根-只有在这种情况下,您必须修改指针本身(源代码中的第三行)。

For example, if you do this 例如,如果您这样做

TreeNode *root = NULL;
treeInsert(root, "hello");

and treeInsert takes TreeNode* , the value of root after the call of treeInsert will remain NULL , because the third line of your source code would modify a local copy of the root pointer. 并且treeInsert采用TreeNode* ,调用treeInsert之后的root值将保持NULL ,因为源代码的第三行将修改root指针的本地副本。

Consider the case when your newItem is the root item. 考虑当newItem是根项目时的情况。 Then this will change your root element. 然后,这将更改您的根元素。 This means that at the end of the operation, your note 'root' has to point to the newly inserted TreeNode. 这意味着在操作结束时,您的注释“ root”必须指向新插入的TreeNode。

Now if you only pass TreeNode *root , you can only change the value, but at the end, your root pointer cannot be changed. 现在,如果仅传递TreeNode *root ,则只能更改该值,但是最后,您的根指针无法更改。 So, you have to pass your pointer by reference, so that you can change it within your function. 因此,您必须按引用传递指针,以便可以在函数中进行更改。

The pointer TreeNode* is passed by reference, so that the original pointer in the calling function can be changed, in case the tree is empty. 指针TreeNode*由引用传递,因此在树为空的情况下,可以更改调用函数中的原始指针

Otherwise, if you just pass a copy of the pointer, the modifications will be local to the function being called. 否则,如果仅传递指针的副本,则修改将在所调用函数的本地进行。

A comment is present in the code itself: Note that root is passed by reference since its value can change in the case where the tree is empty. 在代码本身中有一个注释: 请注意,根是通过引用传递的,因为在树为空的情况下其值可以更改。

If you don't do this, you have a NULL pointer in the calling function representing the root of the tree, which is clearly wrong since you just inserted an element. 如果不这样做,则调用函数中的NULL指针表示树的根,这显然是错误的,因为您只是插入了一个元素。

Some other ways of handling the situation are: 处理这种情况的其他方法是:

  1. Returning the local root pointer 返回本地root指针

    root = treeInsert(root, str);

  2. Passing pointer to root pointer, so it can be changed 将指针传递到根指针,因此可以更改它

    treeInsert(&root, str);

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

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