-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathBST.java
More file actions
204 lines (181 loc) · 5.99 KB
/
BST.java
File metadata and controls
204 lines (181 loc) · 5.99 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//Importing necessary Packages
import java.util.Scanner;
import java.util.InputMismatchException;
class Node {
int data;
Node left;
Node right;
// constructor for class Node
Node(int data) {
this.data = data;
left = null;
right = null;
}
}
class BST {
Node root;
// To initialize a Empty tree
BST() {
root = null;
}
// function to insert data in Tree
void insert(int data) {
root = insertRec(root, data);
}
Node insertRec(Node root, int data) {
if (root == null) {
root = new Node(data);
return root;
}
if (data < root.data) {
root.left = insertRec(root.left, data);
} else if (data > root.data) {
root.right = insertRec(root.right, data);
}
return root;
}
// function to delete the data from tree
void delete(int data) {
root = deleteRec(root, data);
}
Node deleteRec(Node root, int data) {
if (root == null) {
return root;
}
if (data < root.data) {
root.left = deleteRec(root.left, data);
} else if (data > root.data) {
root.right = deleteRec(root.right, data);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
}
root.data = minValue(root.right);
root.right = deleteRec(root.right, root.data);
}
return root;
}
// function to find the minimum value in a tree
int minValue(Node root) {
int minVal = root.data;
while (root.left != null) {
minVal = root.left.data;
root = root.left;
}
return minVal;
}
void inorder() {
inorderRec(root);
}
// function to perform inorder Traversal in tree
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.data + " ");
inorderRec(root.right);
}
}
// function to perform Preorder Traversal in tree
void preorder() {
preorderRec(root);
}
void preorderRec(Node root) {
if (root != null) {
System.out.print(root.data + " ");
preorderRec(root.left);
preorderRec(root.right);
}
}
// function to perform postorder Traversal in tree
void postorder() {
postorderRec(root);
}
void postorderRec(Node root) {
if (root != null) {
postorderRec(root.left);
postorderRec(root.right);
System.out.print(root.data + " ");
}
}
// function to search Node in tree
boolean search(int data) {
return searchRec(root, data);
}
boolean searchRec(Node root, int data) {
if (root == null) {
return false;
}
if (root.data == data) {
return true;
} else if (data < root.data) {
return searchRec(root.left, data);
} else {
return searchRec(root.right, data);
}
}
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
BST bst = new BST();
int choice;
do {
System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. Inorder Traversal");
System.out.println("4. Preorder Traversal");
System.out.println("5. Postorder Traversal");
System.out.println("6. Search");
System.out.println("7. Exit");
System.out.println("Enter your choice:");
choice = input.nextInt();
switch (choice) {
case 1:
System.out.println("Enter value to insert:");
int value = input.nextInt();
bst.insert(value);
break;
case 2:
System.out.println("Enter value to delete:");
int delVal = input.nextInt();
bst.delete(delVal);
break;
case 3:
System.out.println("Inorder Traversal:");
bst.inorder();
System.out.println();
break;
case 4:
System.out.println("Preorder Traversal:");
bst.preorder();
System.out.println();
break;
case 5:
System.out.println("Postorder Traversal:");
bst.postorder();
System.out.println();
break;
case 6:
System.out.println("Enter value to search:");
int searchVal = input.nextInt();
boolean found = bst.search(searchVal);
if (found) {
System.out.println("found in the tree.");
} else {
System.out.println("Not found");
}
break;
case 7:
System.out.println("Exit done");
break;
default:
System.out.println("Invalid choice");
}
} while (choice != 7);
} catch (InputMismatchException exception) {
System.out.println(exception);
} catch (Exception exception) {
System.out.println(exception);
}
}
}