forked from vinay-kumar99/Hactoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsorted_array_to_bst.cpp
More file actions
329 lines (284 loc) · 7.89 KB
/
Copy pathsorted_array_to_bst.cpp
File metadata and controls
329 lines (284 loc) · 7.89 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include<bits/stdc++.h>
using namespace std;
/* A binary tree node has data,
pointer to left child and
a pointer to right child */
class node
{
public:
int value;
node* left;
node* right;
/* Constructor that allocates
a new node with the given data
and NULL left and right pointers. */
node()
{
value=0;
left=NULL;
right=NULL;
}
node(int val)
{
value = val;
left = NULL;
right = NULL;
}
};
class BST
{ public:
node *root;
BST()
{
root=NULL;
}
bool isempty()
{
if(root==NULL)
return true;
else
return false;
}
void insert(node *new_node)
{
if(root==NULL)
{
root=new_node;
cout<<"value inserted at the root";
}
else
{
node *temp=root;
while(temp!=NULL)
{
if(new_node->value < temp->value && temp->left ==NULL)
{
temp->left=new_node;
cout<<"value inserted at left";
break;
}
else if (new_node->value <temp->value)
{
temp=temp->left;
}
else if(new_node->value > temp->value && temp->right ==NULL)
{
temp->right=new_node;
cout<<"value inserted at right";
break;
}
else if (new_node->value >temp->value)
{
temp=temp->right;
}
else if(new_node->value==temp->value)
{ cout<<"no duplicates!!!";
return ;
}
}
}
}
void printinorder(node *r)
{
if(r==NULL)
return;
printinorder(r->left);
cout<<r->value<<" ";
printinorder(r->right);
}
void printpreorder(node *r)
{
if(r==NULL)
return ;
cout<<r->value<<" ";
printpreorder(r->left);
printpreorder(r->right);
}
void printpostorder(node *r)
{
if(r==NULL)
return;
printpostorder(r->left);
printpostorder(r->right);
cout<<r->value<<" ";
}
void search(node *r,int ele)
{
if(r==NULL)
return;
if(r->value==ele)
{
cout<<"element exixts\n";
return;
}
else if(ele<r->value && r->left!=NULL)
search(r->left,ele);
else if(ele>r->value && r->right!=NULL)
search(r->right,ele);
else
cout<<"element does not exist\n";
}
void deletion(node* r)
{
// Base case: empty tree
if (r == nullptr)
return;
queue<node*> queue;
queue.push(r);
node *temp;
// loop till queue is empty
while (!queue.empty())
{
// delete each node in the queue one by one after pushing their
// non-empty left and right child to the queue
temp = queue.front();
queue.pop();
if (temp->left)
queue.push(temp->left);
if (temp->right)
queue.push(temp->right);
// Important - delete front node ONLY after enqueuing its children
delete temp;
}
r = nullptr;
}
/* Returns true if the given
tree is a BST and its values
are >= min and <= max. */
int isBSTUtil(node* node, int min, int max)
{
/* an empty tree is BST */
if (node==NULL)
return 1;
/* false if this node violates
the min/max constraint */
if (node->value < min || node->value > max)
return 0;
/* otherwise check the subtrees recursively,
tightening the min or max constraint */
return
isBSTUtil(node->left, min, node->value-1) && // Allow only distinct values
isBSTUtil(node->right, node->value+1, max); // Allow only distinct values
}
/* Returns true if the given
tree is a binary search tree
(efficient version). */
int isBST(node* node)
{
return(isBSTUtil(node, INT_MIN, INT_MAX));
}
node *Array_toBST(int arr[],int start,int end)
{
/* Base Case */
if (start > end)
return NULL;
/* Get the middle element and make it root */
int mid = (start + end)/2;
node *temp = newNode(arr[mid]);
/* Recursively construct the left subtree
and make it left child of root */
temp->left = Array_toBST(arr, start,
mid - 1);
/* Recursively construct the right subtree
and make it right child of root */
temp->right = Array_toBST(arr, mid + 1, end);
return temp;
}
node *newNode(int data)
{
node *new_node = new node();
new_node->value = data;
new_node->left = NULL;
new_node->right = NULL;
return new_node;
}
};
/* Driver code*/
int main()
{
BST obj;
int val,choice;
do
{
/* code */
cout<<"enter 1 for insertion"<<endl;
cout<<"enter 2 to print in the bst"<<endl;
cout<<"enter 3 to print post the bst"<<endl;
cout<<"enter 4 to print pre the bst"<<endl;
cout<<"enter 5 to check whether a bst"<<endl;
cout<<"enter 6 to search for the element\n";
cout<<"enter 7 for deletion of the desired node\n";
cout<<"enter 8 for making an array to bst\n";
cout<<"enter 0 to exit the program"<<endl;
cin>>choice;
//why didnt we use stack initialization like Node n1 just to make this new_node to be globaaly available
switch(choice)
{
case 1:
{
cout<<"insert"<<endl;
cout<<"enter the value to be inserted"<<endl;
cin>>val;
node *new_node=new node();//heap type of RAM allocation
new_node->value=val;
obj.insert(new_node);
cout<<endl;
break;
}
case 2:
{
cout<<"printinorder"<<endl;
obj.printinorder(obj.root);
cout<<endl;
break;
}
case 3:
{
cout<<"printpostorder"<<endl;
obj.printpostorder(obj.root);
cout<<endl;
break;
}
case 4:
{cout<<"printpreoreder"<<endl;
obj.printpreorder(obj.root);
cout<<endl;
break;
}
case 5:
{
if(obj.isBST(obj.root))
cout<<"yes a bst\n";
else
{
cout<<"not a bst\n";
}
break;
}
case 6:
{ int element;
cin>>element;
obj.search(obj.root,element);
}
case 7://not yet completed
{
// int element;
// cin>>element;
obj.deletion(obj.root);
}
case 8:
{ int len;
cin>>len;
int arr[len];
for (int i=0;i<len;i++)
{
cin>>arr[i];
}
int n=sizeof(arr)/sizeof(arr[0]);
sort(arr,arr+n);
obj.Array_toBST(arr,0,len);
}
}
} while (choice !=0);
return 0;
}