473,465 Members | 1,934 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

clone a tree

jw
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree like
in the preorder tecnique and add all the nodes to another root
is there a common way to do this?

Dec 21 '05 #1
6 6722
standard knows nothing about binary trees and therefore there's no
standard way to do this, it all depends on your particular
implementation of binary tree

Dec 21 '05 #2

jw wrote:
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree like
in the preorder tecnique and add all the nodes to another root
is there a common way to do this?


that would likely be it.

Dec 21 '05 #3
jw wrote:
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree
like in the preorder tecnique and add all the nodes to another root
is there a common way to do this?


I haven't tested this, but I think it would do the trick:

struct Node
{
Node *left;
Node *right;
Node *clone();
};

Node *Node::clone()
{
Node *node = new Node;
if(left)
node->left = left->clone();
if(right)
node->right = right->clone();
return node;
}

Given the root node, the entire tree will be cloned by:
Node *rootClone = root->clone();

DW
Dec 21 '05 #4
David White wrote:
I haven't tested this, but I think it would do the trick:

struct Node
{
Oops, you need this as well:
Node() : left(0), right(0) {}
Node *left;
Node *right;
Node *clone();
};

Node *Node::clone()
{
Node *node = new Node;
if(left)
node->left = left->clone();
if(right)
node->right = right->clone();
return node;
}


DW
Dec 21 '05 #5
jw wrote:
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree like
in the preorder tecnique and add all the nodes to another root
is there a common way to do this?


Tree newTree = oldTree;

Seriously, it's hard to offer much help if we have no idea what your
data structure looks like (not to mention that this is OT without any
reference to C++). Knowing nothing else, I would suggest a recursive
approach: clone the left and right children and attach them to the
cloned root.
Dec 21 '05 #6
jw wrote:
hi all i have a binary tree and want to clone this, is there a
standard way to do this,i think i can do it by traversing the tree like
in the preorder tecnique and add all the nodes to another root
is there a common way to do this?


Why not use the C++ STL library containers std::set or std::map which
can easily be clone via copy constructor?

Many binary tree requirements can be accomplished via std::set or
std::map containers.

Dec 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jason Evans | last post by:
Hi All, I am writing my own implementation of queue via a linked list, note not a LinkedList, and was running into trouble with the clone method. I was wondering if anyone could point out some...
6
by: Steve | last post by:
I can't find a straight answer on what to use? I need a deep copy, so I implemented IConeable and the Clone() method. However, I'm not sure I did it correct. Is it suposed to be an allocation of...
4
by: Brian Keating | last post by:
Hi there, Consider this from MSDN *Notes to Inheritors When you derive from DataGridViewCheckBoxCell and add new properties to the derived class, be sure to override the Clone method to copy the...
2
by: bclegg | last post by:
Hi, When I implement the example code it errors with 'No parameterless constructor defined for this object.' experimenting with dim o as object = mytreenode.clone() gives the same error. The...
1
by: Alex D. | last post by:
hi guys. I need to clone multiple times an object and I am succesfully cloning using the regular serialization process, using a MemoryStream. My problem is that after cloning the object more that...
2
by: Steven | last post by:
Hi, I have created my own node (class MyNode : TreeNode) for a TreeView. To populate the treeview, i use something like MyNode newNode = new MyNode("Bla bla bla","0","1") for example. But, to...
16
by: Hamed | last post by:
Hello I am developing a utility to be reused in other programs. It I have an object of type Control (a TextBox, ComboBox, etc.) that other programmers use it in applications. they may set some...
14
by: Hamed | last post by:
Hello It seems that I should implement ICloneable to implement my own clone object. the critical point for me is to make a control object based on another control object that all of its event...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.