You are on page 1of 6

Name-Mrunmai Patil

Roll No-222051
Prn No-22010930
Batch B2

Code-
//Implement a binary search tree and perform the following operations: a. Insert, Delete, Display

import java.util.*;

class node{
node left;
node right;
int data;

node(int val){
data = val;
left = right = null;
}
}

class BST{
Scanner scan = new Scanner(System.in);
node root;

BST(){
System.out.print("Enter value to be entered in the root: ");
int value = scan.nextInt();
root = new node(value);
}

void insertion(node root, int data){


if (data > root.data){
if (root.right == null){
root.right = new node(data);
return;
}
else{
insertion(root.right, data);
}

}
else{
if (root.left == null){
root.left = new node(data);
}
else{
insertion(root.left, data);
}
}
}

void insert_data(node root){


System.out.println("Do you want to enter a new node? (1 for yes, 2 for no)");
int choice = scan.nextInt();

while(choice == 1){
System.out.print("Enter data: ");
int data = scan.nextInt();
insertion(root, data);

System.out.println("Do you want to enter a new node? (1 for yes, 2 for no)");
choice = scan.nextInt();
}
}

void delete_node(int key){


root = this.delete(root, key);
}

node delete(node rut, int value){


if (value == rut.data){
node templeft = rut.left;
node tempright = rut.right;
node temp = rut;

if(temp.right == null){
rut = temp.left;
return rut;
}
else{
temp = temp.right;
}

if(temp.left == null){
rut = temp;
rut.left = templeft;
return rut;
}
else{
while(temp.left.left != null){
temp = temp.left;
}

rut = temp.left;
if(temp.left.right != null){
temp.left = temp.left.right;
}
else{
temp.left = null;
}

rut.left = templeft;
rut.right = tempright;
return rut;
}

}
else if (value < rut.data){
rut.left = delete(rut.left, value);
return rut;
}
else {
rut.right = delete(rut.right, value);
return rut;
}
}

//using recursion
void preorder(node rut){
System.out.print(rut.data + " ");
if(rut.left != null){
preorder(rut.left);
}

if(rut.right != null){
preorder(rut.right);
}
}
//using recusrsion
void inorder(node rut){
if(rut == null){
return;
}

inorder(rut.left);
System.out.print(rut.data + " ");
inorder(rut.right);
}

//using rucursion
void postorder(node rut){
if(rut==null){
return;
}
postorder(rut.left);
postorder(rut.right);
System.out.print(rut.data + " ");
}

void search(node rut, int value){


if (rut == null){
System.out.println("Value is not present in tree.");
return;
}

if (value == rut.data){
System.out.println("Value is present in tree.");
}
else if (value < rut.data){
search(rut.left, value);
}
else{
search(rut.right, value);
}

// Here for calculating height I have used the convention that height is the number of nodes in
the longest chain from the root to the leaf node
int height(node rut){

if(rut == null){
return 0;
}
else{
int lheight = height(rut.left);
int rheight = height(rut.right);

if(lheight > rheight){


return lheight+1;
}
else{
return rheight+1;
}
}
}
}

public class Main{


public static void main(String[] args){

Scanner scan = new Scanner(System.in);

BST b1 = new BST();

int temp;
int choice = 0;

while(choice<6){
System.out.println("\n-------------Menu------------- \n 1. Insert \n 2. Delete \n 3. Display \n
4. Search \n 5. Height \n 6. Exit.");
choice = scan.nextInt();

switch (choice){
case 1:
b1.insert_data(b1.root);
break;

case 2:
System.out.print("Enter value which is to be deleted: ");
temp = scan.nextInt();
b1.delete_node(temp);
break;

case 3:
System.out.print("Enter 1 for preorder, 2 for inorder, 3 for postorder traversal: ");
temp = scan.nextInt();

if(temp == 1){
b1.preorder(b1.root);
System.out.println();
}
else if(temp == 2){
b1.inorder(b1.root);
System.out.println();
}
else if(temp == 3){
b1.postorder(b1.root);
System.out.println();
}
break;

case 4:
System.out.print("Enter data which is to be searched: ");
temp = scan.nextInt();
b1.search(b1.root, temp);
break;

case 5:
System.out.print("Height of the tree is: " + b1.height(b1.root));
System.out.println();

case 6:
break;

default:
System.out.println("Entered choice is invalid. ");

}
}
scan.close();
}
}

You might also like