forked from ZapeeoSheikh/Hacktoberfest-22
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinarySearchTree.cpp
293 lines (246 loc) · 6.18 KB
/
BinarySearchTree.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
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
#include <iostream>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct Node{
int data;
Node* left;
Node* right;
};
class BST{
private:
Node* root;
public:
BST();
void insert(int n);
void print();
void del(int n);
void find(int n, Node* &p,Node* & parent);
void find_rec(int n, Node* &p,Node* & parent);
private:
void insert_rec(int n, Node* &root);
void print_tree(Node * root);
void getLargestNode(Node * &r, Node * &pred_r);
Node *del_OneChildNode(Node *p,Node *parent);
Node *del_Leaf(Node *p,Node *parent);
Node *del_TwoChildNode(Node *p,Node *parent);
};
//////////////////////////////////////////////////
// constructor
////////////////////////////////////////
BST::BST()
{
root=NULL;
}
///////////////////////////////////
// the public recurisive function that is used only so that root is not accessible outside the class
//////////////////////////////
void BST::insert(int n)
{
insert_rec(n,root);
}
//////////////////////////////////////////////
// the recurisive function that does the actual insertion
//////////////////////////////////////
void BST::insert_rec(int n, Node* &root)
{
if(root==NULL)
{
root=new Node;
root->data=n;
root->left=NULL;
root->right=NULL;
return;
}
else if (root->data==n)
{
cout<<"\n Data already in BST ..not inserting node with data "<<n;
return;
}
else if(n<root->data)
insert_rec(n,root->left);
else
insert_rec(n,root->right);
}
void BST::print()
{
cout<<"\n Printing Tree \n";
print_tree(root);
}
///////////////////////////////////
/////////////////////////////////////
void BST::print_tree(Node * root)
{
if(root==NULL)
return;
else
{
print_tree(root->left);
cout<<" "<<root->data;
print_tree(root->right);
}
}
//////////////////////////////////////////////////
//The below function searches for a node with data n a node and its parent.
/////////////////////////////////////////////////////
void BST::find(int n, Node * &p, Node* &parent)
{
p=root;
parent=NULL;
find_rec( n, p, parent);
if(p==NULL)
cout<<"Node not found";
else if(parent==NULL)
cout<<"\n found node "<<p->data<<" at root";
else
cout<<"\n p->data "<<p->data<<" parent->data "<<parent->data;
}
/////////////////////////////////////////
// actual finding work is done here
// when the function returns to the calling function (main)
// p should poiint to the node and parent to its parent
// p will be NULL if the node does not exist
// parent is NULL if the required node is the root
///////////////////////////////////////////
void BST::find_rec(int n, Node * &p, Node* &parent)
{
if(p==NULL)
return;
if(p->data==n)
return;
else if(n<p->data)
{
parent=p;
p=p->left;
find_rec(n,p,parent);
}
else{//required node on the right
parent=p;
p=p->right;
find_rec(n,p,parent);
}
}
///////////////////////////////////////
// function to delete a node
// input n
////////////////////////////////////////
void BST::del(int n)
{
//search the pointer to the node and the parent
Node *p;
Node* parent;
find(n,p,parent);
if(p==NULL){//if the node doesnt exist
cout<<"\n Node does not exist in the BST";
return;
}
else if(p->left==NULL &&p->right==NULL){//p is a leaf
cout<<"\n Leaf Node";
p=del_Leaf(p,parent);
}
else if(p->left==NULL ||p->right==NULL){//p has one child . If both cases are true then the above if is executed
cout<<"\n One child";
p=del_OneChildNode(p, parent);
}
else{//p has 2 children
cout<<"\n two children";
p=del_TwoChildNode(p,parent);
}
delete p; // now the node is properly removed from the tree so we just free memory
}
////////////////////////////////////////////////
///////////////////////////////////////////
Node * BST::del_OneChildNode(Node *p,Node *parent)
{
//identify the single child of p
Node *temp;
if(p->left!=NULL)
temp=p->left;
else
temp=p->right;
cout<<"\n temp->data "<<temp->data;
if(parent==NULL)// p is the root
{
root=temp;
}
else if(parent->left==p)
parent->left=temp;
else
parent->right=temp;
return p;
}
///////////////////////////////////////////////////////////////
///////////////////////////////////////////////
Node * BST::del_TwoChildNode(Node *p,Node *parent)
{
Node *r=p->left;// r is the address of the node we will replace p with
Node* pred_r; // the predecessor or parent of r
getLargestNode(r,pred_r);
cout<<"\n the largest node in the left subtree is "<<r->data;
// now we will replace the p with r
//first see where to do the replacement
if(parent==NULL)// we are deleting the root
{
root=r; //now replacement becomes the root
}
else if(parent->left==p)//p is on the left side of parent
parent->left=r;
else// its on right
parent->right=r;
//now do the setting in pred_r ( parent of r)
// Note that r is the right most node of the left subtree of p so
// Either 1) it is a leaf
// or 2) r has a left subtree but not a right subtree
// also r is on the right side of pred_r
pred_r->right=r->left;
//now set the left right pointers of the replacement r
r->left=p->left;
r->right=p->right;
return p;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////
Node * BST::del_Leaf(Node* p,Node* parent)
{
if(parent==NULL)//p is the root
root=NULL;
if(parent->left==p)//if p is on left
parent->left=NULL;
else// p is on right
p->right=NULL;
return p;
}
////////////////////////////////////////////
// this function returns the largest node for the tree/subtree rooted at r
// return NULL if tree/subtree rooted at r is empty. ( this should never happen in delete node with 2 children)
/////////////////////////////////////////////////////
void BST::getLargestNode(Node * &r, Node * &pred_r)
{
pred_r=NULL;
if(r==NULL)
return ;
while(r->right!=NULL){//iterate till you reach the right most node
pred_r=r;
r=r->right;
}
}
int main() {
BST tree;
tree.insert(101);
tree.insert(50);
tree.insert(10);
tree.insert(5);
tree.insert(15);
tree.insert(2);
tree.insert(7);
tree.insert(17);
tree.insert(75);
tree.insert(16);
tree.insert(500);
tree.print();
/*Node* p;
Node * parent;
tree.find(10,p,parent);*/
tree.del(50);
tree.print();
return 0;
}