简体   繁体   English

将节点添加到二进制搜索树

[英]Add node to binary search tree

The following code should add a node to my BST structure, but fails to do so. 以下代码应将节点添加到我的BST结构中,但未这样做。 New nodes simply dont get attached to the tree, including the root node. 新节点根本不会附加到树,包括根节点。 Could anyone point out where the problem is? 谁能指出问题出在哪里?

The constructor for the tree: 树的构造函数:

template <typename K, typename T>
MyBinaryTree<K, T>::MyBinaryTree() {
    root = nullptr;
}

Function that gets called from the outside: 从外部调用的函数:

    template <typename K, typename T>
void MyBinaryTree<K, T>::addValue(K key, T value) {
    Node *node = new Node(key, value);
    addToSubtree(root, node);
}

And the internal private function which has to attach the node: 和必须附加节点的内部私有函数:

    template <typename K, typename T>
void MyBinaryTree<K, T>::addToSubtree(MyBinaryTree<K, T>::Node *node,
            MyBinaryTree<K, T>::Node *toAdd) {
    if(node == nullptr) {
        node = toAdd;
    } else {
        if(toAdd->key > node->key) addToSubtree(node->right, toAdd);
        else if(toAdd->key < node->key) addToSubtree(node->left, toAdd);
    }
}

You should use references, like this: MyBinaryTree<K, T>::Node *&root 您应该使用如下引用: MyBinaryTree<K, T>::Node *&root

because of when you modify root in addToSubtree , it modify only local function parameter root, copy of root parameter, not root parameter by itself. 由于您在addToSubtree修改root时,它仅修改本地函数参数root,root参数的副本,而不修改root参数本身。

Your node variable is a local variable containing pointer to Node , when you assign a value to it, it only works for that local variable in function scope. 您的node变量是一个包含指向Node指针的局部变量,当您为它分配一个值时,它仅适用于函数范围内的该局部变量。

To make your function work, you should make a node variable pointer to pointer (so it gets called like addToSubtree(&node->right, toAdd) or reference to pointer. 为了使您的函数正常工作,您应该使node变量的指针指向指针(这样就可以像addToSubtree(&node->right, toAdd)或指向指针的引用那样addToSubtree(&node->right, toAdd)addToSubtree(&node->right, toAdd)

Your code suffers from two problems: 您的代码有两个问题:

  1. Although it's supposed to represent Object-Oriented thinkig, the code falls back to C-style of doing things. 尽管它应该代表面向对象的thinkig,但是代码可以回溯到C风格的服务中。 the root element should not be passed as argument. root元素不应作为参数传递。 it exists automaticallty in every member function scope since it's a member variable. 由于它是成员变量,因此它在每个成员函数作用域中都存在automaticallty。

this : 这个 :

   template <typename K, typename T>
void MyBinaryTree<K, T>::addToSubtree(MyBinaryTree<K, T>::Node *root,
            MyBinaryTree<K, T>::Node *toAdd) {
   //rest...
}

should be this : 应该是这样的:

   template <typename K, typename T>
void MyBinaryTree<K, T>::addToSubtree( MyBinaryTree<K, T>::Node *toAdd) {
    if(root == nullptr) {
        root = toAdd;
    } //rest...
}

The function already knows what root is since root is a member variable. 该函数已经知道什么是root ,因为root是成员变量。 member functions can change member variables without of them needing to be passed into the function 成员函数可以更改成员变量,而无需将其传递给函数

  1. The real problem is the difference between pointer to pointer to pointer . 真正的问题是指针指针之间的区别。 whenever we want a function to change one of the arguments given, that argument should be passed either as pointer or as a reference. 每当我们希望函数更改给定的参数之一时,该参数都应作为指针或引用传递。

if the argument is already a pointer ,in order to change the pointer itself it should be passed either as pointer to pointer or a referene to pointer 如果参数已经是指针,则为了更改指针本身,应将其作为指针传递给指针或作为指针传递给参照

so, let's assume we do want to continue pass root as an argument (we don't), we should pass it either as 因此,假设我们确实希望继续将root作为参数传递(我们不希望这样做),我们应该将它作为

void MyBinaryTree<K, T>::addToSubtree(MyBinaryTree<K, T>::Node **root,
            MyBinaryTree<K, T>::Node *toAdd) {
    if(*root == nullptr) {
        *root = toAdd;
        //rest

either as 要么作为

void MyBinaryTree<K, T>::addToSubtree(MyBinaryTree<K, T>::Node*& root,
            MyBinaryTree<K, T>::Node *toAdd) {
    if(root == nullptr) {
        root = toAdd;
       //the rest...

the second is more C++ style, the first is more C style, but both are valid. 第二个是更多的C ++样式,第一个是更多的C样式,但是两者都是有效的。

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

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