Professionally and thoroughly comment on this code.
//BinarySearchTree.java
package Project.bst;
//imports required
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class BSTTreeNode{
BSTTreeNode left, right;
String data;
public BSTTreeNode(){
left = null;
right = null;
data = null;
}
public BSTTreeNode(String n){
left = null;
right = null;
data = n;
}
public void setLeft(BSTTreeNode n){
left = n;
}
public void setRight(BSTTreeNode n){
right = n;
}
public BSTTreeNode getLeft(){
return left;
}
public BSTTreeNode getRight(){
return right;
}
public void setData(String d){
data = d;
}
public String getData(){
return data;
}
}
class BSTDriver{
private BSTTreeNode root;
public BSTDriver(){
root = null;
}
public boolean isEmpty(){
return root == null;
}
public void insert(String data){
root = insert(root, data);
}
private BSTTreeNode insert(BSTTreeNode node, String data){
if (node == null || node.getData() == null)
node = new BSTTreeNode(data);
else {
if (data.compareTo(node.getData()) >= 0)
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
public void delete(String k){
if (isEmpty())
System.out.println("Tree Empty.");
else if (search(k) == false)
System.out.println(k + " is not present.");
else {
root = delete(root, k);
System.out.println(k + " deleted from the tree.");
}
}
private BSTTreeNode delete(BSTTreeNode root, String k){
BSTTreeNode p, p2, n;
if (root.getData().equals(k)){
BSTTreeNode lt, rt;
lt = root.getLeft();
rt = root.getRight();
if (lt == null && rt == null)
return null;
else if (lt == null){
p = rt;
return p;
}
else if (rt == null){
p = lt;
return p;
}
else{
p2 = rt;
p = rt;
while (p.getLeft() != null)
p = p.getLeft();
p.setLeft(lt);
return p2;
}
}
if (k.compareTo(root.getData()) >= 0){
n = delete(root.getLeft(), k);
root.setLeft(n);
}
else{
n = delete(root.getRight(), k);
root.setRight(n);
}
return root;
}
public int countNodes(){
return countNodes(root);
}
private int countNodes(BSTTreeNode r){
if (r == null)
return 0;
else {
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
public boolean search(String val){
return search(root, val);
}
private boolean search(BSTTreeNode r, String val){
boolean found = false;
while ((r != null) && !found) {
int rval = val.compareTo(r.getData());
if (rval > 0)
r = r.getLeft();
else if (rval < 0)
r = r.getRight();
else {
found = true;
break;
}
found = search(r, val);
}
return found;
}
public void inorder(){
inorder(root);
}
private void inorder(BSTTreeNode r){
if (r != null){
inorder(r.getLeft());
System.out.print(r.getData() + " ");
inorder(r.getRight());
}
}
public void preorder(){
preorder(root);
}
private void preorder(BSTTreeNode r){
if (r != null){
System.out.print(r.getData() + " ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
//function for postorder traversal
public void postorder(){
postorder(root);
}
private void postorder(BSTTreeNode r){
if (r != null){
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() + " ");
}
}
}
//class BinarySearchTree
public class BinarySearchTree{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
BSTDriver bst = new BSTDriver();
System.out.println("Binary Search Tree\n");
char ch;
do{
System.out.println("\nBinary Search Tree Operations\n");
System.out.println("1. Add File Data");
System.out.println("2. Delete");
System.out.println("3. Search");
System.out.println("4. Display");
System.out.println("5. Tree Status");
int choice = scan.nextInt();
switch (choice){
case 1:
if (bst.isEmpty()){
List<String> nameList = readFile();
System.out.println("Name received from list"+
nameList.toString());
for (String name : nameList) {
bst.insert(name);
}
}
else{
System.out.println("Record already added into tree.
Enter a different record.");
}
break;
case 2:
System.out.println("Enter name to delete.");
bst.delete(scan.next());
break;
case 3:
System.out.println("Enter name to search");
System.out.println("Search result : " +
bst.search(scan.next()));
break;
case 4:
System.out.print("\nPost order : ");
bst.postorder();
System.out.print("\nPre order : ");
bst.preorder();
System.out.print("\nIn order : ");
bst.inorder();
break;
case 5:
System.out.println("Empty status = " + bst.isEmpty());
System.exit(1);
break;
default:
System.out.println("Wrong Entry \n ");
break;
}
System.out.println("\nDo you want to continue (Type y
or n) \n");
ch = scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
}
private static List<String> readFile(){
BufferedReader br = null;
FileReader fr = null;
List<String> fileContent = new
ArrayList<String>();
try{
fr = new FileReader("bst/bst_name_list");
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null){
fileContent.add(sCurrentLine);
}
}
catch (FileNotFoundException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
finally{
try{
if (br != null)
br.close();
if (fr != null)
fr.close();
}
catch (IOException ex){
ex.printStackTrace();
}
}
return fileContent;
}
}
Solution:
/*
* BinarySearchTree.java
*/
package Project.bst; /*Importing
the required packages..*/
//imports required
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class BSTTreeNode{
/* Main
class..*/
BSTTreeNode left, right; /* Declaring
instances..*/
String data;
/* Declaring variable..*/
/*
* Empty node declaration.
*/
public BSTTreeNode(){
left = null;
right = null;
data = null;
}
/*
* Node creation and Assigning the data..
*/
public BSTTreeNode(String n){ /
left = null;
right = null;
data = n;
/* Assigning the
data.*/
}
/* To set the left node data..*/
public void setLeft(BSTTreeNode n){
left = n;
}
/* To set the right node data..*/
public void setRight(BSTTreeNode n){
right = n;
}
/* To get the left node data..*/
public BSTTreeNode getLeft(){
return left;
}
/* To get the right node data..*/
public BSTTreeNode getRight(){
return right;
}
/* To set the node's data...*/
public void setData(String d){
data = d;
}
/* To get the node's data...*/
public String getData(){
return data;
}
} /*ENDOF class BSTTreeNode*/
/*
* Class : BSTDriver
*/
class BSTDriver{
private BSTTreeNode root; /* Declaring
instance.. */
/* To crete the root node.*/
public BSTDriver(){
root = null;
}
/* To check the root node isEmpty..*/
public boolean isEmpty(){
return root == null;
}
/* To insert the string data into the root
node..*/
public void insert(String data){
root = insert(root, data);
}
/* To insert the data into the respective
node..*/
private BSTTreeNode insert(BSTTreeNode node, String
data){
if (node == null || node.getData()
== null)
node = new
BSTTreeNode(data); /*Create new node..*/
else {
if
(data.compareTo(node.getData()) >= 0)
node.left = insert(node.left, data); /*Assign at
left..*/
else
node.right = insert(node.right, data); /*Assign
at right..*/
}
return node;
/*Return the node.. */
}
/* To delete the data in the respective node..*/
public void delete(String k){
/*To check the tree is empty or
not..*/
if (isEmpty())
System.out.println("Tree Empty.");
/* To search the data in the
tree..*/
else if (search(k) == false)
System.out.println(k + " is not present.");
/* To delete the data from the
tree..*/
else {
root =
delete(root, k);
System.out.println(k + " deleted from the tree.");
}
}
/* To delete the node from the tree..*/
private BSTTreeNode delete(BSTTreeNode root, String
k){
BSTTreeNode p, p2, n; /*Variable declaration..*/
if
(root.getData().equals(k)){ /*Matching the data*/
BSTTreeNode lt,
rt;
lt =
root.getLeft(); /*To get the root's left node*/
rt =
root.getRight(); /*To get the root's right node*/
if (lt == null
&& rt == null)
return null; /*Return
null..*/
else if (lt ==
null){
p = rt;
/*Assign right to the variable..*/
return p;
/*Return the variable 'p'..*/
}
else if (rt ==
null){
p = lt;
/*Assign left to the variable..*/
return p; /*Return the
variable 'p'..*/
}
else{
p2 = rt; /*Assign rightt to the
variable..*/
p = rt; /*Assign
rightt to the variable..*/
while (p.getLeft() != null)
p = p.getLeft(); /*To get the
p's left node*/
p.setLeft(lt);
/*To set the p's left node*/
return p2;
/*Return the variable 'p2'..*/
}
}
if (k.compareTo(root.getData())
>= 0){
n =
delete(root.getLeft(), k); /*To delete root's left node*/
root.setLeft(n);
/*To set the root's left node*/
}
else{
n =
delete(root.getRight(), k); /*To delete root's right
node*/
root.setRight(n); /*To set the root's right node*/
}
return root;
/*Return root..*/
}
/* To count the total no.of nodes in the
tree..*/
public int countNodes(){
return countNodes(root);
}
/* To count the total no.of nodes in the
tree..*/
private int countNodes(BSTTreeNode r){
if (r == null)
/*Check if is null*/
return
0;
else {
int l =
1;
/* To
count the no.of nodes in the left side tree..*/
l +=
countNodes(r.getLeft());
/* To
count the no.of nodes in the right tree..*/
l +=
countNodes(r.getRight());
return
l; /*Return l..*/
}
}
/* To search the string in the tree..*/
public boolean search(String val){
return search(root, val);
}
/* To search the string in the tree..*/
private boolean search(BSTTreeNode r, String
val){
boolean found = false;
while ((r != null) &&
!found) {
/* To compare
the value with the root node's data*/
int rval =
val.compareTo(r.getData());
if (rval >
0)
r = r.getLeft(); /* To get the left
node*/
else if (rval
< 0)
r = r.getRight(); /* To get the right
node*/
else {
found = true;
break; /* Break
the loop..*/
}
found =
search(r, val); /* Search the value..*/
}
return found; /* Return
the found value..*/
}
/* To perform the inorder traversal..*/
public void inorder(){
inorder(root);
}
/* To perform the inorder traversal..*/
private void inorder(BSTTreeNode r){
if (r != null){
inorder(r.getLeft()); /* To get the root's left
node*/
System.out.print(r.getData() + " "); /* Print current node's
data..*/
inorder(r.getRight()); /* To get the root's right node*/
}
}
/* To perform the preorder traversal..*/
public void preorder(){
preorder(root);
}
/* To perform the preorder traversal..*/
private void preorder(BSTTreeNode r){
if (r != null){
System.out.print(r.getData() + " "); /* Print current node's
data*/
preorder(r.getLeft()); /* To get the root's left
node*/
preorder(r.getRight()); /* To get the root's right node*/
}
}
//function for postorder traversal
/* To perform the postorder traversal..*/
public void postorder(){
postorder(root);
}
/* To perform the postorder traversal..*/
private void postorder(BSTTreeNode r){
if (r != null){
postorder(r.getLeft()); /* To get the root's left
node*/
postorder(r.getRight()); /* To get the root's right node*/
System.out.print(r.getData() + " "); /* Print current node's
data*/
}
}
}/* ENDOF class BSTDriver..*/
/*
* Class : BinarySearchTree
*/
public class BinarySearchTree{
/*Main function declaration..*/
public static void main(String[] args){
Scanner scan = new
Scanner(System.in); /* Declaring the scanner..*/
BSTDriver bst = new
BSTDriver(); /* Declaring the instances..*/
System.out.println("Binary Search
Tree\n");
char ch;
do{
/*To print the
menu in the console..*/
System.out.println("\nBinary Search Tree Operations\n");
System.out.println("1. Add File Data");
System.out.println("2. Delete");
System.out.println("3. Search");
System.out.println("4. Display");
System.out.println("5. Tree Status");
int choice =
scan.nextInt(); /* Read the choice from the
user..*/
/* Switch to
select the required choice..*/
switch
(choice){
case 1:
if
(bst.isEmpty()){ /* Check if
empty..*/
List<String> nameList = readFile();
System.out.println("Name received from list"+
nameList.toString());
for
(String name : nameList) {
bst.insert(name); /* Inserting the
name..*/
}
}
else{
/* Eles part.. */
System.out.println("Record already added into tree. Enter a
different record.");
}
break;
case 2:
System.out.println("Enter
name to delete.");
bst.delete(scan.next()); /* Deleting the
name..*/
break;
case 3:
System.out.println("Enter
name to search");
/* Search the name..*/
System.out.println("Search
result : " + bst.search(scan.next()));
break;
case 4:
System.out.print("\nPost
order : ");
bst.postorder();
/* Func call to perform postorder
traversal..*/
System.out.print("\nPre order
: ");
bst.preorder();
/* Func call to perform
preorder traversal..*/
System.out.print("\nIn order
: ");
bst.inorder();
/* Func call to perform
inorder traversal..*/
break;
case 5:
/* TO check if the tree is
empty..*/
System.out.println("Empty
status = " + bst.isEmpty());
System.exit(1);
/* Exit statement.. */
break;
default:
System.out.println("Wrong
Entry \n ");
break;
/* Default case....*/
}
System.out.println("\nDo you want to continue (Type y or n)
\n");
ch =
scan.next().charAt(0);
} while (ch == 'Y' || ch == 'y');
/* To get user's choice to continue or not.*/
}/* ENDOF main*/
/* To read List from the file..*/
private static List<String> readFile(){
BufferedReader br =
null; /* Creating buffer
reader..*/
FileReader fr = null;
/* Creating file
reader..*/
List<String> fileContent =
new ArrayList<String>();
try{
fr = new
FileReader("bst/bst_name_list"); /* Opening the
file..*/
br = new
BufferedReader(fr);
String
sCurrentLine; /*
Traversing the file..*/
while
((sCurrentLine = br.readLine()) != null){
fileContent.add(sCurrentLine); /*
Adding file contents to list..*/
}
}
catch (FileNotFoundException
e){
e.printStackTrace(); /* Exception
handling..*/
}
catch (IOException e){
e.printStackTrace(); /* Exception handling..*/
}
finally{
/* Exception handling..*/
try{
if (br != null)
br.close();
/* Close buffer reader..*/
if (fr != null)
fr.close();
/* Close file reader..*/
}
catch
(IOException ex){
ex.printStackTrace(); /* Exception
handling..*/
}
}
return fileContent;
/* Return the file content..*/
}/* ENDOF function List*/
}/* ENDOF class BinarySearchTree */
Screenshots:
---------------------------------------------------------------------
Professionally and thoroughly comment on this code. //BinarySearchTree.java package Project.bst; //imports required import java.io.BufferedReader; import java.io.FileNotFoundException;...
Using the following implementation of Tree class Node { public int iData; // data item (key) public double dData; // data item public Node leftChild; // this node's left child public Node rightChild; // this node's right child public void displayNode() // display ourself { System.out.print('{'); System.out.print(iData); System.out.print(", "); System.out.print(dData); System.out.print("} "); } } // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...
please make a pretty JAVA GUI for this code
this is RED BLACK TREE and i Finished code
already
jus need a JAVA GUI for this code ... if poosible make
it pretty to look thanks and
please make own GUI code base on my code
thanks
ex:
(GUI only have to show RBTree)
----------------------------------------
RBTree.java
import java.util.Stack;
public class RBTree{
private Node current;
private Node parent;
private Node grandparent;
private Node header;
private Node...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Need help in the below question. Answer in java Start with the tree.java program (Listing 8.1) and modify it to create a binary tree from a string of letters (like A, B, and so on) entered by the user. Each letter will be displayed in its own node. Construct the tree so that all the nodes that contain letters are leaves. Parent nodes can contain some non-letter symbol like +. Make sure that every parent node has exactly two children....
public class Buildbst { private int data; private Buildbst left; private Buildbst right; //Set the binary search tree public Buildbst(int data) { this.data = data; this.left = null; this.right =null; } public int getData() { return data; } public void setData(int data) { this.data = data; } public Buildbst getLeft() { return left; } public void setLeft(Buildbst left) { this.left = left; } public Buildbst getRight() { return right; } public void setRight(Buildbst right) { this.right = right; } }...
Test Data would be as follows:
Using java language. Build an expression tree. • When you see a number: o you create a new node with the number as the data. o Then you push the new node to the stack. . When you see a binary operator: 0 You create a new Node for the operator. Then you pop and set the right child. Then you pop and set the left child o Then you push the new node...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree { class Node{ int key; Node left,right; public Node(int item) { key = item; left = right = null; } } Node root; public void BinaryTree(){ root = null; } void...
Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method: InOrder(Node theRoot), PreOrder(Node theRoot), PostOrder(Node theRoot), FindMin(), FindMax(), Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...
BST JAVA FILE import java.util.*; public class BST <E extends Comparable <E>> { private TreeNode<E> overallRoot; public BST() { overallRoot = null; } // ************ ADD ************ // public void add(E addThis) { if (overallRoot == null) { overallRoot = new TreeNode<>(addThis); } else { add(overallRoot, addThis); } } private TreeNode<E> add(TreeNode<E> node, E addThis) { if...