mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-02-11 06:26:05 +08:00
clang-tidy errors fixed, reference docs link added
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* @file
|
* @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.
|
* 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
|
* ### 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,
|
* @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.
|
* 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.
|
* 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 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.
|
* @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.
|
* @returns Node pointer to the inorder successor node.
|
||||||
* */
|
* */
|
||||||
Node *getInorderSuccessor(Node *root, int64_t data) {
|
Node *getInorderSuccessor(Node *root, int64_t data) {
|
||||||
// O(h)
|
|
||||||
|
|
||||||
Node *current = getNode(root, data);
|
Node *current = getNode(root, data);
|
||||||
if (current == nullptr) return nullptr;
|
if (current == nullptr) return nullptr;
|
||||||
@@ -132,13 +132,14 @@ namespace binary_search_tree {
|
|||||||
Node *successor = nullptr;
|
Node *successor = nullptr;
|
||||||
Node *ancestor = root;
|
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) {
|
if (current->data < ancestor->data) {
|
||||||
// This means my current node is in left of the root node
|
|
||||||
successor = ancestor;
|
successor = ancestor;
|
||||||
ancestor = ancestor->left; // keep going left
|
ancestor = ancestor->left; // keep going left
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
ancestor = ancestor->right;
|
ancestor = ancestor->right;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -173,7 +174,7 @@ namespace binary_search_tree {
|
|||||||
}
|
}
|
||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
} // namespace binary_search_tree
|
} // namespace binary_search_tree
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief class encapsulating the necessary test cases
|
* @brief class encapsulating the necessary test cases
|
||||||
@@ -235,6 +236,9 @@ public:
|
|||||||
assert(inorderSuccessor == expectedOutput);
|
assert(inorderSuccessor == expectedOutput);
|
||||||
log("Assertion check passed!");
|
log("Assertion check passed!");
|
||||||
|
|
||||||
|
delete(inorderSuccessor);
|
||||||
|
delete(root);
|
||||||
|
|
||||||
log("[PASS] : TEST CASE 1 PASS!");
|
log("[PASS] : TEST CASE 1 PASS!");
|
||||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||||
}
|
}
|
||||||
@@ -266,6 +270,9 @@ public:
|
|||||||
assert(inorderSuccessor->data == expectedOutput);
|
assert(inorderSuccessor->data == expectedOutput);
|
||||||
log("Assertion check passed!");
|
log("Assertion check passed!");
|
||||||
|
|
||||||
|
delete(inorderSuccessor);
|
||||||
|
delete(root);
|
||||||
|
|
||||||
log("[PASS] : TEST CASE 2 PASS!");
|
log("[PASS] : TEST CASE 2 PASS!");
|
||||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||||
}
|
}
|
||||||
@@ -297,6 +304,9 @@ public:
|
|||||||
assert(inorderSuccessor->data == expectedOutput);
|
assert(inorderSuccessor->data == expectedOutput);
|
||||||
log("Assertion check passed!");
|
log("Assertion check passed!");
|
||||||
|
|
||||||
|
delete(inorderSuccessor);
|
||||||
|
delete(root);
|
||||||
|
|
||||||
log("[PASS] : TEST CASE 3 PASS!");
|
log("[PASS] : TEST CASE 3 PASS!");
|
||||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||||
}
|
}
|
||||||
@@ -341,5 +351,8 @@ int main(int argc, char *argv[]) {
|
|||||||
std::cout<<"Inorder successor for target element is : "<<inorderSuccessor->data;
|
std::cout<<"Inorder successor for target element is : "<<inorderSuccessor->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
delete(inorderSuccessor);
|
||||||
|
delete(root);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user