通过并返回指针'对于C++中的二进制搜索树插入

发布于 2025-01-27 11:22:31 字数 1109 浏览 2 评论 0原文

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode*& node = find(root, val);
        node = new TreeNode(val);
        return root;
    }
    TreeNode*& find(TreeNode*& root, int& val) {
        if (root == nullptr) return root;
        else if (root->val > val) return find(root->left, val);
        else return find(root->right, val);
    }
};

我正在学习C ++,并在演讲幻灯片上阅读此代码。该代码是关于将新节点插入二进制搜索树中的。这个想法是找到目标位置,然后将新节点插入到该位置。我可以理解“查找”函数返回“指针”类型的原因。我认为这是因为我们需要修改“查找”功能返回后目标位置的地址。但是,当我们将根节点传递到“查找”函数中时,我不知道为什么还需要使用“对指针的引用”类型。如果我更改treenode*&找到(treenode*& root,int& val)到treenode*&查找(treenode* root,int& val),程序将返回原始树而无需目标插入。谁能帮忙这个问题?先感谢您!

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        TreeNode*& node = find(root, val);
        node = new TreeNode(val);
        return root;
    }
    TreeNode*& find(TreeNode*& root, int& val) {
        if (root == nullptr) return root;
        else if (root->val > val) return find(root->left, val);
        else return find(root->right, val);
    }
};

I am learning C++ and read this code on a lecture slide. The code is about the insertion of a new node into a binary search tree. The idea is to find the target location and then insert a new node to the location. I can understand the reason for the 'find' function returning a 'reference to pointer' type. I think it is because we need to modify the address of the target location after the 'find' function returns. However, I don't know why we need to use the 'reference to pointer' type also when we pass the root node into the 'find' function. If I change TreeNode*& find(TreeNode*& root, int& val) to TreeNode*& find(TreeNode* root, int& val), the program will return the original tree without the target insertion. Can anyone help with this question? Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与之呼应 2025-02-03 11:22:31

如果您将更改为 treenode*&查找(treenode* root,int& val)然后查看函数的第一行:

    if (root == nullptr) return root;

这将返回对本地变量的引用。在insertIntObst中更改它是未定义的行为,不会更改root actiber actertintobst 。

将第一个值插入空树时逐步浏览代码:

NodeTree *tree = nullptr;
tree = insertIntoBST(tree, 42);

insertintObst函数可以使用与查找的技巧相同的技巧,并修改root到位:

void insertIntoBST(TreeNode*& root, int val) {
    TreeNode*& node = find(root, val);
    node = new TreeNode(val);
}

NodeTree *tree = nullptr;
insertIntoBST(tree, 42);
insertIntoBST(tree, 23);
insertIntoBST(tree, 666);    

甚至更短:

void insertIntoBST(TreeNode*& root, int val) {
    find(root, val) = new TreeNode(val);
}

If you change find to TreeNode*& find(TreeNode* root, int& val) then look at the first line of the function:

    if (root == nullptr) return root;

This would return a reference to a local variable. Changing it in insertIntoBST is undefined behavior and will not change the root variable inside insertIntoBST.

Go through the code step by step when inserting the first value into an empty tree:

NodeTree *tree = nullptr;
tree = insertIntoBST(tree, 42);

The insertIntoBST function could use the same trick as find and modify the root in place:

void insertIntoBST(TreeNode*& root, int val) {
    TreeNode*& node = find(root, val);
    node = new TreeNode(val);
}

NodeTree *tree = nullptr;
insertIntoBST(tree, 42);
insertIntoBST(tree, 23);
insertIntoBST(tree, 666);    

or even shorter:

void insertIntoBST(TreeNode*& root, int val) {
    find(root, val) = new TreeNode(val);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文