# How to copy a tree in C

Page 1 of 1

## 1 Replies - 521 Views - Last Post: 23 January 2013 - 10:45 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=308993&amp;s=49450ca4b573e43fbe6007a288158beb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 asdk77

Reputation: 0
• Posts: 6
• Joined: 16-November 12

# How to copy a tree in C

Posted 23 January 2013 - 09:53 PM

I want to copy a part of a tree.
```struct node
{
...
int num_number; //number of children nodes
node **children; //pointer point to children nodes. When I copy the node, how can I deal with **children? Right now, there is no children yet.
}
void copy_tree(node *node, node *T)
{
int i;
char number[12] = {0}; //used in case of if
int n = node->num_children;
if (node == NULL)
T = NULL;
if（...)
{
.... // copy the node
}
else
{
....  // copy the node
}
for(i=0; i<n; i++)
copy_tree(node->children[i], T->children[i]); // I want to copy the children nodes recursively, but the new node has no children; that is T->children not existing, how can I copy the children nodes and allocate to parent?

```

I hope someone can give some tips.
Thanks!

Is This A Good Question/Topic? 0

## Replies To: How to copy a tree in C

### #2 Martyr2

• Programming Theoretician

Reputation: 5205
• Posts: 13,951
• Joined: 18-April 07

## Re: How to copy a tree in C

Posted 23 January 2013 - 10:45 PM

Go check out the answer I posted on codecall... you know, that other place you are obviously plastering your question to.

Thanks!