Re: Specific Cloning Question




"Alex" <alexander.webb@xxxxxxxxx> wrote in message news:1159181125.560249.98400@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Oliver Wong wrote:
"Alex" <alexander.webb@xxxxxxxxx> wrote in message
news:1158934564.874336.109020@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> I am not entirely sure what you mean by a "recursive solution". I have
> tried cloning the tree by cloning all the nodes in it. However the
> problem here is that when I try to clone Node A it has a reference to
> Node B as its parent so Node B gets cloned. But when I am cloning Node
> B, it has a reference to Node A as its child it goes back to trying to
> clone A again.

If you only clone in "one direction", you can avoid this problem. For
example, cloning a node Foo wil clone it, and all of its children, but none
of its parents.

Alternatively, you could wrap all your nodes in an actual Tree object,
and put a clone method on the tree, and not on any of the nodes themselves,
and have the tree orchestrating the cloning of the nodes.

- Oliver

Ok, so I think it's the second thing that I have tried to do. But the
problem is I still need to clone the actual nodes otherwise the cloned
tree with have references to the same original nodes and when I change
one, it will change the other. And it's the cloning of the nodes that
seems to cause the problem.

Right, when you ask the tree to clone itself, the new tree would scan the old tree, perhaps in a depth first traversal, and recreate in itself the exact same node structure (by creating entirely new nodes, not by referencing the old existing nodes).


With the first suggestion, it sounds good. I am not entirely sure how
to work my way down the tree from the top and make sure I clone all
nodes since at each point, we branch and have two more nodes to clone.
I guess if the tree isn't too big it's not too bad to keep a list of
things that need to be cloned. Any idea?

You don't need to keep an explicit list if you use a recursive solution instead (I know, I'm beginning to sound like a broken record). There are (at least) two ways to implement a tree structure. There's the one where "Tree" is a seperate class which hosts a bunch of nodes (as above); and then there's the one where there is no explicit "Tree" object, and every "Node" is itself a tree, and trees can be sub-trees of other trees. So for example the leaves themselves are trees (of height 0). If you use the latter implementation, then you can implementing cloning a tree by recursively cloning the subtrees, and then assembling them together into a bigger tree.

- Oliver

.



Relevant Pages

  • Re: Specific Cloning Question
    ... tried cloning the tree by cloning all the nodes in it. ... problem here is that when I try to clone Node A it has a reference to ... Alternatively, you could wrap all your nodes in an actual Tree object, ... public MyNode subtreeCopy{ ...
    (comp.lang.java.help)
  • Re: Specific Cloning Question
    ... tried cloning the tree by cloning all the nodes in it. ... problem here is that when I try to clone Node A it has a reference to ... it has a reference to Node A as its child it goes back to trying to ... Alternatively, you could wrap all your nodes in an actual Tree object, ...
    (comp.lang.java.help)
  • Re: Specific Cloning Question
    ... tried cloning the tree by cloning all the nodes in it. ... problem here is that when I try to clone Node A it has a reference to ... Node B as its parent so Node B gets cloned. ... Alternatively, you could wrap all your nodes in an actual Tree object, and put a clone method on the tree, and not on any of the nodes themselves, and have the tree orchestrating the cloning of the nodes. ...
    (comp.lang.java.help)
  • Re: What about cloning cattle and the FDA not informing us...
    ... Then stay very far away from any tree fruit, nuts, vine fruits, garlic, ... "cloning" that has been practiced for centuries. ... the term "clone" that is in many nursery and garden catalogs. ... Why can't I get a packet of seeds for it? ...
    (misc.health.alternative)
  • Re: Specific Cloning Question
    ... I am not entirely sure what you mean by a "recursive solution". ... tried cloning the tree by cloning all the nodes in it. ... problem here is that when I try to clone Node A it has a reference to ... you could wrap all your nodes in an actual Tree ...
    (comp.lang.java.help)