mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
docs formatting changed, namespace added
This commit is contained in:
parent
9d3707d132
commit
f54f31cd4f
@ -1,18 +1,23 @@
|
||||
/**
|
||||
* @file
|
||||
* @brief An implementation for finding [Inorder successor of binary search
|
||||
* @brief An implementation for finding the [Inorder successor of a 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
|
||||
* ### Case 1
|
||||
*
|
||||
* The given node has right node/subtree
|
||||
*
|
||||
* In this case the left most deepest node in the right subtree will come
|
||||
* just after the given node as we go to left deep in inorder.
|
||||
* - Go deep to left most node in right subtree.
|
||||
* OR, we can also say in case if BST, find the minimum of the subtree
|
||||
* for a given node.
|
||||
*
|
||||
* ### Case 2 : The given node does not have a right node/subtree
|
||||
* ### Case 2
|
||||
*
|
||||
* The given node does not have a right node/subtree
|
||||
*
|
||||
* #### Method 1: Use parent pointer (store the address of parent nodes)
|
||||
* If a node does not have right subtree, and we already visited the node
|
||||
@ -34,7 +39,19 @@
|
||||
#include <iostream> /// For IO Operations
|
||||
#include <vector> /// For std::vector
|
||||
|
||||
namespace binary_search_tree {
|
||||
/**
|
||||
* @namespace operations_on_datastructures
|
||||
* @brief Operations on data structures
|
||||
*/
|
||||
namespace operations_on_datastructures {
|
||||
|
||||
/**
|
||||
* @namespace inorder_successor_of_bst
|
||||
* @brief Functions for the [Inorder successor of a binary search
|
||||
* tree](https://www.youtube.com/watch?v=5cPbNCrdotA) implementation
|
||||
*/
|
||||
namespace inorder_traversal_of_bst {
|
||||
|
||||
/**
|
||||
* @brief A Node structure representing a single node in bst.
|
||||
*/
|
||||
@ -111,6 +128,37 @@ Node *findMinNode(Node *root) {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prints the BST in inorder traversal using recursion.
|
||||
* @param root A pointer to the root node of the BST.
|
||||
* @returns void
|
||||
* */
|
||||
void printInorder(Node *root) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
printInorder(root->left); /// recursive call to left subtree
|
||||
std::cout << root->data << " ";
|
||||
printInorder(root->right); /// recursive call to right subtree
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is used in test cases to quickly create BST containing
|
||||
* large data instead of hard coding it in code. For a given root, this will add
|
||||
* all the nodes containing data passes in data vector.
|
||||
* @param root Pointer to the root node.
|
||||
* @param data A vector containing integer values which are suppose to be
|
||||
* inserted as nodes in BST.
|
||||
* @returns Node pointer to the root node.
|
||||
* */
|
||||
Node *makeBST(Node *root, const std::vector<int64_t> &data) {
|
||||
for (int64_t values : data) {
|
||||
root = Insert(root, values);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
@ -124,8 +172,9 @@ Node *findMinNode(Node *root) {
|
||||
* */
|
||||
Node *getInorderSuccessor(Node *root, int64_t data) {
|
||||
Node *current = getNode(root, data);
|
||||
if (current == nullptr)
|
||||
if (current == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Case - 1
|
||||
if (current->right != nullptr) {
|
||||
@ -148,37 +197,8 @@ Node *getInorderSuccessor(Node *root, int64_t data) {
|
||||
return successor; // Nodes with maximum vales will not have a successor
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Prints the BST in inorder traversal using recursion.
|
||||
* @param root A pointer to the root node of the BST.
|
||||
* @returns void
|
||||
* */
|
||||
void printInorder(Node *root) {
|
||||
if (root == nullptr)
|
||||
return;
|
||||
|
||||
printInorder(root->left); /// recursive call to left subtree
|
||||
std::cout << root->data << " ";
|
||||
printInorder(root->right); /// recursive call to right subtree
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief This function is used in test cases to quickly create BST containing
|
||||
* large data instead of hard coding it in code. For a given root, this will add
|
||||
* all the nodes containing data passes in data vector.
|
||||
* @param root Pointer to the root node.
|
||||
* @param data A vector containing integer values which are suppose to be
|
||||
* inserted as nodes in BST.
|
||||
* @returns Node pointer to the root node.
|
||||
* */
|
||||
Node *makeBST(Node *root, const std::vector<int64_t> &data) {
|
||||
for (int64_t values : data) {
|
||||
root = Insert(root, values);
|
||||
}
|
||||
return root;
|
||||
}
|
||||
} // namespace binary_search_tree
|
||||
} // namespace inorder_traversal_of_bst
|
||||
} // namespace operations_on_datastructures
|
||||
|
||||
/**
|
||||
* @brief class encapsulating the necessary test cases
|
||||
@ -218,7 +238,7 @@ class TestCases {
|
||||
* @returns void
|
||||
* */
|
||||
void testCase_1() {
|
||||
const binary_search_tree::Node *expectedOutput =
|
||||
const operations_on_datastructures::inorder_traversal_of_bst::Node *expectedOutput =
|
||||
nullptr; ///< Expected output of this test
|
||||
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
@ -227,20 +247,20 @@ class TestCases {
|
||||
log(" EDGE CASE : Printing inorder successor for last node in the "
|
||||
"BST, Output will be nullptr.");
|
||||
|
||||
binary_search_tree::Node *root = nullptr;
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *root = nullptr;
|
||||
std::vector<int64_t> node_data{
|
||||
20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
|
||||
|
||||
root = binary_search_tree::makeBST(root,
|
||||
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(root,
|
||||
node_data); ///< Adding nodes to BST
|
||||
|
||||
std::cout << "Inorder sequence is : ";
|
||||
binary_search_tree::printInorder(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
|
||||
root); ///< Printing inorder to cross-verify.
|
||||
std::cout << std::endl;
|
||||
|
||||
binary_search_tree::Node *inorderSuccessor =
|
||||
binary_search_tree::getInorderSuccessor(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *inorderSuccessor =
|
||||
operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor(
|
||||
root, 78); ///< The inorder successor node for given data
|
||||
|
||||
log("Checking assert expression...");
|
||||
@ -265,20 +285,20 @@ class TestCases {
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
log("This is test case 2 : ");
|
||||
|
||||
binary_search_tree::Node *root = nullptr;
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *root = nullptr;
|
||||
std::vector<int64_t> node_data{
|
||||
20, 3, 5, 6, 2, 23, 45, 78, 21}; ///< Data to make nodes in BST
|
||||
|
||||
root = binary_search_tree::makeBST(root,
|
||||
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(root,
|
||||
node_data); ///< Adding nodes to BST
|
||||
|
||||
std::cout << "Inorder sequence is : ";
|
||||
binary_search_tree::printInorder(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
|
||||
root); ///< Printing inorder to cross-verify.
|
||||
std::cout << std::endl;
|
||||
|
||||
binary_search_tree::Node *inorderSuccessor =
|
||||
binary_search_tree::getInorderSuccessor(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *inorderSuccessor =
|
||||
operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor(
|
||||
root, 20); ///< The inorder successor node for given data
|
||||
|
||||
log("Checking assert expression...");
|
||||
@ -303,21 +323,21 @@ class TestCases {
|
||||
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
||||
log("This is test case 3 : ");
|
||||
|
||||
binary_search_tree::Node *root = nullptr;
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *root = nullptr;
|
||||
std::vector<int64_t> node_data{
|
||||
89, 67, 32, 56, 90, 123, 120,
|
||||
110, 115, 6, 78, 7, 10}; ///< Data to make nodes in BST
|
||||
|
||||
root = binary_search_tree::makeBST(root,
|
||||
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(root,
|
||||
node_data); ///< Adding nodes to BST
|
||||
|
||||
std::cout << "Inorder sequence is : ";
|
||||
binary_search_tree::printInorder(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::printInorder(
|
||||
root); ///< Printing inorder to cross-verify.
|
||||
std::cout << std::endl;
|
||||
|
||||
binary_search_tree::Node *inorderSuccessor =
|
||||
binary_search_tree::getInorderSuccessor(
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *inorderSuccessor =
|
||||
operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor(
|
||||
root, 90); ///< The inorder successor node for given data
|
||||
|
||||
log("Checking assert expression...");
|
||||
@ -350,18 +370,18 @@ static void test() {
|
||||
int main(int argc, char *argv[]) {
|
||||
test(); /// run self-test implementations
|
||||
|
||||
binary_search_tree::Node *root = nullptr; ///< root node of the bst
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *root = nullptr; ///< root node of the bst
|
||||
std::vector<int64_t> node_data{3, 4, 5,
|
||||
89, 1, 2}; ///< Data to add nodes in BST
|
||||
|
||||
int64_t targetElement = 4; ///< An element to find inorder successor for.
|
||||
root = binary_search_tree::makeBST(root, node_data); ///< Making BST
|
||||
root = operations_on_datastructures::inorder_traversal_of_bst::makeBST(root, node_data); ///< Making BST
|
||||
|
||||
binary_search_tree::Node *inorderSuccessor =
|
||||
binary_search_tree::getInorderSuccessor(root, targetElement);
|
||||
operations_on_datastructures::inorder_traversal_of_bst::Node *inorderSuccessor =
|
||||
operations_on_datastructures::inorder_traversal_of_bst::getInorderSuccessor(root, targetElement);
|
||||
|
||||
std::cout << "In-order sequence is : ";
|
||||
binary_search_tree::printInorder(root);
|
||||
operations_on_datastructures::inorder_traversal_of_bst::printInorder(root);
|
||||
std::cout << std::endl;
|
||||
|
||||
if (inorderSuccessor == nullptr) {
|
||||
|
Loading…
Reference in New Issue
Block a user