这是我的 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/