c++ - 如何在 C++ 中有效地返回树中的节点列表

标签 c++ list tree binary-tree std

这是我的 Node 类的简化版本:

class Node {
public:
    Node();

    // indicators of whether the node is a top or bottom node
    bool Top;
    bool Bot;

    // pointers for tree structure
    Node *Parent;
    Node *LeftC;
    Node *RightC;

    std::list<Node*> getNodesList();
};

我想要的是能够按特定顺序获得指向树中节点的指针列表。我尝试了以下代码来执行此操作:

std::list<Node*> Node::getNodesList(){
    if (Bot) return (std::list<Node*>(1,this));
    else {
        std::list<Node*> temp (1,this);
        temp.splice(temp.end(), LeftC->getNodesVector()); // Combine with left childrens
        temp.splice(temp.end(), RightC->getNodesVector()); // Combine with right childrens
        return temp;
    }
}

拼接功能不起作用并给我一个错误。

所以我的问题是:

  • 为什么 splice 函数无法组合列表?
  • 是否有更有效的方法来返回指向节点的指针列表?

最佳答案

因为我不知道你的确切错误,快速浏览一下你的代码就会告诉我你的 Node 类可能不知道什么 getNodesVector()是因为它没有在您的类(class)中定义。

关于c++ - 如何在 C++ 中有效地返回树中的节点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864708/

相关文章:

list - Scala Map[String, MutableList]()

javascript - 始终触发 jstree 事件

c++ - sizeof(...) = 0 或 C++ 模板中的条件变量声明

c# - 列表 'Except' 比较 - 忽略大小写

使用带有虚函数的协变返回类型的 C++ 无效转换错误

python - 编写这个 for 循环的更有效方法?

c# - .NET 数据绑定(bind) - 文件夹和项目的递归树的自定义数据源

java - 将 yaml 文件解析为树

c++ - 如何检查 operator== 是否存在?

c++ - 类型别名和自引用