开发者

C: How add nodes to a tree in a loop?

开发者 https://www.devze.com 2023-02-19 03:53 出处:网络
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:

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开发者_Go百科.

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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消