Question: I need a pseudocode and algorithm for this following code. along with three output class Node { String key; Node left, right; public Node (

I need a pseudocode and algorithm for this following code. along with three output
class Node {
String key;
Node left, right;
public Node(String item){
key = item;
left = right = null;
}
}
public class BinarySearchTree {
Node root;
public BinarySearchTree(){
root = null;
}
// Function to insert a new student into the BST
public void insert(String key){
root = insertRec(root, key);
}
private Node insertRec(Node root, String key){
if (root == null){
root = new Node(key);
return root;
}
if (key.compareTo(root.key)<0){
root.left = insertRec(root.left, key);
} else if (key.compareTo(root.key)>0){
root.right = insertRec(root.right, key);
}
return root;
}
// Function to search for a student in the BST
public boolean search(String key){
return searchRec(root, key);
}
private boolean searchRec(Node root, String key){
if (root == null || root.key.equals(key)){
return root != null;
}
if (key.compareTo(root.key)<0){
return searchRec(root.left, key);
} else {
return searchRec(root.right, key);
}
}
// Function to delete a student from the BST
public void delete(String key){
root = deleteRec(root, key);
}
private Node deleteRec(Node root, String key){
if (root == null){
return root;
}
if (key.compareTo(root.key)<0){
root.left = deleteRec(root.left, key);
} else if (key.compareTo(root.key)>0){
root.right = deleteRec(root.right, key);
} 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;
}
private String minValue(Node root){
String minValue = root.key;
while (root.left != null){
minValue = root.left.key;
root = root.left;
}
return minValue;
}
// Traversal methods
public void preOrderTraversal(){
preOrderTraversalRec(root);
}
private void preOrderTraversalRec(Node root){
if (root != null){
System.out.print(root.key +"");
preOrderTraversalRec(root.left);
preOrderTraversalRec(root.right);
}
}
public void inOrderTraversal(){
inOrderTraversalRec(root);
}
private void inOrderTraversalRec(Node root){
if (root != null){
inOrderTraversalRec(root.left);
System.out.print(root.key +"");
inOrderTraversalRec(root.right);
}
}
public void postOrderTraversal(){
postOrderTraversalRec(root);
}
private void postOrderTraversalRec(Node root){
if (root != null){
postOrderTraversalRec(root.left);
postOrderTraversalRec(root.right);
System.out.print(root.key +"");
}
}
public static void main(String[] args){
BinarySearchTree bst = new BinarySearchTree();
// Inserting students
bst.insert("John");
bst.insert("Alice");
bst.insert("Bob");
bst.insert("Charlie");
// Performing search operation
System.out.println("Is Bob present in the tree? "+ bst.search("Bob"));
// Performing deletions
bst.delete("Bob"); // Deleting a leaf node
bst.delete("Alice"); // Deleting a node with one child
bst.delete("John"); // Deleting a node with two children
// Performing traversals
System.out.println("Pre-order traversal:");
bst.preOrderTraversal();
System.out.println();
System.out.println("In-order traversal:");
bst.inOrderTraversal();
System.out.println();
System.out.println("Post-order traversal:");
bst.postOrderTraversal();
System.out.println();
}
}

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!