简体   繁体   English

指向二叉树中的新节点的指针

[英]Pointer to a new node in a Binary Tree

I was solving problem of insertion of node in a binary tree. 我正在解决在二叉树中插入节点的问题。 I have the following doubts: 我有以下疑问:

1) If we are inserting a node then we should return a pointer pointing to that node as then only we will be able to access the node, right? 1)如果要插入节点,则应返回一个指向该节点的指针,因为只有这样我们才能访问该节点,对吗?

2) Then here why are we returning root? 2)那我们为什么要返回根? We must return root->left or root->right accordingly, where am I wrong? 我们必须相应地返回root->leftroot->right ,我在哪里错了?

struct node* insert(struct node* root, int data)
    {
        if (root == NULL)    //If the tree is empty, return a new,single node
            return newNode(data);
        else
        {
            //Otherwise, recur down the tree 
            if (data <= root->data)
                root->left  = insert(root->left, data);
            else
                root->right = insert(root->right, data);
            return root;
        }
    }

3) Is this root which the above code returns the changed one from what it was previously due to recursion? 3)上面的代码返回的根是否是由于递归而更改过的根?

You misunderstand the return value. 您误解了返回值。

The return value of this insert function is a pointer to the subtree that now has data inserted into it. insert函数的返回值是一个指向现在已插入data的子树的指针。 If the passed in root was null, this is a new 1 node tree; 如果传入的root为空,则这是一个新的1节点树; if the passed in root is non-null, the return value is the same root . 如果传入的root为非null,则返回值为相同的root

This makes the recursion a bit simpler. 这使得递归更加简单。 We simply recurse until we run head-on into nullptr in a branch. 我们简单地递归,直到我们直接进入分支的nullptr为止。 Then the recursion stops, and the return value sets the parent's left or right node. 然后递归停止,返回值设置父级的left节点或right节点。

To create a brand new tree you type: 要创建全新的树,请输入:

node* new_tree = insert(nullptr, 7);

to insert something into an existing tree you type: 在您输入的现有树中插入内容:

existing_tree = insert(existing_tree, 7);

or equivalently 或同等

insert(existing_tree, 7);

so long as existing_tree isn't null. 只要existing_tree不为null。

This "double use" of the function (to both create and modify a tree) can confuse, but it makes the specific recursive use a tad less verbose, and makes the "empty tree is a nullptr" and "always do existing_tree = insert(existing_tree, val); " is a rule that makes the empty tree as the null tree work. 该函数的“双重使用”(用于创建和修改树)可能会造成混淆,但是它使特定的递归使用不那么冗长,并且使“空树为nullptr”和“总是做exist_tree existing_tree = insert(existing_tree, val); “是使空树作为空树起作用的规则。

This is, however, a very C way of doing things. 但是,这是一种非常C的处理方式。

A more way of doing things would be: 一种更方式是:

std::unique_ptr<node> insert(std::unique_ptr<node> root, int data)
{
    if (root == nullptr)    //If the tree is empty, return a new,single node
        return std::make_unique<node>(data);
    else
    {
        //Otherwise, recur down the tree 
        if (data <= root->data)
            root->left  = insert(std::move(root->left), data);
        else
            root->right = insert(std::move(root->right), data);
        return std::move(root);
    }
}

where the flow of data into and out of the function is more explicit, and we assume node has a constructor that takes data . 进出函数的数据流更为明确,我们假设node有一个采用data的构造函数。

This recursive insert should always return the very root node of the tree. 此递归插入应始终返回树的根节点。 Just because you read return root doesn't mean the original function call has finished executing, it just means the n'th recursion has finished. 仅仅因为您读取了return root并不意味着原始函数调用已完成执行,仅意味着第n个递归已完成。 The recursive calls have all been pushed onto the stack and therefore must all be resolved before the original caller receives the returned value. 递归调用已全部压入堆栈,因此必须在原始调用者收到返回值之前全部解决。

You can get back to the inserted node by doing a find for the inserted value. 您可以通过find插入的值来返回到插入的节点。

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

相关问题 二叉树旋转函数,不包含引用指针,指针到指针或返回新节点的函数 - Binary tree rotate function without reference-to-pointer, pointer-to-pointer, or the function returning the new node C ++:指针与指针的指针,用于在二叉树中插入节点 - C++: Pointer vs Pointer of Pointer to insert a node in a Binary Tree 二叉树(非二叉搜索树)创建新节点和子节点 - Binary Tree (Not Binary Search Tree) Creating New Node and Children 为什么二叉树中的根变量是节点的指针而不是节点本身? - Why is the root variable in a Binary Tree a pointer the node and not the node itself? 二叉树不接受新节点 - Binary tree doesn't accept new node 为二叉搜索树创建一个新节点 - Creating a new Node for a binary search tree 在没有节点指针作为参数的情况下遍历二进制搜索树 - In order traversal for binary search tree without node pointer as parameter 尝试在构建二进制树时创建指向父节点的指针 - Trying to create a pointer to parent node whilst building binary Tree 递归二叉树生成中分配节点指针时出错 - error in allocating pointer of node in recursive binary tree generation 返回指针到级别排序的二叉树中的第n个节点 - Return pointer to nth node in level-ordered binary tree
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM