Question
Please help Below is my code in java. class BinarySearchTree { class Node { int key; Node left, right; public Node(int item) { key =
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.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 objectsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started