Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you please provide me with a method that takes a BST as an input and build another BST(duplicate a tree) having the same values

Can you please provide me with a method that takes a BST as an input and build another BST(duplicate a tree) having the same values and structure. Can u please edit the TestClass and BSTree class for the required input and output

import java.util.Scanner;

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();

} }

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 int getSize() { return size; } public static TreeNode clone (TreeNode root) {if(root==null){return null;} TreeNode temp= new TreeNode(root.data); temp.left=clone(root.left); temp.right=clone(root.right); return temp; }

//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 {

public int data; public TreeNode left; public 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions