-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleteBinarySearchTree.cpp
More file actions
550 lines (509 loc) · 10.8 KB
/
completeBinarySearchTree.cpp
File metadata and controls
550 lines (509 loc) · 10.8 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#include<iostream>
#include<queue>
#include<vector>
using namespace std;
//class for BST same as Binary tree
class binarySearchTreeNode
{
public:
int data;
binarySearchTreeNode* left;
binarySearchTreeNode* right;
binarySearchTreeNode(int d)
{
data=d;
left=NULL;
right=NULL;
}
~binarySearchTreeNode()
{
delete left;
delete right;
}
};
//Take Input Level wise
binarySearchTreeNode* takeInput()
{
int rootdata;
cout<<"enter root data "<<endl;
cin>>rootdata;
if(rootdata==-1)
{
return NULL;
}
binarySearchTreeNode* root=new binarySearchTreeNode(rootdata);
queue<binarySearchTreeNode*>q;
q.push(root);
while(q.size()!=0)
{
binarySearchTreeNode* front=q.front();
q.pop();
cout<<"enter left child value of "<<front->data<<endl;
int lchild;
cin>>lchild;
if(lchild!=-1)
{
binarySearchTreeNode* leftChild=new binarySearchTreeNode(lchild);
q.push(leftChild);
front->left=leftChild;
}
int rchild;
cout<<"enter right child value of "<<front->data<<endl;
cin>>rchild;
if(rchild!=-1)
{
binarySearchTreeNode* rightChild=new binarySearchTreeNode(rchild);
q.push(rightChild);
front->right=rightChild;
}
}
return root;
}
//Print Level Wise
void print(binarySearchTreeNode* root)
{
if(root==NULL)
{
return ;
}
queue<binarySearchTreeNode*> q;
q.push(root);
while(q.size()!=0)
{
binarySearchTreeNode* front=q.front();
q.pop();
cout<<front->data<<": ";
if(front->left!=NULL)
{
cout<<"L "<<front->left->data<<",";
q.push(front->left);
}
if(front->right!=NULL)
{
cout<<"R "<<front->right->data;
q.push(front->right);
}
cout<<endl;
}
}
//Search specific Key in BST
int search(binarySearchTreeNode* root,int key)
{
int ans=-1;
if(root==NULL)
{
cout<<"Not present "<<endl;
return -1;
}
if(root->data==key)
{
cout<<"key is present "<<endl;
return root->data;
}
if(root->data<key)
{
ans=search(root->right,key);
}
if(root->data>key)
{
ans=search(root->left,key);
}
return ans;
}
//Function to print elements in given range
int elementsInRange(binarySearchTreeNode* root,int min,int max)
{
static int ans=0;
// int ans=0;
if(root==NULL)
{
return -1 ;
}
if(root->data>=min && root->data<=max)
{
ans++;
cout<<root->data<<" ";
}
if(root->data>min)
{
int lhs=elementsInRange(root->left,min,max);
}
if(root->data<max)
{
int rhs=elementsInRange(root->right,min,max);
}
return ans;
}
//Function to find maximum node value in BST
int maximum(binarySearchTreeNode *root)
{
if(root==NULL)
{
return INT_MIN;
}
int lmax=maximum(root->left);
int rmax=maximum(root->right);
int ans=max(root->data,max(lmax,rmax));
return ans;
}
//Function to find minimum node value in BST
int minimum(binarySearchTreeNode* root)
{
if(root==NULL)
{
return INT_MAX;
}
int lmin=minimum(root->left);
int rmin=minimum(root->right);
int ans=min(root->data,min(lmin,rmin));
}
//Function to check whether the given tree is BST or Not 1
bool checkIsBST1(binarySearchTreeNode* root)
{
if(root==NULL)
{
return true;
}
int lmax=maximum(root->left);
int rmin=minimum(root->right);
bool lBST=checkIsBST1(root->left);
bool rBST=checkIsBST1(root->right);
bool ans=(root->data>lmax) && (root->data<rmin) && lBST && rBST;
return ans;
}
//Function to check whether the given tree is BST or Not 2
pair<pair<int,int>,bool> checkIsBST2(binarySearchTreeNode* root)
{
if(root==NULL)
{
pair<pair<int,int>,bool> ans;
ans.first.first=INT_MAX;
ans.first.second=INT_MIN;
ans.second=true;
return ans;
}
pair<pair<int,int>,bool> ans=checkIsBST2(root->left);
pair<pair<int,int>,bool> ans1=checkIsBST2(root->right);
int mini=min(root->data,min(ans.first.first,ans1.first.first));
int maxi=max(root->data,max(ans.first.second,ans1.first.second));
bool answer=(root->data >ans.first.second)&& (root->data<ans1.first.first) && (ans.second) && (ans1.second);
pair<pair<int,int>,bool> totalAns;
totalAns.first.first=mini;
totalAns.first.second=maxi;
totalAns.second=answer;
return totalAns;
}
//Function to check whether the given tree is BST or Not 3
bool checkIsBST3(binarySearchTreeNode* root,int min=INT_MIN,int max=INT_MAX)
{
if(root==NULL)
{
return true;
}
if(root->data<min || root->data>max)
{
return false;
}
bool lBst=checkIsBST3(root->left,min,root->data-1);
bool rBst=checkIsBST3(root->right,root->data,max);
bool ans=lBst&&rBst;
return ans;
}
//Function to construct BST from sorted array
binarySearchTreeNode* constructBST(int*ar,int si,int ei)
{
if(si>ei)
{
return NULL;
}
int mid=(si+ei)/2;
binarySearchTreeNode* root=new binarySearchTreeNode(ar[mid]);
binarySearchTreeNode* lchild=constructBST(ar,si,mid-1);
binarySearchTreeNode* rchild=constructBST(ar,mid+1,ei);
root->left=lchild;
root->right=rchild;
return root;
}
//Node class for linked list
class node
{
public:
int data;
node* next;
node(int d)
{
data=d;
next=NULL;
}
};
// function to convert BST to Sorted Linked List
node* BST2LL(binarySearchTreeNode* root)
{
if(root==NULL)
{
return NULL;
}
node* leftSubTree=BST2LL(root->left);
node* rightSubTree=BST2LL(root->right);
node* root1=new node(root->data);
root1->next=rightSubTree;
if(leftSubTree==NULL)
{
return root1;
}
node*temp=leftSubTree;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=root1;
return leftSubTree;
}
//Function to print Linked list
void printLL(node* head)
{
node* temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
// Function to print path from root to given node
vector<int>* getRootToNode(binarySearchTreeNode* root,int data)
{
if(root==NULL)
{
return NULL;
}
if(root->data==data)
{
vector<int>*op=new vector<int>();
op->push_back(root->data);
return op;
}
vector<int>* leftOp=getRootToNode(root->left,data);
if(leftOp!=NULL)
{
leftOp->push_back(root->data);
return leftOp;
}
vector<int>* rightOp=getRootToNode(root->right,data);
if(rightOp!=NULL)
{
rightOp->push_back(root->data);
return rightOp;
}
else
{
return NULL;
}
}
// class bst for specific order insertion,deletion and searching
class BST{
public:
binarySearchTreeNode* root;
BST()
{
root=NULL;
}
~BST()
{
delete root;
}
private:
bool hasData(int data,binarySearchTreeNode*root)
{
bool ans;
if(root==NULL)
{
return false;
}
if(root->data==data)
{
return true;
}
if(root->data>data)
{
ans=hasData(data,root->left);
}
if(root->data<data)
{
ans=hasData(data,root->right);
}
return ans;
}
binarySearchTreeNode* insert(int data,binarySearchTreeNode* node)
{
if(node==NULL)
{
binarySearchTreeNode* newnode=new binarySearchTreeNode(data);
return newnode;
}
if(node->data>data)
{
binarySearchTreeNode* insertLeft=insert(data,node->left);
node->left=insertLeft;
}
else
{
binarySearchTreeNode* insertRight=insert(data,node->right);
node->right=insertRight;
}
return node;
}
binarySearchTreeNode* deleteNode(int data,binarySearchTreeNode* node)
{
if(node==NULL)
{
return NULL;
}
if(node->data>data)
{
binarySearchTreeNode* delLeft=deleteNode(data,node->left);
node->left=delLeft;
}
else if(node->data<data)
{
binarySearchTreeNode* delRight=deleteNode(data,node->right);
node->right=delRight;
}
else
{
if(node->left==NULL && node->right==NULL)
{
delete node;
return NULL;
}
if(node->left==NULL)
{
binarySearchTreeNode* temp=node->right;
node->right=NULL;
delete node;
return temp;
}
if(node->right==NULL)
{
binarySearchTreeNode* temp=node->right;
node->right=NULL;
delete node;
return temp;
}
else
{
binarySearchTreeNode* minNode=node->right;
while(minNode->left!=NULL)
{
minNode=minNode->left;
}
int min=minNode->data;
node->data=min;
node->right=deleteNode(data,node->right);
}
}
return node;
}
void printTree(binarySearchTreeNode* root)
{
if(root==NULL)
{
return ;
}
queue<binarySearchTreeNode*> q;
q.push(root);
while(q.size()!=0)
{
binarySearchTreeNode* front=q.front();
q.pop();
cout<<front->data<<": ";
if(front->left!=NULL)
{
cout<<"L "<<front->left->data<<",";
q.push(front->left);
}
if(front->right!=NULL)
{
cout<<"R "<<front->right->data;
q.push(front->right);
}
cout<<endl;
}
}
public:
bool hasData(int data)
{
return hasData(data,root);
}
void insert(int data)
{
root=insert(data,root);
}
void deleteNode(int data)
{
root=deleteNode(data,root);
}
void printTree()
{
printTree(root);
}
};
//input: 50 30 70 10 40 60 80 5 20 -1 45 -1 65 75 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
int main()
{
// binarySearchTreeNode* root=takeInput();
// print(root);
// cout<<endl;
// cout<<"search : "<<search(root,65)<<endl;
// int min;
// cout<<"enter min value "<<endl;
// cin>>min;
// int max;
// cout<<"enter max value "<<endl;
// cin>>max;
// cout<<endl;
// cout<<endl<<"total elements in this range are : "<<elementsInRange(root,min,max)<<endl;
// cout<<"min value "<<minimum(root)<<endl;
// cout<<"maximum value "<<maximum(root)<<endl;
// cout<<checkIsBST1(root)<<endl;
// pair<pair<int,int>,bool> ans=checkIsBST2(root);
// cout<<ans.second<<endl;
// bool ans1=checkIsBST3(root);
// cout<<ans1<<endl;
// int ar[]={1,2,3,4,5,6,7,8};
// int si=0;
// int size=sizeof(ar)/sizeof(ar[0]);
// int ei=size-1;
// binarySearchTreeNode* root=constructBST(ar,si,ei);
// print(root);
// node* head=BST2LL(root);
// printLL(head);
// cout<<endl<<endl;
// vector<int>* op=getRootToNode(root,65);
// if(op==NULL)
// {
// cout<<"No element present "<<endl;
// }
// else
// {
// for(int i=0;i<op->size();i++)
// {
// cout<<op->at(i)<<endl;
// }
// }
BST b;
b.insert(10);
b.insert(20);
b.insert(15);
b.insert(5);
b.insert(7);
b.insert(3);
b.insert(17);
b.printTree();
b.deleteNode(10);
cout<<endl<<endl;
b.printTree();
cout<<endl<<endl;
cout<<b.hasData(17);
// delete op;
// delete root;
}