@@ -194,9 +194,25 @@ namespace operations_on_datastructures {
194
194
return successor; // Nodes with maximum vales will not have a successor
195
195
}
196
196
}
197
- } // namespace inorder_traversal_of_bst
197
+
198
+ /* *
199
+ * @brief This function clears the memory allocated to entire tree recursively. Its just for clean up the
200
+ * memory and not relevant to the actual topic.
201
+ * @param root Root node of the tree.
202
+ * @returns void
203
+ * */
204
+ void deallocate (Node* rootNode){
205
+ if (rootNode == nullptr ) return ;
206
+ deallocate (rootNode->left );
207
+ deallocate (rootNode->right );
208
+ delete (rootNode);
209
+ }
210
+
211
+ } // namespace inorder_traversal_of_bst
198
212
} // namespace operations_on_datastructures
199
213
214
+
215
+
200
216
/* *
201
217
* @brief class encapsulating the necessary test cases
202
218
*/
@@ -267,8 +283,7 @@ class TestCases {
267
283
assert (inorderSuccessor == expectedOutput);
268
284
log (" Assertion check passed!" );
269
285
270
- delete (inorderSuccessor);
271
- delete (root);
286
+ operations_on_datastructures::inorder_traversal_of_bst::deallocate (root); // / memory cleanup!
272
287
273
288
log (" [PASS] : TEST CASE 1 PASS!" );
274
289
log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
@@ -308,8 +323,7 @@ class TestCases {
308
323
assert (inorderSuccessor->data == expectedOutput);
309
324
log (" Assertion check passed!" );
310
325
311
- delete (inorderSuccessor);
312
- delete (root);
326
+ operations_on_datastructures::inorder_traversal_of_bst::deallocate (root); // / memory cleanup!
313
327
314
328
log (" [PASS] : TEST CASE 2 PASS!" );
315
329
log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
@@ -350,8 +364,7 @@ class TestCases {
350
364
assert (inorderSuccessor->data == expectedOutput);
351
365
log (" Assertion check passed!" );
352
366
353
- delete (inorderSuccessor);
354
- delete (root);
367
+ operations_on_datastructures::inorder_traversal_of_bst::deallocate (root); // / memory cleanup!
355
368
356
369
log (" [PASS] : TEST CASE 3 PASS!" );
357
370
log (" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" );
@@ -401,8 +414,7 @@ int main(int argc, char *argv[]) {
401
414
<< inorderSuccessor->data ;
402
415
}
403
416
404
- delete (inorderSuccessor);
405
- delete (root);
417
+ deallocate (root); // / memory cleanup!
406
418
407
419
return 0 ;
408
420
}
0 commit comments