Write a program in java which reads a sequence of integer values from input- val
ID: 3714154 • Letter: W
Question
Write a program in java which reads a sequence of integer values from input- values should be separated by space. This program should Build a binary search tree using these values in the order they are entered. Print it in 3 traversal methods: pre-order. In-order, and post-order. Allow the user to Add or Remove a value. Once a new tree is generated, print it in in- order traversal . NOTES . Use recursion to implement add and remove methods, otherwise you will lose points . Duplicates are NOT allowed in this BST. At the very first step after obtaining initial values check for duplicated values. In case of any duplication asks user to enter values again .Your program should use an interactive interface with the format shown in following demo example (the user inputs are underlined) % java Project 1 Please enter values: 26 34 19 12 40 51 29 44 77 60 84 1199 41 36 22 16 Duplication values are not allowed! Please enter values 26 34 19 12 40 51 29 44 77 60 84 11 941 36 22 1615 Pre-order: XXX X In-order XXX...X Post-order: XXX... X Main Menu A: Add a value R Remove a value E : Exit the program What command would like to run? Please enter a value to add: 27 In-order: XXX...X what command would like to run? Please enter a value to add: 11 11 already exists! Duplicated values are not allowed. What command would like to run? R Please enter a value to remove:12 In-order: XXX... X What command would like to run? R Please enter a value to remove: 10 10 doesn't exist What command would like to run? E Exit!Explanation / Answer
hi every one,... here is the code
package chegg;
import java.util.Scanner;
class BSTNode
{
BSTNode left, right;
int data;
public BSTNode()
{
left = null;
right = null;
data = 0;
}
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
public void setLeft(BSTNode n)
{
left = n;
}
public void setRight(BSTNode n)
{
right = n;
}
public BSTNode getLeft()
{
return left;
}
public BSTNode getRight()
{
return right;
}
public void setData(int d)
{
data = d;
}
public int getData()
{
return data;
}
}
class BST
{
private BSTNode root;
public BST()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(int data)
{
root = insert(root, data);
}
private BSTNode insert(BSTNode node, int data)
{
if (node == null)
node = new BSTNode(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
public void delete(int k)
{
if (isEmpty())
System.out.println("Tree Empty");
else if (search(k) == false)
System.out.println("Sorry "+ k +" is not present");
else
{
root = delete(root, k);
System.out.println(k+ " deleted from the tree");
}
}
private BSTNode delete(BSTNode root, int k)
{
BSTNode p, p2, n;
if (root.getData() == k)
{
BSTNode 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 < root.getData())
{
n = delete(root.getLeft(), k);
root.setLeft(n);
}
else
{
n = delete(root.getRight(), k);
root.setRight(n);
}
return root;
}
public boolean search(int val)
{
return search(root, val);
}
private boolean search(BSTNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval)
r = r.getLeft();
else if (val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public void inorder()
{
inorder(root);
}
private void inorder(BSTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
public void preorder()
{
preorder(root);
}
private void preorder(BSTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
public void postorder()
{
postorder(root);
}
private void postorder(BSTNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
public class Chegg
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
BST bst = new BST();
System.out.println("Binary Search Tree Test ");
char ch;
do
{
System.out.println(" Binary Search Tree Operations ");
System.out.println("1. insert ");
System.out.println("2. delete");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to insert");
bst.insert( scan.nextInt() );
break;
case 2 :
System.out.println("Enter integer element to delete");
bst.delete( scan.nextInt() );
break;
default :
System.out.println("Wrong Entry ");
break;
}
System.out.print(" Post order : ");
bst.postorder();
System.out.print(" Pre order : ");
bst.preorder();
System.out.print(" In order : ");
bst.inorder();
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
i was tried as best as i can.... here is the code for entering no but not in bunch of numbers....
you can only enter the number one bye one. and then select the operation either insert or delete.... it also check the duplication. so that if u enter duplicate number it shows you the message that you enter the duplicate number.
and after every number it shows u three order that is pre, in, and post order.
i hope it will help you. ythank you
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.