Copying a Tree

 


public Node insert(Node node,int data)
 {
 if(node==null)
 {
 node = new Node(data);
 }

 else if(data < node.data)
 {
 node.left = insert(node.left,data);
 node.left.parent = node;
 }
 else
 {
 node.right = insert(node.right,data);
 node.right.parent = node;
 }

 return node;
 }

 public void preorderT()
 {
 preorderT(root);
 }

 public void preorderT(Node node)
 {
 if(node!=null)
 {
 System.out.println(" "+node.data);
 preorderT(node.left);
 preorderT(node.right);
 }
 }


 public Node copy(Node node)
 {

 if(node==null)
 {
 return null;
 }
 else
 {
 Node copy = new Node(node.data);
 copy.left = copy(node.left);
 copy.right= copy(node.right);

 return copy;
 }


 }

&nbsp;

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: