-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST_insert_delete.cpp
246 lines (233 loc) · 4.92 KB
/
BST_insert_delete.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
#include<iostream>
class TreeNode{
public:
int val;
TreeNode *left , *right, *parent;
TreeNode(int val, TreeNode* parent);
};
TreeNode::TreeNode(int val, TreeNode* parent)
{
this->val = val;
this->parent = parent;
right = left = NULL;
}
class BST{
TreeNode *root, *current;
public:
TreeNode* get_root(){return root;}
BST(){root = current = NULL;}
void insert(int val,bool flag = true, TreeNode* root = NULL, TreeNode* parent = NULL);
void In_order(TreeNode* temp = NULL);
void del(int val,bool flag = true, TreeNode* curr= NULL, int* third_print = NULL);
void show_del(int val);
bool search(int val, bool flag = true);
TreeNode* MIN(TreeNode* min);
};
void BST::show_del(int val)
{
std::cout << val << " is deleted Successfully " << std::endl;
}
void BST::insert(int val, bool flag, TreeNode* root, TreeNode* parent)
{
if(this->root == NULL)
{
this->root = new TreeNode(val, parent);
return;
}
if(flag)
root = this->root;
if(val == root->val)
{
std::cout << val << " is Duplicate. Which is not Allowed " << std::endl;
return;
}
if(val < root->val )
{
if(root->left == NULL)
{
root->left = new TreeNode(val, root);
return;
}
insert(val,0, root->left, root->left);
}
if(val > root->val)
{
if(root->right == NULL)
{
root->right = new TreeNode(val, root);
return;
}
insert(val,0, root->right, root->right);
}
}
void BST::del(int val,bool flag, TreeNode* curr, int* third_print)
{
if(flag)
{
if(!search(val))
throw "Sorry this value is not present in the list " ;
curr = root;
}
// ist case
if(root->left == NULL && root->right == NULL)
{
std::cout << root->val << " is deleted Successfully " << std::endl;
delete root;
root = curr = NULL;
return;
}
if(val == curr->val && curr->left == NULL && curr->right == NULL)
{
if(curr->parent->right == curr)
curr->parent->right = NULL;
else if(curr->parent->left == curr)
curr->parent->left = NULL;
int dd = curr->val;
delete curr;
curr = NULL;
if(third_print != NULL)
{
show_del(*third_print);
delete third_print;
third_print = NULL;
return;
}
show_del(dd);
return;
}
// 2nd case
if(val == curr->val && (curr->left == NULL || curr->right == NULL))
{
if(curr->right != NULL)
{
if(curr->parent == NULL)
{
curr->right->parent = curr->parent;
root = curr->right;
}
else if(curr->parent->left == curr)
{
curr->right->parent = curr->parent;
curr->parent->left = curr->right;
}
else if(curr->parent->right == curr)
{
curr->right->parent = curr->parent;
curr->parent->right = curr->right;
}
}
else if(curr->left != NULL)
{
if(curr->parent == NULL)
{
curr->left->parent = curr->parent;
root = curr->left;
}
else if(curr->parent->right == curr)
{
curr->left->parent = curr->parent;
curr->parent->right = curr->left;
}
else if(curr->parent->left == curr)
{
curr->left->parent = curr->parent;
curr->parent->left = curr->left;
}
}
int vall = curr->val;
delete curr;
curr = NULL;
if(third_print != NULL)
{
show_del(*third_print);
delete third_print;
third_print = NULL;
return;
}
show_del(vall);
return;
}
// 3rd case
if(val == curr->val && curr->right != NULL && curr->left != NULL)
{
third_print = new int;
*third_print = curr->val;
TreeNode* dell;
dell = MIN(curr->right);
curr->val = dell->val;
del(dell->val,0,dell,third_print );
return;
}
if(val < curr->val)
del(val, 0,curr->left);
else if(val > curr->val)
del(val, 0,curr->right);
}
void BST::In_order(TreeNode* temp)
{
if(root == NULL)
{
throw "Tree is Emtpy ] ";
}
if(temp == NULL)
temp = root;
if(temp->left != NULL)
In_order(temp->left);
std::cout << temp->val << " ";
if(temp->right != NULL)
In_order(temp->right);
}
bool BST::search(int val, bool flag)
{
if(flag)
{
if(root == NULL)
throw "BST is empty";
current = root;
}
if(val == current->val)
return true;
if(val < current->val)
{
current = current->left;
if(current == NULL)
return false;
}
if(val > current->val)
{
current = current->right;
if(current == NULL)
return false;
}
search(val, 0);
}
TreeNode* BST::MIN(TreeNode* min)
{
if(root == NULL)
throw "List is Empty ";
if(min == NULL)
{
min = root;
}
while(min->left != NULL)
min = min->left;
return min;
}
int main()
{
BST t;
int arr[11] = {7,3,2,0,4,5,10,8,56,12,44};
try{
for(int i = 0 ; i < 11; i++)
t.insert(arr[i]);
std::cout << "[ ";t.In_order();std::cout << "]\n";
for(int i = 0; i < 11; i++)
{
t.del(arr[i]);
std::cout << "[ ";t.In_order();std::cout << "]\n";
}
std::cout << "[ ";t.In_order();std::cout << "]\n";
}catch(const char* msg){
std::cout << msg << std::endl;
}
}