我想构建一个n维树。我使用vector
来存储每个节点的子节点。我写的代码给出了“堆栈溢出错误”,我不知道为什么,我使用new
。如果有人能告诉我我出错的地方,我将非常感激。当我尝试构建八叉树结构时堆栈溢出
class Node
{
public:
int q_number;
int layer;
int value;
vector<Node*> n_list;
Node(int n):q_number(n),n_list(n) //initialize node vector
{
}
};
Node* buildtree(int n,int depth)
{
Node * node = new Node(depth);
if(n==depth-1)
{
for(int i = 0; i<depth;i++)
{
node->n_list[i] = NULL;
node->n_list[i]->value = i;
node->n_list[i]->layer = depth-1;
}
}
else
{
for (int i =0;i<depth;i++)
{
node->n_list[i] = buildtree(n++,depth);// span the tree recursively
node->n_list[i]->value = i;
node->n_list[i]->layer = n; // the layer value
}
}
return node;
}
int main()
{
Node * tree = buildtree(0,8); // build an octree
}
请在您的帖子中缩进您的代码。 – Appleshell
我想知道如果你发现这个网站是因为“堆栈溢出”错误?鉴于这是你的第一个问题,很有可能 –
我认为我们不应该帮助黑魔王建立一个圣殿,它肯定会对我们人类造成严重的后果。 –