Skip to content

Commit e4e3ec9

Browse files
committed
memory leaks patched
1 parent f1b8319 commit e4e3ec9

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

operations_on_datastructures/inorder_successor_of_bst.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,25 @@ namespace operations_on_datastructures {
194194
return successor; // Nodes with maximum vales will not have a successor
195195
}
196196
}
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
198212
} // namespace operations_on_datastructures
199213

214+
215+
200216
/**
201217
* @brief class encapsulating the necessary test cases
202218
*/
@@ -267,8 +283,7 @@ class TestCases {
267283
assert(inorderSuccessor == expectedOutput);
268284
log("Assertion check passed!");
269285

270-
delete (inorderSuccessor);
271-
delete (root);
286+
operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup!
272287

273288
log("[PASS] : TEST CASE 1 PASS!");
274289
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
@@ -308,8 +323,7 @@ class TestCases {
308323
assert(inorderSuccessor->data == expectedOutput);
309324
log("Assertion check passed!");
310325

311-
delete (inorderSuccessor);
312-
delete (root);
326+
operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup!
313327

314328
log("[PASS] : TEST CASE 2 PASS!");
315329
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
@@ -350,8 +364,7 @@ class TestCases {
350364
assert(inorderSuccessor->data == expectedOutput);
351365
log("Assertion check passed!");
352366

353-
delete (inorderSuccessor);
354-
delete (root);
367+
operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup!
355368

356369
log("[PASS] : TEST CASE 3 PASS!");
357370
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
@@ -401,8 +414,7 @@ int main(int argc, char *argv[]) {
401414
<< inorderSuccessor->data;
402415
}
403416

404-
delete (inorderSuccessor);
405-
delete (root);
417+
deallocate(root); /// memory cleanup!
406418

407419
return 0;
408420
}

0 commit comments

Comments
 (0)