【问题标题】:How to make a weak pointer to the parent in a binary tree?如何在二叉树中创建一个指向父节点的弱指针?
【发布时间】:2022-12-14 01:20:21
【问题描述】:

我想创建一个从子节点到其父节点的弱指针,但我不知道如何获取该节点。我不想更改我的所有代码以使其工作,但我不知道任何其他方式

namespace Datastructures {

    // creating new tree nodes with given data and nullptr to left and right child
    TreeNode::TreeNode (int data){
        data_ = data;
        left_child = nullptr;
        right_child = nullptr;
        WeakTreeNodeptr parent;
    }

    // returns data of the tree node
    int TreeNode::get_data(){
        return data_;
    }

    // sets data of the tree node
    void TreeNode::set_data(int data){
        data_ = data;
    }

    // sets the data of the left child of the tree node
    void TreeNode::set_left_child(int data){
      if (left_child == nullptr) // if the left child does not exist then create one
       { 
           left_child = std::make_shared<TreeNode>(data);
       }
      else // if a left child exists then change the data to the given data
      {
         left_child->data_ = data;
      }
    }

    // sets the data of the right child of the tree node
    void TreeNode::set_right_child(int data){
      if (right_child == nullptr) // if the right child does not exist then create one
       { 
           right_child = std::make_shared<TreeNode>(data);
       }
      else //if a right child exists then change the data to the given data
      {
         right_child->data_ = data;
      }
    }

}

【问题讨论】:

  • 获取什么节点?请提供minimal reproducible example,说明您尝试过的内容以及遇到的问题。
  • 获取父节点指针没有神奇的方法,您需要将其存储在 TreeNode 类中。因此,您需要更改 TreeNode 类以添加该成员变量。完成后,您需要在构造函数中初始化该值,或将 set_parent_node 方法添加到 TreeNode(或两者)。话虽如此,许多二叉树节点算法不需要知道父节点。所以你可能在那里有一些灵活性。

标签: c++ binary-tree weak-ptr


【解决方案1】:

就像@John 说的。与某些高级语言不同,c++ 不提供仅通过 item 本身访问项目父节点的方法。在这种语言的大部分情况下,您必须自己配置和设置诸如此类的系统。做你想做的事情的最简单方法是像你一样在你的类中有一个指针,并在添加到二叉树期间将其值添加到其父级的值。我也不太理解“弱指针”这个短语,我在某些地方遇到过弱函数这个名字,这意味着一个函数是多态的,以至于某些函数以某种方式存在,直到它被用户定义为不同的方式。所以说 cpp 中没有弱指针。这只是一个指针。

我建议按照以下几行做一些事情。将节点中指向父节点的指针设置为默认为 NULL。这将帮助您以后更好地调试和控制您的指针。之后,您将必须指定节点的父节点并将其地址存储在要存储它的节点的指针中。我认为最好和最简单的选择是,就像您填写节点的数据一样除了二叉树之外,还要将要添加到的特定父级作为参数添加。很可能在二叉树节点填充期间,您将遍历特定数量的数据以及节点,获取特定的父节点不会那么困难。

在此示例中,让类节点成为您的节点:


class node
{
public:
    int someData;
    node *parentNode = NULL;
//...
}

还有你的 BinaryTree 类:

class BinaryTree
{
vector<node> nodes; //doesnt really matter what way the nodes are saved. these could also be saved via dynamic variables. doesnt matter.

void add(string data)
{
//adding to this binary tree works via:
//check from root to endpoint if value is larger or smaller. if node has 2 children, enter one of them depending on the value if smaller or bigger repeat until space for variable found (this is basic binary tree so i wont explain the theory)
//What you do is you iterate through these paths as stated above until you get to the 'parent' you want to add the child to. Instead of then just adding it from parent, also set the parent variable to the variable of the parent from the iteration you've been doing to get here. If its a normal variable you need to dereference it via '&' else without.
}
}

假设您通过 fstream 从硬盘驱动器上的物理文件将数据输入到二叉树中。然后,您到目前为止使用的代码和上述更改将起作用:


int main()
{
//...
BinaryTree bt;
fstream file;
file.open();
//...
while(for every line in file)
{
string data = line from file;
bt.add(data);
}


return 0;
}

希望对您有所帮助

【讨论】:

    猜你喜欢
    • 2019-03-12
    • 2023-03-20
    • 2021-02-26
    • 2021-07-22
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多