From e4e3ec98ba39a219629be5a46a74e64d0382cbe2 Mon Sep 17 00:00:00 2001 From: foo290 Date: Sat, 17 Jul 2021 21:19:49 +0530 Subject: [PATCH] memory leaks patched --- .../inorder_successor_of_bst.cpp | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/operations_on_datastructures/inorder_successor_of_bst.cpp b/operations_on_datastructures/inorder_successor_of_bst.cpp index ffbbd9797..0fa60fbda 100644 --- a/operations_on_datastructures/inorder_successor_of_bst.cpp +++ b/operations_on_datastructures/inorder_successor_of_bst.cpp @@ -194,9 +194,25 @@ namespace operations_on_datastructures { return successor; // Nodes with maximum vales will not have a successor } } -} // namespace inorder_traversal_of_bst + + /** + * @brief This function clears the memory allocated to entire tree recursively. Its just for clean up the + * memory and not relevant to the actual topic. + * @param root Root node of the tree. + * @returns void + * */ + void deallocate (Node* rootNode){ + if (rootNode == nullptr) return; + deallocate(rootNode->left); + deallocate(rootNode->right); + delete(rootNode); + } + + } // namespace inorder_traversal_of_bst } // namespace operations_on_datastructures + + /** * @brief class encapsulating the necessary test cases */ @@ -267,8 +283,7 @@ public: assert(inorderSuccessor == expectedOutput); log("Assertion check passed!"); - delete (inorderSuccessor); - delete (root); + operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup! log("[PASS] : TEST CASE 1 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); @@ -308,8 +323,7 @@ public: assert(inorderSuccessor->data == expectedOutput); log("Assertion check passed!"); - delete (inorderSuccessor); - delete (root); + operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup! log("[PASS] : TEST CASE 2 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); @@ -350,8 +364,7 @@ public: assert(inorderSuccessor->data == expectedOutput); log("Assertion check passed!"); - delete (inorderSuccessor); - delete (root); + operations_on_datastructures::inorder_traversal_of_bst::deallocate(root); /// memory cleanup! log("[PASS] : TEST CASE 3 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); @@ -401,8 +414,7 @@ int main(int argc, char *argv[]) { << inorderSuccessor->data; } - delete (inorderSuccessor); - delete (root); + deallocate(root); /// memory cleanup! return 0; }