C:如何在循环中向树添加节点?

发布于 2024-10-24 04:43:24 字数 747 浏览 8 评论 0原文

我制作了一个只有根的树结构。然后由用户来创建节点并将它们作为子节点添加到根节点或其他现有节点。结构如下:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心是晴朗的。 2024-10-31 04:43:24

您正在将临时值添加到子列表中:

node newNode;

这会在堆栈上创建一个值,该值仅存在于以下“}”之前。下一次迭代很可能会为新对象重新使用相同的内存块。您需要动态创建对象而不是堆栈分配它:

node *newNode = malloc sizeof (node);
newNode->id = i;
addChild (newNode, &Root);

您确实需要确保正确释放节点,否则您将泄漏内存。释放节点内存的函数应该为每个子节点递归调用自身。

You are adding temporary values to the child list:

node newNode;

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:

node *newNode = malloc sizeof (node);
newNode->id = i;
addChild (newNode, &Root);

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.

仅此而已 2024-10-31 04:43:24

这对我来说就像是家庭作业,所以我会尽力提供指导而不是明确的答案。

在此循环中:

for (i=0; i<4; i++){
    node newNode;   // <-- newNode is created on the stack here .
    newNode.id=i;
    addChild(&newNode, &Root);  
                    // <-- and is removed from the stack here.
}

newNode 在循环的每次迭代中都会在堆栈上创建,并且仅在迭代结束之前有效。

为了让它活得更久,你需要在堆上为其分配一些内存。看一下 malloc()free()

This looks like homework to me so I'll try to offer guidance rather than a definitive answer.

In this loop:

for (i=0; i<4; i++){
    node newNode;   // <-- newNode is created on the stack here .
    newNode.id=i;
    addChild(&newNode, &Root);  
                    // <-- and is removed from the stack here.
}

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() and free().

山有枢 2024-10-31 04:43:24

与亚历克西斯不同,我在算法的这个级别上看不到任何东西,但我不确定您是否正在跳过细节或感到困惑。

for (i=0; i<4; i++){ // I think you mean 5 here -- or rather MAXCHILDREN.
        node newNode; // This is a temporary on the stack.
        newNode.id=i;
        addChild(&newNode, &Root);   // &newNode is a pointer to that temporary.
    }

所以addChild每次都会得到一个指向临时newNode的指针。它恰好位于堆栈上的同一地址,因此它们都指向同一位置。所以他们都有相同的 ID。不过,该堆栈位置可能已被覆盖,因此它可能是一些完全随机的值。

for (i=0; i<MAXCHILDREN; i++){
        node *newNode = new node(); // C++, or use malloc if you're stuck in C
        newNode->id=i;
        addChild(newNode, &Root);  
    }

应该让你更接近你想做的事情。

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.

for (i=0; i<4; i++){ // I think you mean 5 here -- or rather MAXCHILDREN.
        node newNode; // This is a temporary on the stack.
        newNode.id=i;
        addChild(&newNode, &Root);   // &newNode is a pointer to that temporary.
    }

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.

for (i=0; i<MAXCHILDREN; i++){
        node *newNode = new node(); // C++, or use malloc if you're stuck in C
        newNode->id=i;
        addChild(newNode, &Root);  
    }

Should get you closer to what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文