clang-format and clang-tidy fixes for f8658fe5

This commit is contained in:
github-actions 2021-07-10 06:45:21 +00:00
parent 9ff12e94c7
commit 9d3707d132

View File

@ -1,33 +1,38 @@
/** /**
* @file * @file
* @brief An implementation for finding [Inorder successor of binary search tree](https://www.youtube.com/watch?v=5cPbNCrdotA&t=904s) * @brief An implementation for finding [Inorder successor of binary search
* 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. * 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 * In this case the left most deepest node in the right subtree will come
* deep in inorder. * just 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 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 * ### 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 itself, * If a node does not have right subtree, and we already visited the node
* then the next node will be its parent node according to inorder traversal, and if we are going to parent from left, * itself, then the next node will be its parent node according to inorder
* then the parent would be unvisited. In other words, go to the nearest ancestor for which given node would be in left subtree. * 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 * #### 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, * In case if there is no link to the parent, we need to walk the tree
* by doing so, we are visiting every ancestor of the given node. In order successor would be the deepest node in * starting from the root node to the given node, by doing so, we are visiting
* this path for which given node is in left subtree. * 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) * @author [Nitin Sharma](https://github.com/foo290)
* */ * */
#include <cassert> /// For assert
#include <iostream> /// For IO Operations #include <iostream> /// For IO Operations
#include <vector> /// For std::vector #include <vector> /// For std::vector
#include <cassert> /// For assert
namespace binary_search_tree { namespace binary_search_tree {
/** /**
@ -71,7 +76,8 @@ namespace binary_search_tree {
} }
/** /**
* @brief Searches the given data in BST and returns the pointer to the node containing that data. * @brief Searches the given data in BST and returns the pointer to the node
* containing that data.
* @param root Pointer to the root node of the BST * @param root Pointer to the root node of the BST
* @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
@ -79,15 +85,12 @@ namespace binary_search_tree {
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;
} } else if (data > root->data) {
else if (data > root->data) {
/// recursive call /// recursive call
return getNode(root->right, data); return getNode(root->right, data);
} } else {
else {
/// recursive call /// recursive call
return getNode(root->left, data); return getNode(root->left, data);
} }
@ -109,19 +112,20 @@ 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
* by doing so, we are visiting every ancestor of the given node. * the root node to the given node, by doing so, we are visiting every ancestor
* In order successor would be the deepest node in this path for which given node is in left subtree. * of the given node. In order successor would be the deepest node in this path
* 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 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) {
Node *current = getNode(root, data); Node *current = getNode(root, data);
if (current == nullptr) return nullptr; if (current == nullptr)
return nullptr;
// Case - 1 // Case - 1
if (current->right != nullptr) { if (current->right != nullptr) {
@ -133,13 +137,11 @@ namespace binary_search_tree {
Node *ancestor = root; Node *ancestor = root;
while (ancestor != current && ancestor != nullptr) { while (ancestor != current && ancestor != nullptr) {
// This means my current node is in left of the root node // This means my current node is in left of the root node
if (current->data < ancestor->data) { if (current->data < ancestor->data) {
successor = ancestor; successor = ancestor;
ancestor = ancestor->left; // keep going left ancestor = ancestor->left; // keep going left
} } else {
else {
ancestor = ancestor->right; ancestor = ancestor->right;
} }
} }
@ -153,19 +155,21 @@ namespace binary_search_tree {
* @returns void * @returns void
* */ * */
void printInorder(Node *root) { void printInorder(Node *root) {
if (root == nullptr) return; if (root == nullptr)
return;
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 large data instead of hard coding it in code. * @brief This function is used in test cases to quickly create BST containing
* For a given root, this will add all the nodes containing data passes in data vector. * 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 root Pointer to the root node.
* @param data A vector containing integer values which are suppose to be inserted as nodes in BST. * @param data A vector containing integer values which are suppose to be
* 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) {
@ -209,28 +213,35 @@ public:
} }
/** /**
* @brief A test case contains edge case, printing inorder successor of last node. * @brief A test case contains edge case, printing inorder successor of last
* node.
* @returns void * @returns void
* */ * */
void testCase_1() { void testCase_1() {
const binary_search_tree::Node *expectedOutput = nullptr; ///< Expected output of this test const binary_search_tree::Node *expectedOutput =
nullptr; ///< Expected output of this test
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
log("This is test case 1 : "); log("This is test case 1 : ");
log("Description:"); log("Description:");
log(" EDGE CASE : Printing inorder successor for last node in the BST, Output will be nullptr."); log(" EDGE CASE : Printing inorder successor for last node in the "
"BST, Output will be nullptr.");
binary_search_tree::Node *root = nullptr; binary_search_tree::Node *root = nullptr;
std::vector<int64_t> node_data {20, 3, 5, 6, 2, 23, 45 , 78, 21}; ///< Data to make nodes in BST 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, node_data); ///< Adding nodes to BST root = binary_search_tree::makeBST(root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : "; std::cout << "Inorder sequence is : ";
binary_search_tree::printInorder(root); ///< Printing inorder to cross-verify. binary_search_tree::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl; std::cout << std::endl;
binary_search_tree::Node *inorderSuccessor = binary_search_tree::Node *inorderSuccessor =
binary_search_tree::getInorderSuccessor(root, 78); ///< The inorder successor node for given data binary_search_tree::getInorderSuccessor(
root, 78); ///< The inorder successor node for given data
log("Checking assert expression..."); log("Checking assert expression...");
assert(inorderSuccessor == expectedOutput); assert(inorderSuccessor == expectedOutput);
@ -255,16 +266,20 @@ public:
log("This is test case 2 : "); log("This is test case 2 : ");
binary_search_tree::Node *root = nullptr; binary_search_tree::Node *root = nullptr;
std::vector<int64_t> node_data {20, 3, 5, 6, 2, 23, 45 , 78, 21}; ///< Data to make nodes in BST 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, node_data); ///< Adding nodes to BST root = binary_search_tree::makeBST(root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : "; std::cout << "Inorder sequence is : ";
binary_search_tree::printInorder(root); ///< Printing inorder to cross-verify. binary_search_tree::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl; std::cout << std::endl;
binary_search_tree::Node *inorderSuccessor = binary_search_tree::Node *inorderSuccessor =
binary_search_tree::getInorderSuccessor(root, 20); ///< The inorder successor node for given data binary_search_tree::getInorderSuccessor(
root, 20); ///< The inorder successor node for given data
log("Checking assert expression..."); log("Checking assert expression...");
assert(inorderSuccessor->data == expectedOutput); assert(inorderSuccessor->data == expectedOutput);
@ -289,16 +304,21 @@ public:
log("This is test case 3 : "); log("This is test case 3 : ");
binary_search_tree::Node *root = nullptr; binary_search_tree::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 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, node_data); ///< Adding nodes to BST root = binary_search_tree::makeBST(root,
node_data); ///< Adding nodes to BST
std::cout << "Inorder sequence is : "; std::cout << "Inorder sequence is : ";
binary_search_tree::printInorder(root); ///< Printing inorder to cross-verify. binary_search_tree::printInorder(
root); ///< Printing inorder to cross-verify.
std::cout << std::endl; std::cout << std::endl;
binary_search_tree::Node *inorderSuccessor = binary_search_tree::Node *inorderSuccessor =
binary_search_tree::getInorderSuccessor(root, 90); ///< The inorder successor node for given data binary_search_tree::getInorderSuccessor(
root, 90); ///< The inorder successor node for given data
log("Checking assert expression..."); log("Checking assert expression...");
assert(inorderSuccessor->data == expectedOutput); assert(inorderSuccessor->data == expectedOutput);
@ -331,7 +351,8 @@ int main(int argc, char *argv[]) {
test(); /// run self-test implementations test(); /// run self-test implementations
binary_search_tree::Node *root = nullptr; ///< root node of the bst binary_search_tree::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 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. int64_t targetElement = 4; ///< An element to find inorder successor for.
root = binary_search_tree::makeBST(root, node_data); ///< Making BST root = binary_search_tree::makeBST(root, node_data); ///< Making BST
@ -345,10 +366,10 @@ int main(int argc, char *argv[]) {
if (inorderSuccessor == nullptr) { if (inorderSuccessor == nullptr) {
std::cout << "Inorder successor for last node is NULL" << std::endl; std::cout << "Inorder successor for last node is NULL" << std::endl;
} } else {
else {
std::cout << "Target element is : " << targetElement << std::endl; std::cout << "Target element is : " << targetElement << std::endl;
std::cout<<"Inorder successor for target element is : "<<inorderSuccessor->data; std::cout << "Inorder successor for target element is : "
<< inorderSuccessor->data;
} }
delete (inorderSuccessor); delete (inorderSuccessor);