Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

The World Wide Web And Databases International Workshop Webdb 98 Valencia Spain March 27 28 1998 Selected Papers Lncs 1590

Authors: Paolo Atzeni ,Alberto Mendelzon ,Giansalvatore Mecca

1st Edition

3540658904, 978-3540658900

More Books

Students also viewed these Databases questions

Question

6. Explain the power of labels.

Answered: 1 week ago

Question

10. Discuss the complexities of language policies.

Answered: 1 week ago