Question: Implemented in Java CODE WILL BE AT BOTTOM import java.io.*; import java.util.*; class Node { int data; Node left, right; Node(int data) { this.data =

Implemented in Java

CODE WILL BE AT BOTTOM

Implemented in Java CODE WILL BE AT BOTTOM import java.io.*; import java.util.*;

class Node { int data; Node left, right; Node(int data) { this.data

= data; } } public class GenericBST { private Node root; public

void insert(int data) { root = insert(root, data); } private Node insert(Node

import java.io.*;

import java.util.*;

class Node

{

int data;

Node left, right;

Node(int data)

{

this.data = data;

}

}

public class GenericBST

{

private Node root;

public void insert(int data)

{

root = insert(root, data);

}

private Node insert(Node root, int data)

{

if (root == null)

{

return new Node(data);

}

else if (data

{

root.left = insert(root.left, data);

}

else if (data > root.data)

{

root.right = insert(root.right, data);

}

else

{

;

}

return root;

}

public void delete(int data)

{

root = delete(root, data);

}

private Node delete(Node root, int data)

{

if (root == null)

{

return null;

}

else if (data

{

root.left = delete(root.left, data);

}

else if (data > root.data)

{

root.right = delete(root.right, data);

}

else

{

if (root.left == null && root.right == null)

{

return null;

}

else if (root.right == null)

{

return root.left;

}

else if (root.left == null)

{

return root.right;

}

else

{

root.data = findMax(root.left);

root.left = delete(root.left, root.data);

}

}

return root;

}

private int findMax(Node root)

{

while (root.right != null)

{

root = root.right;

}

return root.data;

}

{

return contains(root, data);

}

private boolean contains(Node root, int data)

{

if (root == null)

{

return false;

}

else if (data

{

return contains(root.left, data);

}

else if (data > root.data)

{

return contains(root.right, data);

}

else

{

return true;

}

}

public void inorder()

{

System.out.print("In-order Traversal:");

inorder(root);

System.out.println();

}

private void inorder(Node root)

{

if (root == null)

return;

inorder(root.left);

System.out.print(" " + root.data);

inorder(root.right);

}

public void preorder()

{

System.out.print("Pre-order Traversal:");

preorder(root);

System.out.println();

}

private void preorder(Node root)

{

if (root == null)

return;

System.out.print(" " + root.data);

preorder(root.left);

preorder(root.right);

}

public void postorder()

{

System.out.print("Post-order Traversal:");

postorder(root);

System.out.println();

}

private void postorder(Node root)

{

if (root == null)

return;

postorder(root.left);

postorder(root.right);

System.out.print(" " + root.data);

}

public static void main(String [] args)

{

BST myTree = new BST();

for (int i = 0; i

{

int r = (int)(Math.random() * 100) + 1;

System.out.println("Inserting " + r + "...");

myTree.insert(r);

}

myTree.inorder();

myTree.preorder();

myTree.postorder();

}

}

In this assignment, you will gain experience worKing with generics in Java. In particular, you will extend my binary search tree (BST) code from a data structure that just holds integers to a powerful container that m is that you get to looking through some fairly straight-forward and well-structured BST code. It is always good to revisit how common data structures can be implemented as you ramp up your skills in different programming languages. You will also gain experience modifying someone else's code (something you will be doing a lot if you become a software developer) and adding comments to code. Deieralbles GenericBST.java (Note: Capitalization of your filename matters!)

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!