C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 8th, 2013, 11:47 AM
asdk77 asdk77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 asdk77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 19 m 31 sec
Reputation Power: 0
How to copy a tree

I want to copy a part of a tree. And I have found the node as the beginning(like root). How can I copy the tree from this node as a whole?
Thanks in advance!

Reply With Quote
  #2  
Old January 8th, 2013, 01:04 PM
salem's Avatar
salem salem is offline
Contributed User
Click here for more information
 
Join Date: Jun 2005
Posts: 3,838 salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)salem User rank is General 12nd Grade (Above 100000 Reputation Level)  Folding Points: 153 Folding Title: Novice Folder
Time spent in forums: 2 Months 3 Weeks 2 Days 17 h 58 m 15 sec
Reputation Power: 1774
C or C++?

What sort of "tree" utility functions do you have, say
- create node
- insert node
- copy node
- traverse tree
and so on.

In essence, it's just traversal + node copy + insert
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

Reply With Quote
  #3  
Old January 9th, 2013, 12:05 PM
asdk77 asdk77 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 17 asdk77 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 5 h 19 m 31 sec
Reputation Power: 0
Quote:
Originally Posted by salem
C or C++?

What sort of "tree" utility functions do you have, say
- create node
- insert node
- copy node
- traverse tree
and so on.

In essence, it's just traversal + node copy + insert

in C
copy node. copy the children nodes from one node(including this parent node). I think the structure variable can be assigned value directly, so I can write codes like: new_node = node; new_node->children[0] = node->children[0]; recursively.
Thanks!

Reply With Quote
  #4  
Old January 9th, 2013, 01:45 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,130 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 19 h 43 m 21 sec
Reputation Power: 1949
Since it's not clear which you're describing, we should discuss the concept of deep copy vs shallow copy when pointers are involved. Basically, with a shallow copy you have the copy pointing to the exact same data location as the original did, but with a deep copy you also create copies of what the original pointed to so that both the original and the copy has their very own and separate data. With the result of a shallow copy, if either the original or the copy changes the data, both the original and the copy will be affected, but with the result of a deep copy a change in the data of one will not affect the other.

A simple example would be copying a C-style string:
Code:
    char *dest;
    char *src = "A string.";

    // shallow copy
    dest = src;  // now both pointers point to the same string

    // deep copy
    dest = malloc(strlen(src)+1);
    strcpy(dest, src);


So to deep-copy the subtree it would be:
malloc root node of the new tree, copy the node's data fields to the new node.
malloc a child node and assign the pointer to the new root node's children field, then copy the child's data to the new child.
Repeat.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > How to copy a tree

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap