-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA2.cpp
385 lines (331 loc) · 12.3 KB
/
A2.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
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
/**
* @brief: A2.cpp.
*
* @paragraph: Code Description - This code is used to demonstrate a delete operation from a Binary Search Tree (BST).
*
* @paragraph: Problem Statement - We have discussed Binary Search Trees (BST). Write a program to implement a delete operation from BST. You will have to write the program to insert nodes in the BST also (we already did the algorithm in detail in the class for insert). DO NOT USE ANY LIBRARIES. Insert the following nodes in the order mentioned here. 40, 60, 20, 80, 50, 10, 30, 15, 5, 35, 25, 45, 55, 70, 90, 32, 33, 48, 46. Do an inorder traversal. Now delete 40 (you decide predecessor or successor). Do inorder traversal again. Now delete 20. Do inorder traversal.
*
* @author: Shreyans Patel (SSP210009)
*/
#include <iostream>
#include <stdlib.h>
//Defines for the two values to be deleted as per the problem statement. Change these to test different values. Add defines for testing more values. main function contents will change accordingly if the defines are increased or decreased. Reference can be taken from code in main function.
#define VAL_1_TO_DELETE 40
#define VAL_2_TO_DELETE 20
//The array given as per the problem statement. Make changes here and update the number of elements to change the input as desired.
int DataArr[19] = {40, 60, 20, 80, 50, 10, 30, 15, 5, 35, 25, 45, 55, 70, 90, 32, 33, 48, 46};
//Derive number of elements from the array
int DataArrElementNum = (sizeof(DataArr)/sizeof(DataArr[0]));
//Node structure of the BST. Each node has an integer value and pointers to left child and right child of type "BSTNode".
struct BSTNode
{
int val; //val: Variable with integer value.
BSTNode *left; //left: Pointer to left child.
BSTNode *right; //right: Pointer to right child.
//Initializing structure members with default values.
BSTNode()
{
val = 0;
left = NULL;
right = NULL;
}
};
//Initializing the root BSTNode pointer for the BST.
BSTNode *pRoot = NULL;
/**
* @brief: getNewBSTNode.
* @details: Used to create a new BSTNode with the given value.
* @param: iVal (Input) - Value to assign to the node's data.
* @return BSTNode* - The newly created BST node.
*/
BSTNode* getNewBSTNode(int iVal)
{
BSTNode *tpNode = NULL;
tpNode = new BSTNode;
if(NULL == tpNode)
{
std::cout<<"Memory allocation failed.....exiting....."<<std::endl;
}
tpNode->val = iVal;
return tpNode;
}
/**
* @brief: insertBSTNode.
* @details: Used to add a new node to the BST.
* @param: iBSTNode (Input) - Node to add to the BST.
* @return none.
*/
void insertBSTNode(BSTNode *iBSTNode)
{
//New node is null.
if(NULL == iBSTNode)
{
return;
}
//If root node is null, then the node being added is the first node to be added in the BST i.e. it becomes the root node.
if(NULL == pRoot)
{
pRoot = iBSTNode;
return;
}
BSTNode *tpBSTNode = pRoot; //For traversal purpose without modifying the root node.
while(true)
{
//If value of node to be added is greater than or equal to the current node.
if(tpBSTNode->val <= iBSTNode->val)
{
//If right child is null, new node becomes the right child.
if(NULL == tpBSTNode->right)
{
tpBSTNode->right = iBSTNode;
return;
}
//Traverse to the right child.
tpBSTNode = tpBSTNode->right;
}
//If value of node to be added is smaller than the current node.
if(tpBSTNode->val > iBSTNode->val)
{
//If left child is null, new node becomes the left child.
if(NULL == tpBSTNode->left)
{
tpBSTNode->left = iBSTNode;
return;
}
//Traverse to the left child.
tpBSTNode = tpBSTNode->left;
}
}
}
/**
* @brief: createRequiredTree.
* @details: Used to create BST automatically by adding all values of the pre-defined array to the BST. It is used for initialization.
* @return none.
*/
void createRequiredTree(void)
{
//This is used to automate the BST creation process and avoid hardcoded sections in main for adding each node to the BST.
for(int tIterator = 0; tIterator < DataArrElementNum; tIterator++)
{
BSTNode *tpBSTNode = getNewBSTNode(DataArr[tIterator]);
if(NULL == tpBSTNode)
{
return;
}
insertBSTNode(tpBSTNode);
tpBSTNode = NULL;
}
}
/**
* @brief: printByInorderTraversal.
* @details: Used to print the BST contents by inorder traversal.
* @param: ipBSTRootNode (Input) - Root node of the BST.
* @return none.
*/
void printByInorderTraversal(BSTNode *ipBSTRootNode)
{
//Root is null.
if(NULL == ipBSTRootNode)
{
return;
}
printByInorderTraversal(ipBSTRootNode->left);
std::cout<<ipBSTRootNode->val<<" "; //For proper data representation.
printByInorderTraversal(ipBSTRootNode->right);
}
/**
* @brief: deleteBSTNode.
* @details: Used to delete a BST node from the BST using its data value.
* @param: iVal (Input) - Data value of the BST node to be deleted.
* @return none.
*/
void deleteBSTNode(int iVal)
{
//Root is null.
if(NULL == pRoot)
{
return;
}
BSTNode *tpBSTNode = pRoot; //Used for traversal.
BSTNode *tpBSTNodeToDelete = NULL; //Used for saving the address of node to be deleted.
BSTNode *tpBSTSuccessorNodeParent = NULL; //Used for saving the address of successor's parent for linking/unlinking as per need.
BSTNode *tpBSTSuccessorNode = NULL; //Used for saving the successor of the node to be deleted.
//This loop will traverse until we get the node which is to be deleted and it points to the parent of this node by tpBSTSuccessorNodeParent.
while(true)
{
//Value matched. Update tpBSTNodeToDelete.
if(tpBSTNode->val == iVal)
{
tpBSTNodeToDelete = tpBSTNode;
break;
}
//Value is smaller, traverse to the left and find.
if(tpBSTNode->val > iVal)
{
//Nothing left on left. Value not found.
if(NULL == tpBSTNode->left)
{
return;
}
tpBSTSuccessorNodeParent = tpBSTNode;
tpBSTNode = tpBSTNode->left;
}
//Value is larger, traverse to the right and find.
if(tpBSTNode->val < iVal)
{
//Nothing left on right. Value not found.
if(NULL == tpBSTNode->right)
{
return;
}
tpBSTSuccessorNodeParent = tpBSTNode;
tpBSTNode = tpBSTNode->right;
}
}
//If node to be deleted is the leaf node.
if((NULL == tpBSTNodeToDelete->right) && (NULL == tpBSTNodeToDelete->left))
{
//Root Node is the node to delete and it is also the leaf node.
if(pRoot == tpBSTNodeToDelete)
{
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
pRoot = NULL;
return;
}
//If leaf node is the left child of its parent.
if(tpBSTSuccessorNodeParent->left == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->left = NULL;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
//If leaf node is the right child of its parent.
else if(tpBSTSuccessorNodeParent->right == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->right = NULL;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
return;
}
//Node to delete only has left child.
if((NULL == tpBSTNodeToDelete->right) && (NULL != tpBSTNodeToDelete->left))
{
//Root Node is the node to delete and it has only a left child.
if(pRoot == tpBSTNodeToDelete)
{
pRoot = tpBSTNodeToDelete->left;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
return;
}
//If node to delete is the left child of the parent.
if(tpBSTSuccessorNodeParent->left == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->left = tpBSTNodeToDelete->left;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
//If node to delete is the right child of the parent.
else if(tpBSTSuccessorNodeParent->right == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->right = tpBSTNodeToDelete->left;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
return;
}
//Node to delete only has right child
if((NULL != tpBSTNodeToDelete->right) && (NULL == tpBSTNodeToDelete->left))
{
//Root Node is the node to delete and it has only a right child
if(pRoot == tpBSTNodeToDelete)
{
pRoot = tpBSTNodeToDelete->right;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
return;
}
//If node to delete is the left child of the parent.
if(tpBSTSuccessorNodeParent->left == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->left = tpBSTNodeToDelete->right;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
//If node to delete is the right child of the parent.
else if(tpBSTSuccessorNodeParent->right == tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->right = tpBSTNodeToDelete->right;
delete tpBSTNodeToDelete;
tpBSTNodeToDelete = NULL;
}
return;
}
//Initialize successor and successor parent node for using deletion by successor method.
tpBSTSuccessorNode = tpBSTNodeToDelete;
tpBSTSuccessorNodeParent = tpBSTSuccessorNode;
//Go right for one time for finding the successor.
tpBSTSuccessorNode = tpBSTSuccessorNode->right;
//Go to the left child until null is encountered. Last node is the successor.
while(NULL != tpBSTSuccessorNode->left)
{
tpBSTSuccessorNodeParent = tpBSTSuccessorNode;
tpBSTSuccessorNode = tpBSTSuccessorNode->left;
}
//Swap the data values.
tpBSTNodeToDelete->val = tpBSTSuccessorNode->val;
//If the successor has a right child.
if(NULL != tpBSTSuccessorNode->right)
{
//Link parent to successor's right child.
if(tpBSTSuccessorNodeParent != tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->left = tpBSTSuccessorNode->right;
}
//If the successor is the right child of node to be deleted -> there was no left child after that. Then, link parent to its right child.
else
{
tpBSTSuccessorNodeParent->right = tpBSTSuccessorNode->right;
}
}
//If the successor does not have a right child.
else
{
//Make parent's left node null.
if(tpBSTSuccessorNodeParent != tpBSTNodeToDelete)
{
tpBSTSuccessorNodeParent->left = NULL;
}
//If the successor is the right child of node to be deleted -> there was no left child after that. Then, make parent's right child null.
else
{
tpBSTSuccessorNodeParent->right = NULL;
}
}
//Delete the successor node.
delete tpBSTSuccessorNode;
tpBSTSuccessorNode = NULL;
}
int main()
{
std::cout<<std::endl<<"Creating the BST tree from the given array....."<<std::endl<<std::endl;
createRequiredTree(); //Prepare the BST tree as per the given data.
std::cout<<"Initial inorder traversal after creating the BST: "<<std::endl<<std::endl;
std::cout<<"====> ";
printByInorderTraversal(pRoot);
std::cout<<std::endl<<std::endl;
deleteBSTNode(VAL_1_TO_DELETE); //Delete node with given value.
std::cout<<"Inorder traversal after deleting "<<VAL_1_TO_DELETE<<": "<<std::endl<<std::endl;
std::cout<<"====> ";
printByInorderTraversal(pRoot);
std::cout<<std::endl<<std::endl;
deleteBSTNode(VAL_2_TO_DELETE); //Delete node with given value.
std::cout<<"Inorder traversal after deleting "<<VAL_2_TO_DELETE<<": "<<std::endl<<std::endl;
std::cout<<"====> ";
printByInorderTraversal(pRoot);
std::cout<<std::endl<<std::endl;
std::cout<<"Execution completed, exiting....."<<std::endl<<std::endl;
return 0;
}