From f8658fe52c3dde22400e507c0ca2b6b122b39f13 Mon Sep 17 00:00:00 2001 From: foo290 Date: Sat, 10 Jul 2021 12:14:34 +0530 Subject: [PATCH] clang-tidy errors fixed, reference docs link added --- .../inorder_successor_of_bst.cpp | 27 ++++++++++++++----- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/operations_on_datastructures/inorder_successor_of_bst.cpp b/operations_on_datastructures/inorder_successor_of_bst.cpp index 07e53c61b..6509699cc 100644 --- a/operations_on_datastructures/inorder_successor_of_bst.cpp +++ b/operations_on_datastructures/inorder_successor_of_bst.cpp @@ -1,6 +1,6 @@ /** * @file - * @brief An implementation for finding [Inorder successor of binary search tree](link) + * @brief An implementation for finding [Inorder successor of binary search tree](https://www.youtube.com/watch?v=5cPbNCrdotA&t=904s) * Inorder successor of a node is the next node in Inorder traversal of the Binary Tree. Inorder Successor is NULL for the last node in Inorder traversal. * * ### Case 1 : The given node has right node/subtree @@ -112,13 +112,13 @@ namespace binary_search_tree { * @brief Search from the root node as we need to walk the tree starting from the root node to the given node, * by doing so, we are visiting every ancestor of the given node. * In order successor would be the deepest node in this path for which given node is in left subtree. + * Time complexity O(h) * * @param root A pointer to the root node of the BST * @param data The data (or the data of node) for which we have to find inorder successor. * @returns Node pointer to the inorder successor node. * */ Node *getInorderSuccessor(Node *root, int64_t data) { - // O(h) Node *current = getNode(root, data); if (current == nullptr) return nullptr; @@ -132,13 +132,14 @@ namespace binary_search_tree { Node *successor = nullptr; Node *ancestor = root; - while (ancestor != current) { + while (ancestor != current && ancestor != nullptr) { + // This means my current node is in left of the root node if (current->data < ancestor->data) { - // This means my current node is in left of the root node successor = ancestor; ancestor = ancestor->left; // keep going left - } else { + } + else { ancestor = ancestor->right; } } @@ -173,7 +174,7 @@ namespace binary_search_tree { } return root; } - } // namespace binary_search_tree +} // namespace binary_search_tree /** * @brief class encapsulating the necessary test cases @@ -235,6 +236,9 @@ public: assert(inorderSuccessor == expectedOutput); log("Assertion check passed!"); + delete(inorderSuccessor); + delete(root); + log("[PASS] : TEST CASE 1 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } @@ -266,6 +270,9 @@ public: assert(inorderSuccessor->data == expectedOutput); log("Assertion check passed!"); + delete(inorderSuccessor); + delete(root); + log("[PASS] : TEST CASE 2 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } @@ -297,6 +304,9 @@ public: assert(inorderSuccessor->data == expectedOutput); log("Assertion check passed!"); + delete(inorderSuccessor); + delete(root); + log("[PASS] : TEST CASE 3 PASS!"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } @@ -341,5 +351,8 @@ int main(int argc, char *argv[]) { std::cout<<"Inorder successor for target element is : "<data; } + delete(inorderSuccessor); + delete(root); + return 0; -} \ No newline at end of file +}