C:如何在循环中向树添加节点?
我制作了一个只有根的树结构。然后由用户来创建节点并将它们作为子节点添加到根节点或其他现有节点。结构如下:
#define MAXCHILDREN 5 //max children a node can have
typedef struct node{
unsigned int id;
struct node* parent;
struct node* children[MAXCHILDREN];
}node;
typedef struct spantree{
node root;
}spantree;
现在我想要实现的是创建 4 个节点并将它们作为子节点添加到根。这是我所做的,但不能正常工作:
for (i=0; i<4; i++){
node newNode;
newNode.id=i;
addChild(&newNode, &Root);
}
当我运行时,这不起作用
showChildId(0, &root); //其中第一个数字表示第一个孩子、第二个孩子等。
showChildId(1, &root);
等等
我得到相同的 id,这意味着只添加了一个孩子。那么我该如何继续创建 4 个不同节点并将它们添加到循环中的父节点(在本例中为根节点)?
I made a tree structure that only has a root. Then it's up to the user to make nodes and either add them as children to the root or an other existing node. Here's how the structure looks:
#define MAXCHILDREN 5 //max children a node can have
typedef struct node{
unsigned int id;
struct node* parent;
struct node* children[MAXCHILDREN];
}node;
typedef struct spantree{
node root;
}spantree;
Now what I want to achieve is to make say 4 nodes and add them as children to the root. Here is what I have done which doesn't work properly:
for (i=0; i<4; i++){
node newNode;
newNode.id=i;
addChild(&newNode, &Root);
}
That doesn't work as when I run
showChildId(0, &root); //where the first number denotes first child, second child etc.
showChildId(1, &root);
etc
I get the same id which mean that there is only one child added. So how do I go on making 4 different nodes and adding them to a parent(root in this case) in a loop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在将临时值添加到子列表中:
这会在堆栈上创建一个值,该值仅存在于以下“}”之前。下一次迭代很可能会为新对象重新使用相同的内存块。您需要动态创建对象而不是堆栈分配它:
您确实需要确保正确释放节点,否则您将泄漏内存。释放节点内存的函数应该为每个子节点递归调用自身。
You are adding temporary values to the child list:
This creates a valus on the stack that only exists until the following '}'. The next iteration will most likely re-use the same chunk of memory for the new object. You need to dynamically create the object rather than stack allocating it:
You do need to make sure the nodes are freed correctly, otherwise you'll leak memory. The function that frees the node memory should recursively call itself for each child node.
这对我来说就像是家庭作业,所以我会尽力提供指导而不是明确的答案。
在此循环中:
newNode
在循环的每次迭代中都会在堆栈上创建,并且仅在迭代结束之前有效。为了让它活得更久,你需要在堆上为其分配一些内存。看一下
malloc()
和free()
。This looks like homework to me so I'll try to offer guidance rather than a definitive answer.
In this loop:
newNode
is created on the stack every iteration of the loop and it is only valid until the end of the iteration.To make it live longer you need to allocate some memory for it on the heap. Have a look at
malloc()
andfree()
.与亚历克西斯不同,我在算法的这个级别上看不到任何东西,但我不确定您是否正在跳过细节或感到困惑。
所以addChild每次都会得到一个指向临时newNode的指针。它恰好位于堆栈上的同一地址,因此它们都指向同一位置。所以他们都有相同的 ID。不过,该堆栈位置可能已被覆盖,因此它可能是一些完全随机的值。
应该让你更接近你想做的事情。
Unlike Alexis, I see nothing right here at this level of the algorithm, but I'm not sure if you're skipping details or confused.
So addChild gets a pointer to a temporary newNode each time. It will happen to be at the same address on the stack, so they'll all point to the same place. So they'll all have the same ID. That stack location may have been overwritten by then though, so it may be some completely random value.
Should get you closer to what you want to do.