Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help Below is my code in java. class BinarySearchTree { class Node { int

ID: 3822228 • Letter: P

Question

Please help

Below is my code in java.

class BinarySearchTree {

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

}
  
}

// Root of BST

Node root;

// Constructor

BinarySearchTree() {

root = null;

}

// This method mainly calls insertRec()

void insert(int key) {

root = insertRec(root, key);

}

/* A recursive function to insert a new key in BST */

Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */

if (root == null) {

root = new Node(key);

return root;

}

/* Otherwise, recur down the tree */

if (key < root.key)

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */

return root;

}

// This method mainly calls InorderRec()

void inorder() {

inorderRec(root);

}

// A utility function to do inorder traversal of BST

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.println(root.key);

inorderRec(root.right);

}

}

// Driver Program to test above functions

public static void main(String[] args) {

BinarySearchTree tree = new BinarySearchTree();

tree.insert(50);

tree.insert(30);

tree.insert(20);

tree.insert(40);

tree.insert(70);

tree.insert(60);

tree.insert(80);

// print inorder traversal of the BST

tree.inorder();
}
}

l. Implement Search T data in Bin to search for a node having value of key data in a Binary Search Tree. See the algorithm presented below. TREE-SEARCH(x, k) 1 if x NIL or k x.key return x 3 if k x. key 4 return TREE-SEARCH (x.left, k) 5 else return TREE-SEARCH(x.right, k) 2. Implement Delete (T data) in Binary SearchTreejava to search for a node having value of key data in a Binary Search Tree. See the two algorithms presented below. TREE-DELETE (T, z) 1 if left NIL 2 TRANSPLANT(T, z, z. right 3 elseif z right NIL 4 TRANSPLANT(T, z, z. left) 5 else y B TREE-MINIMUM(z.right) TRANSPLANT(T,y,y.right) y. right z. right y right p y 10 TRANSPLANT(T, z, y) 12 y.left.p CS303 Lab Assignment 6 of 7 TRANSPLANT(T, u, v) 1 if u.p NIL Troot v 3 elseif u u.p left 5 else u.p. right v 6 if v NIL u.P 3. Exercise your Binary Search Tree by selectively inserting and removing items, and using In-order-Traversal at each step to show the tree's contents at each step At the maximum, your tree should contain at least ten objects.

Explanation / Answer

Hi, I have added required functionality.

Please test and let me know in case of any issue.

public class BinarySearchTree {

   class Node {

       int key;

       Node left, right;

       public Node(int item) {

           key = item;

           left = right = null;

       }

   }

   // Root of BST

   Node root;

   // Constructor

   BinarySearchTree() {

       root = null;

   }

   // This method mainly calls insertRec()

   void insert(int key) {

       root = insertRec(root, key);

   }

   /* A recursive function to insert a new key in BST */

   Node insertRec(Node root, int key) {

       /* If the tree is empty, return a new node */

       if (root == null) {

           root = new Node(key);

           return root;

       }

       /* Otherwise, recur down the tree */

       if (key < root.key)

           root.left = insertRec(root.left, key);

       else if (key > root.key)

           root.right = insertRec(root.right, key);

       /* return the (unchanged) node pointer */

       return root;

   }

   // This method mainly calls InorderRec()

   void inorder() {

       inorderRec(root);

   }

   // A utility function to do inorder traversal of BST

   void inorderRec(Node root) {

       if (root != null) {

           inorderRec(root.left);

           System.out.println(root.key);

           inorderRec(root.right);

       }

   }

   public boolean search(int key){

       return searchUtil(root, key);

   }

   // A utility function to search a given key in BST

   private boolean searchUtil(Node root, int key)

   {

       // Base Cases: root is null or key is present at root

       if (root==null)

           return false;

       if (root.key==key)

           return true;

       // val is greater than root's key

       if (root.key > key)

           return searchUtil(root.left, key);

       // val is less than root's key

       return searchUtil(root.right, key);

   }

   // This method mainly calls deleteRec()

   void deleteKey(int key)

   {

       root = deleteRec(root, key);

   }

   /* A recursive function to insert a new key in BST */

   Node deleteRec(Node root, int key)

   {

       /* Base Case: If the tree is empty */

       if (root == null) return root;

       /* Otherwise, recur down the tree */

       if (key < root.key)

           root.left = deleteRec(root.left, key);

       else if (key > root.key)

           root.right = deleteRec(root.right, key);

       // if key is same as root's key, then This is the node

       // to be deleted

       else

       {

           // node with only one child or no child

           if (root.left == null)

               return root.right;

           else if (root.right == null)

               return root.left;

           // node with two children: Get the inorder successor (smallest

           // in the right subtree)

           root.key = minValue(root.right);

           // Delete the inorder successor

           root.right = deleteRec(root.right, root.key);

       }

       return root;

   }

   int minValue(Node root)

   {

       int minv = root.key;

       while (root.left != null)

       {

           minv = root.left.key;

           root = root.left;

       }

       return minv;

   }

   // Driver Program to test above functions

   public static void main(String[] args) {

       BinarySearchTree tree = new BinarySearchTree();

       tree.insert(50);

       tree.insert(30);

       tree.insert(20);

       tree.insert(40);

       tree.insert(70);

       tree.insert(60);

       tree.insert(80);

       // print inorder traversal of the BST

       tree.inorder();

   }

}