-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.java
152 lines (145 loc) · 4.69 KB
/
BinarySearchTree.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import java.util.Scanner;
public class BinarySearchTree {
class Node
{
int Value;
Node LeftChild,RightChild;
public Node(int Data){
Value=Data;
LeftChild=RightChild=null;
}
}
Node root;
BinarySearchTree(){
root=null;
}
void InsertNode(int Value){
root=InsertInBST(root,Value);
}
Node InsertInBST(Node root,int Value){
if(root==null){
root=new Node(Value);
return root;
}
else if(Value<root.Value){
root.LeftChild=InsertInBST(root.LeftChild, Value);
return root;
}
else{
root.RightChild=InsertInBST(root.RightChild, Value);
return root;
}
}
void inordertraversal(){
InOrderTraversal(root);
}
void InOrderTraversal(Node root){
if(root!=null){
InOrderTraversal(root.LeftChild);
System.out.println(root.Value+"");
InOrderTraversal(root.RightChild);
}
}
boolean searchinbstInvoker(int Value_to_be_searched){
SearchInBST(root, Value_to_be_searched);
if (root!= null){
return true;
}
else{
return false;
}
}
Node SearchInBST(Node root,int Value_to_be_searched){
if(root==null||root.Value==Value_to_be_searched){
return root;
}
else if(root.Value>Value_to_be_searched){
return SearchInBST(root.LeftChild, Value_to_be_searched);
}
else{
return SearchInBST(root.RightChild, Value_to_be_searched);
}
}
int getMinValue(Node root){
int minvalue=root.Value;
while (root.LeftChild != null) {
minvalue = root.LeftChild;
root = root.LeftChild;
}
return minvalue;
}
void deleteinvoker(int Value){
root=DeleteNode(root, Value);
}
Node DeleteNode(Node root,int Value){
if(root==null){
return root;
}
// moving along the Left part of the tree
if(Value<root.Value){
root.LeftChild=DeleteNode(root.LeftChild, Value);
}
// moving along the Right part of the tree
else if(Value>root.Value){
root.RightChild=DeleteNode(root.RightChild, Value);
}
else{
// handling the cases where only one child of Root node is present
if(root.LeftChild==null){
return root.RightChild;
}
else if(root.RightChild==null){
return root.LeftChild;
}
// getting the inorder successor and deleting it from the tree when the Node has 2 children
root.Value=getMinValue(root.RightChild);
//deleting that value from the tree
root.RightChild=DeleteNode(root.RightChild, root.Value);
}
return root;
}
public static void main(String[] args) {
int N,option,choice;
boolean var;
Scanner sc=new Scanner(System.in);
BinarySearchTree bst=new BinarySearchTree();
System.out.println("WELCOME TO BINARY SERACH TREE PROGRAM");
do {
System.out.println("1.Insert Node in BST 2.Perform Inorder Traversal 3.Search Node 4.Delete Node");
System.out.println("Enter the choice of operation to be performed");
option=sc.nextInt();
switch (option) {
case 1:
System.out.println("Enter the Number you want to Insert");
N=sc.nextInt();
bst.InsertNode(N);
break;
case 2:
System.out.println("The Inorder Traversal of the Tree is");
bst.inordertraversal();
break;
case 3:
System.out.println("Enter the Number you want to Search");
N=sc.nextInt();
var=bst.searchinbstInvoker(N);
if(var){
System.out.println("The Node is present in BST ");
}
else{
System.out.println("Sorry The Node is Not Present");
}
break;
case 4:
System.out.println("Enter the Number you want to delete");
N=sc.nextInt();
bst.deleteinvoker(N);
break;
default:
System.out.println("Sorry Please Enter the Right Choice!!!");
break;
}
System.out.println("Do you want to continue with the Program if yes than press 1");
choice=sc.nextInt();
} while (choice==1);
}
}