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? 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
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.
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
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |