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
* 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.
*
* @details
* ### Case 1: The given node has the 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.
* 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.
* 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
*
* #### 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
* itself, then the next node will be its parent node according to inorder
* traversal, and if we are going to parent from left, then the parent would be
* unvisited. In other words, go to the nearest ancestor for which given node
* would be in left subtree.
* * If a node does not have the right subtree, and we already visited the node itself,
* then the next node will be its parent node according to inorder traversal,
* and if we are going to parent from left, then the parent would be unvisited.
* * In other words, go to the nearest ancestor for which given node would be in left subtree.
*
* #### Method 2: Search from the root node
* * In case if there is no link to the parent, 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.
* * In case if there is no link from a child node to the parent node, we need to walk down 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.
*
* @author [Nitin Sharma](https://github.com/foo290)
* */
#include <cassert> /// For assert
#include <iostream> /// For IO Operations
#include <vector> /// For std::vector
#include <cassert> /// for assert
#include <iostream> /// for IO Operations
#include <vector> /// for std::vector
/**
* @namespace operations_on_datastructures
@ -99,12 +96,14 @@ Node *getNode(Node *root, int64_t data) {
if (root == nullptr) {
return nullptr;
} else if (root->data == data) {
return root;
return root; /// Node found!
} 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);
} 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);
}
}
@ -156,11 +155,13 @@ Node *makeBST(Node *root, const std::vector<int64_t> &data) {
}
/**
* @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
* 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.