Question: i am lokking for code to duplicate a given tree in binary search??.here are test class,tree node and binary search tree public class TestClass{ public

i am lokking for code to duplicate a given tree in binary search??.here are test class,tree node and binary search tree

public class TestClass{ public static void main(String[] args) { BSTree arbol1= new BSTree(); //create first tree

arbol1.inorder(); //traverse the first tree (empty) arbol1.insert(50); //insert first value arbol1.insert(50); //try to add duplicate

arbol1.inorder(); arbol1.insert(55); arbol1.insert(40); arbol1.inorder(); System.out.println(" Size of arbol1 is: "+arbol1.getSize()+ " nodes");

BSTree arbol2= new BSTree(new int[] {10,20,15,45,62,99,36}); System.out.print(" Size of arbol2 is: "+arbol2.getSize()+ " nodes"); arbol2.inorder(); arbol2.preorder(); arbol2.posorder(); arbol2= new BSTree(new int[] {60,20,15,45,62,97,86,61,99}); System.out.print(" Size of arbol2 is: "+arbol2.getSize()+ " nodes"); arbol2.inorder(); arbol2.preorder(); arbol2.posorder(); System.out.print(" Finding 23 in arbol2: " + arbol2.search(23)); System.out.print(" Finding 61 in arbol2: " + arbol2.search(61));

System.out.print(" Finding 23 in arbol2: " + arbol2.RecSearch(23)); System.out.print(" Finding 61 in arbol2: " + arbol2.RecSearch(61)); System.out.print(" Size of arbol2 is: "+arbol2.getSize()+ " nodes: "); arbol2.inorder(); arbol2.remove(97); System.out.print(" Size of arbol2 is: "+arbol2.getSize()+ " nodes: "); arbol2.inorder();

BSTree arbol3= new BSTree(new int[] {60,63,25,22,33}); System.out.print(" Size of arbol3 is: "+arbol3.getSize()+ " nodes: "); arbol3.inorder(); arbol3.preorder(); arbol3.remove(63); System.out.print(" Size of arbol3 is: "+arbol3.getSize()+ " nodes: "); arbol3.inorder(); arbol3.preorder(); }

}

class BSTree { private TreeNode root; //Create a reference that will point to the root node private int size=0; //Number of nodes in the tree

public BSTree() { //Default constructor }

public BSTree(int[] args) { //Use the insertion method to create a tree from an array for(int i=0;i

public boolean isEmpty() { if (size==0) return true; else return false; } public int getSize() { return size; } //The following insertion method adds a node to the tree if the value is not already in public boolean insert(int value) { if(root == null) { //The tree is empty, make the new node root root = new TreeNode(value); } else { TreeNode node = root; TreeNode parent = null; while(node != null) { //This while searched the location of the new node if(value < node.data) { parent = node; node = node.left; } else if(value > node.data){ parent = node; node = node.right; } else { System.out.println(" The value already exists in the tree and cannot be added"); return false; //Exit with false, the node was not added } } node = new TreeNode(value); //create the new node if(value < parent.data) parent.left=node; //add the node on the correct subtree else parent.right=node; } size++; //You have added one more node return true; //The node was successfully added }

public void inorder() { //This is a helper method needed to invoke inorder with parameters System.out.print(" Tree InOrder -> [ "); inorder(root); System.out.print("]"); }

protected void inorder(TreeNode root) { //inorder with parameters allows a recursion if (root == null) return; inorder(root.left); System.out.print(root.data + " "); inorder(root.right); } public void preorder() { //This is a helper method needed to invoke preorder with parameters System.out.print(" Tree PreOrder -> [ "); preorder(root); System.out.print("]"); }

protected void preorder(TreeNode root) { //preorder with parameters allows a recursion if (root == null) return; System.out.print(root.data + " "); preorder(root.left); preorder(root.right); }

public void posorder() { //This is a helper method needed to invoke posorder with parameters System.out.print(" Tree PosOrder -> [ "); posorder(root); System.out.print("]"); }

protected void posorder(TreeNode root) { //posorder with parameters allows a recursion if (root == null) return; posorder(root.left); posorder(root.right); System.out.print(root.data + " "); }

public boolean search(int value) { if (root==null) return false; TreeNode node = root; while(node!=null){ if(value < node.data) node = node.left; else if(value > node.data) node = node.right; else return true; } return false; }

//Helper method for calling the recursive search public boolean RecSearch(int value) { return RecSearch(root,value); }

protected boolean RecSearch(TreeNode root, int value) { if (root==null) //The tree is empty return false; if (root.data==value) //Element is found return true; else //Recursive search on both left and right subtrees return (RecSearch(root.left,value)||RecSearch(root.right,value)); }

public boolean remove(int value) { if(root == null) { //The tree is empty, exit routine with false return false; } else { TreeNode node = root; TreeNode parent = null; while(node != null) { //This while loop searches the element if(value < node.data) { parent = node; node = node.left; } else if(value > node.data){ parent = node; node = node.right; } else { break; //element found, stop the search } } if(node==null) return false; //element not found, exit with false else { //Removing the element after it was located if(node.right==null && node.left==null) { //element is a leaf if(parent==null) { //the tree has only a root node root=null; } else if(parent.left==node) parent.left=null; else parent.right=null; } else { // the element has children if(node.right==null) { //element has only a left tree if(parent==null) //the tree has only a root node root=node.left; else if(parent.left==node) parent.left=node.left; else parent.right=node.left; } else { if(node.left==null) { //element has only a right tree if(parent==null) //the tree has only a root node root=node.right; else if(parent.right==node) parent.right=node.right; else parent.right=node.left; } else { //the element has both children TreeNode aux=parent=node; node=node.left; while(node.right!=null) { parent=node; node=node.right; } aux.data=node.data; if(parent==aux) { parent.left=null; } else parent.right=null; } } } size--; return true; //node has been removed } } } }

class TreeNode { protected int data; protected TreeNode left; protected TreeNode right;

public TreeNode(int nbr) { this.data = nbr; this.left = null; this.right = null; }

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!