docs improved

This commit is contained in:
foo290 2021-07-13 07:58:14 +05:30
parent 84866c04ad
commit f1b8319824
No known key found for this signature in database
GPG Key ID: 37349CBEF6299747

View File

@ -4,36 +4,33 @@
* search tree](https://www.youtube.com/watch?v=5cPbNCrdotA&t=904s) Inorder * 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. * 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 is NULL for the last node in Inorder traversal.
* * @details
* ### Case 1: The given node has the right node/subtree * ### Case 1: The given node has the right node/subtree
* *
* In this case the left most deepest node in the right subtree will come * In this case the left most deepest node in the right subtree will come just
* just after the given node as we go to left deep in inorder. * after the given node as we go to left deep in inorder.
* - Go deep to left most node in right subtree. * - Go deep to left most node in right subtree.
* OR, we can also say in case if BST, find the minimum of the subtree * OR, we can also say in case if BST, find the minimum of the subtree for a given node.
* 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) * #### 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 * * If a node does not have the right subtree, and we already visited the node itself,
* itself, then the next node will be its parent node according to inorder * then the next node will be its parent node according to inorder traversal,
* traversal, and if we are going to parent from left, then the parent would be * and if we are going to parent from left, then the parent would be unvisited.
* unvisited. In other words, go to the nearest ancestor for which given node * * In other words, go to the nearest ancestor for which given node would be in left subtree.
* would be in left subtree.
* *
* #### Method 2: Search from the root node * #### Method 2: Search from the root node
* * In case if there is no link to the parent, we need to walk the tree * * In case if there is no link from a child node to the parent node, we need to walk down the tree starting
* starting from the root node to the given node, by doing so, we are visiting * from the root node to the given node, by doing so, we are visiting every ancestor of the given node.
* every ancestor of the given node. In order successor would be the deepest * * In order successor would be the deepest node in this path for which given node is in left subtree.
* node in this path for which given node is in left subtree.
* *
* @author [Nitin Sharma](https://github.com/foo290) * @author [Nitin Sharma](https://github.com/foo290)
* */ * */
#include <cassert> /// For assert #include <cassert> /// for assert
#include <iostream> /// For IO Operations #include <iostream> /// for IO Operations
#include <vector> /// For std::vector #include <vector> /// for std::vector
/** /**
* @namespace operations_on_datastructures * @namespace operations_on_datastructures
@ -46,30 +43,30 @@ namespace operations_on_datastructures {
* @brief Functions for the [Inorder successor of a binary search * @brief Functions for the [Inorder successor of a binary search
* tree](https://www.youtube.com/watch?v=5cPbNCrdotA) implementation * tree](https://www.youtube.com/watch?v=5cPbNCrdotA) implementation
*/ */
namespace inorder_traversal_of_bst { namespace inorder_traversal_of_bst {
/** /**
* @brief A Node structure representing a single node in BST * @brief A Node structure representing a single node in BST
*/ */
class Node { class Node {
public: public:
int64_t data; ///< The key/value of the node int64_t data; ///< The key/value of the node
Node *left; ///< Pointer to Left child Node *left; ///< Pointer to Left child
Node *right; ///< Pointer to right child Node *right; ///< Pointer to right child
}; };
/** /**
* @brief Allocates a new node in heap for given data and returns it's pointer. * @brief Allocates a new node in heap for given data and returns it's pointer.
* @param data Data for the node. * @param data Data for the node.
* @returns A pointer to the newly allocated Node. * @returns A pointer to the newly allocated Node.
* */ * */
Node *makeNode(int64_t data) { Node *makeNode(int64_t data) {
Node *node = new Node(); Node *node = new Node();
node->data = data; ///< setting data for node node->data = data; ///< setting data for node
node->left = nullptr; ///< setting left child as null node->left = nullptr; ///< setting left child as null
node->right = nullptr; ///< setting right child as null node->right = nullptr; ///< setting right child as null
return node; return node;
} }
/** /**
* @brief Inserts the given data in BST while maintaining the properties of BST. * @brief Inserts the given data in BST while maintaining the properties of BST.
@ -77,7 +74,7 @@ Node *makeNode(int64_t data) {
* @param data Data to be inserted. * @param data Data to be inserted.
* @returns Node* Pointer to the root node. * @returns Node* Pointer to the root node.
* */ * */
Node *Insert(Node *root, int64_t data) { Node *Insert(Node *root, int64_t data) {
if (root == nullptr) { if (root == nullptr) {
root = makeNode(data); root = makeNode(data);
} else if (data <= root->data) { } else if (data <= root->data) {
@ -86,7 +83,7 @@ Node *Insert(Node *root, int64_t data) {
root->right = Insert(root->right, data); root->right = Insert(root->right, data);
} }
return root; return root;
} }
/** /**
* @brief Searches the given data in BST and returns the pointer to the node * @brief Searches the given data in BST and returns the pointer to the node
@ -95,26 +92,28 @@ Node *Insert(Node *root, int64_t data) {
* @param data Data to be Searched. * @param data Data to be Searched.
* @returns Node* pointer to the found node * @returns Node* pointer to the found node
* */ * */
Node *getNode(Node *root, int64_t data) { Node *getNode(Node *root, int64_t data) {
if (root == nullptr) { if (root == nullptr) {
return nullptr; return nullptr;
} else if (root->data == data) { } else if (root->data == data) {
return root; return root; /// Node found!
} else if (data > root->data) { } else if (data > root->data) {
/// recursive call /// Traverse right subtree recursively as the given data is greater than the data in root node,
/// data must be present in right subtree.
return getNode(root->right, data); return getNode(root->right, data);
} else { } else {
/// recursive call /// Traverse left subtree recursively as the given data is less than the data in root node,
/// data must be present in left subtree.
return getNode(root->left, data); return getNode(root->left, data);
} }
} }
/** /**
* @brief Finds and return the minimum node in BST. * @brief Finds and return the minimum node in BST.
* @param root A pointer to root node. * @param root A pointer to root node.
* @returns Node* Pointer to the found node * @returns Node* Pointer to the found node
* */ * */
Node *findMinNode(Node *root) { Node *findMinNode(Node *root) {
if (root == nullptr) { if (root == nullptr) {
return root; return root;
} }
@ -122,14 +121,14 @@ Node *findMinNode(Node *root) {
root = root->left; root = root->left;
} }
return root; return root;
} }
/** /**
* @brief Prints the BST in inorder traversal using recursion. * @brief Prints the BST in inorder traversal using recursion.
* @param root A pointer to the root node of the BST. * @param root A pointer to the root node of the BST.
* @returns void * @returns void
* */ * */
void printInorder(Node *root) { void printInorder(Node *root) {
if (root == nullptr) { if (root == nullptr) {
return; return;
} }
@ -137,7 +136,7 @@ void printInorder(Node *root) {
printInorder(root->left); /// recursive call to left subtree printInorder(root->left); /// recursive call to left subtree
std::cout << root->data << " "; std::cout << root->data << " ";
printInorder(root->right); /// recursive call to right subtree printInorder(root->right); /// recursive call to right subtree
} }
/** /**
* @brief This function is used in test cases to quickly create BST containing * @brief This function is used in test cases to quickly create BST containing
@ -148,25 +147,27 @@ void printInorder(Node *root) {
* inserted as nodes in BST. * inserted as nodes in BST.
* @returns Node pointer to the root node. * @returns Node pointer to the root node.
* */ * */
Node *makeBST(Node *root, const std::vector<int64_t> &data) { Node *makeBST(Node *root, const std::vector<int64_t> &data) {
for (int64_t values : data) { for (int64_t values : data) {
root = Insert(root, values); root = Insert(root, values);
} }
return root; return root;
} }
/** /**
* @brief Search from the root node as we need to walk the tree starting from * @brief Inorder successor of a node is the next node in inorder traversal of the Binary Tree.
* This function takes the root node and the data of the node for which we have to find the inorder successor, and
* returns the inorder successor node.
* @details 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 * 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 * 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) * 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 * @param data The data (or the data of node) for which we have to find inorder
* successor. * 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) {
Node *current = getNode(root, data); Node *current = getNode(root, data);
if (current == nullptr) { if (current == nullptr) {
return nullptr; return nullptr;
@ -192,7 +193,7 @@ Node *getInorderSuccessor(Node *root, int64_t data) {
} }
return successor; // Nodes with maximum vales will not have a successor return successor; // Nodes with maximum vales will not have a successor
} }
} }
} // namespace inorder_traversal_of_bst } // namespace inorder_traversal_of_bst
} // namespace operations_on_datastructures } // namespace operations_on_datastructures
@ -200,7 +201,7 @@ Node *getInorderSuccessor(Node *root, int64_t data) {
* @brief class encapsulating the necessary test cases * @brief class encapsulating the necessary test cases
*/ */
class TestCases { class TestCases {
private: private:
/** /**
* @brief A function to print given message on console. * @brief A function to print given message on console.
* @tparam T Type of the given message. * @tparam T Type of the given message.
@ -212,7 +213,7 @@ class TestCases {
std::cout << "[TESTS] : ---> " << msg << std::endl; std::cout << "[TESTS] : ---> " << msg << std::endl;
} }
public: public:
/** /**
* @brief Executes test cases * @brief Executes test cases
* @returns void * @returns void