Java Binary Tree Cloning Problem?

This can be done if you set up copy on write in the structure: in Item */ Item add(String value){ Item item = new Item(this. Value); if(value. CompareTo(this.

Value)Right = this.right. Add(value); }else{ item. Left = this.left.

Add(value); item. Right = this. Right; } return item; } then the cloning is only copying the root to the other tree.

This can be done if you set up copy on write in the structure: /*in Item */ Item add(String value){ Item item = new Item(this. Value); if(value. CompareTo(this.

Value)Right = this.right. Add(value); }else{ item. Left = this.left.

Add(value); item. Right = this. Right; } return item; } then the cloning is only copying the root to the other tree.

You could write a clone method for a specific node. This method creates a new node with the value of the current node. Result of the method would be the cloned node.In the method you could call the clone method for the right and left node recursively.

The result of the calls will be set as right and left node of the cloned node. This should do the trick. The code would look something like this public Item cloneItem() { Item cloneLeft = left.cloneItem(); Item cloneRight = right.cloneItem(); return new Item(value, cloneLeft, cloneRight); } You have to check if left and/or right are set which I have left out in the example.

Question says "needs to be done without recursion" – MarcoS May 18 at 10:30.

The final for the left and right sub tree's don't make much sense. Making them final, the tree is essentially immutable, and as such an insert operation does not make sense. Anyway, this code should hopefully warm up the thinking process.

Public class Node { final String value; Node left; Node right; boolean copyOnWrite; //google it for more information public Node(String value,Node left,Node right,boolean copyOnWrite) { //blabla } public Node copy() { //not recursive! CopyOnWrite = true; return new Node(value,left,right,true); } public Node deepCopy() { //recursive! Return new Node(value,left.deepCopy(),right.deepCopy(),false); } public void insert(String value) { if (copyOnWrite) { copyOnWrite = false; left = left.deepCopy(); right = right.deepCopy(); } //normal tree insert code } So, calling copy() is fast it makes a shallow copy.

A deepCopy() is only performed when an attempt is made to modify it. Actually, there is no need to make a deep copy of the entire tree , but since this is homework, I leave that(how many copies are needed? What nodes become 'dirty'?) as an exercise :).

The simple straightforward answer is to make a Cell object and have the tree node reference it rather than the data of the cell within the node.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions