-
Notifications
You must be signed in to change notification settings - Fork 284
/
Implementation_BST_SpokenEnglishClass.cpp
249 lines (171 loc) · 4.01 KB
/
Implementation_BST_SpokenEnglishClass.cpp
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include <bits/stdc++.h>
using namespace std;
class Node{
public:
int data;
Node *left;
Node *right;
public:
Node(int x){
data = x;
left = NULL;
right = NULL;
}
};
Node *insert_BST(Node *tree, int val)
{
if(tree == NULL){
cout<<val <<" is inserted into BST successfully"<<endl;
return (new Node(val));
}
if(tree->data >= val)
tree->left = insert_BST(tree->left, val);
else
tree->right = insert_BST(tree->right, val);
return tree;
}
bool searchInBST(Node *root , int k) {
// Write your code here
if(root==NULL){
return false;
}
if(root->data==k){
return true;
}
else if(root->data>k){
return searchInBST(root->left ,k);
}
else if(root->data<k){
return searchInBST(root->right ,k);
}
}
Node* delete_BST(Node *tree, int val)
{
if(tree == NULL){
cout<<"value is not present in BST"<<endl;
return tree;
}
if(tree->data > val)
tree->left = delete_BST(tree->left, val);
else if(tree->data < val)
tree->right = delete_BST(tree->right, val);
else{
if(tree->left == NULL){
Node *temp = tree->right;
free(tree);
tree= temp;
}
else if(tree->right == NULL){
Node *temp = tree->left;
tree = temp;
free(temp);
}
else{
Node *temp = tree->left;
while(temp->right->right!=NULL){
temp = temp->right;
}
tree->data = temp->right->data;
Node *temp2 = temp->right->left;
free(temp->right);
temp->right = temp2;
}
}
return tree;
}
void inorder(Node *tree)
{
if (tree != NULL)
{
inorder(tree->left);
cout<<tree->data<<" ";
inorder(tree->right);
}
}
void printLevelATNewLine(Node *root) {
if (root == NULL) {
return;
}
queue<Node *> q;
q.push(root);
q.push(NULL);
while (!q.empty()) {
Node *first = q.front();
q.pop();
if (first == NULL) {
if (q.empty()) {
break;
}
cout << endl;
q.push(NULL);
continue;
}
cout << first->data << " ";
if (first->left != NULL) {
q.push(first->left);
}
if (first->right != NULL) {
q.push(first->right);
}
}
}
int main(){
cout<<"BRITISH COUNCIL CLASS"<<endl;
// cout<<"Root of the BST i.e. the first child ";
// int x;
// cin>>x;
// cout<<x<<endl;
Node *root = NULL;
//takeInput(root);
int z;
while(z!=6){
cout<<"*****************"<<endl;
cout<<"Enter 1: To Insert student"<<endl;
cout<<"Enter 2: To Search for any student"<<endl;
cout<<"Enter 3: if any student exits"<<endl;
cout<<"Enter 4: To print the tree in order"<<endl;
cout<<"Enter 5: To print level wise"<<endl;
cout<<"Enter 6: To Exit"<<endl;
cout<<"*****************"<<endl;
cin >> z;
switch(z){
case 1:
cout<<"Enter the student roll no. as they enter the class "<<endl;
int id;
cin>>id;
root=insert_BST(root, id);
inorder(root);
break;
case 2:
int k;
cout<<"Enter the student Roll No. for search "<<endl;
cin>>k;
if(searchInBST(root,k)==false){
cout<<"Student is NOT PRESENT in the class"<<endl;
}
else{
cout<<"Student is PRESENT in the class"<<endl;
}
break;
case 3:
int m;
cout<<"Enter the student's Roll No. who exited "<<endl;
cin>>m;
delete_BST(root, m);
//printLevelATNewLine(root);
break;
case 4:
inorder(root);
break;
case 5:
printLevelATNewLine(root);
break;
case 6:
z=6;
break;
}
}
// cout << ((searchInBST(root, k)) ? "true" : "false");
cout<<"*******EXIT********"<<endl;
delete root;
}