clang-format and clang-tidy fixes for 0a293ece

This commit is contained in:
github-actions 2021-07-06 14:38:56 +00:00
parent d6f5e44843
commit c4e0c16710
2 changed files with 183 additions and 128 deletions

View File

@ -1,17 +1,21 @@
/** /**
* @file * @file
* @brief We are given with an array and a sum value. The algorithms find all * @brief We are given with an array and a sum value. The algorithms find all
* the subarrays of that array with sum equal to given sum and return such subarrays * the subarrays of that array with sum equal to given sum and return such
* count. This approach will have \f$O(n)\f$ time complexity and \f$O(n)\f$ space complexity. * subarrays count. This approach will have \f$O(n)\f$ time complexity and
* NOTE: In this problem, we are only refering to the continuous subsets as subarrays everywhere. Subarrays can be created using deletion operation at the end or the front of an array only. The parent array is also counted in subarrays having 0 number of deletion operations. * \f$O(n)\f$ space complexity. NOTE: In this problem, we are only refering to
* @details Subset sum(only continuous subsets) problem (https://en.wikipedia.org/wiki/Subset_sum_problem) * the continuous subsets as subarrays everywhere. Subarrays can be created
* using deletion operation at the end or the front of an array only. The parent
* array is also counted in subarrays having 0 number of deletion operations.
* @details Subset sum(only continuous subsets) problem
* (https://en.wikipedia.org/wiki/Subset_sum_problem)
* @author [Swastika Gupta](https://github.com/swastyy) * @author [Swastika Gupta](https://github.com/swastyy)
*/ */
#include <cassert> /// for assert #include <cassert> /// for assert
#include <iostream> /// for io operations #include <iostream> /// for io operations
#include <vector> /// for std::vector
#include <unordered_map> /// for unordered_map #include <unordered_map> /// for unordered_map
#include <vector> /// for std::vector
/** /**
* @namespace backtracking * @namespace backtracking
@ -20,8 +24,9 @@
namespace backtracking { namespace backtracking {
/** /**
* @namespace Subarrays * @namespace Subarrays
* @brief Functions for counting subsets(only continuous subarrays) in a given array with a given sum Time Complexity: O(n), * @brief Functions for counting subsets(only continuous subarrays) in a given
* where n is the number of elements in the given array. * array with a given sum Time Complexity: O(n), where n is the number of
* elements in the given array.
*/ */
namespace Subarrays { namespace Subarrays {
/** /**
@ -35,7 +40,8 @@ std::uint64_t subarray_sum(int sum, const std::vector<int> &in_arr) {
int nelement = in_arr.size(); int nelement = in_arr.size();
int count_of_subset = 0; int count_of_subset = 0;
int current_sum = 0; int current_sum = 0;
std::unordered_map<int, int> sumarray; // to store the subarrays count frequency having some sum value std::unordered_map<int, int> sumarray; // to store the subarrays count
// frequency having some sum value
for (int i = 0; i < nelement; i++) { for (int i = 0; i < nelement; i++) {
current_sum += in_arr[i]; current_sum += in_arr[i];
@ -62,31 +68,42 @@ static void test() {
// Test 1 // Test 1
std::cout << "1st test "; std::cout << "1st test ";
std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array std::vector<int> array1 = {-7, -3, -2, 5, 8}; // input array
assert(backtracking::Subarrays::subarray_sum(0, array1) == 1); // first argument in subarray_sum function is the required sum and second is the input array, answer is the subarray {(-3,-2,5)} assert(
backtracking::Subarrays::subarray_sum(0, array1) ==
1); // first argument in subarray_sum function is the required sum and
// second is the input array, answer is the subarray {(-3,-2,5)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 2 // Test 2
std::cout << "2nd test "; std::cout << "2nd test ";
std::vector<int> array2 = {1, 2, 3, 3}; std::vector<int> array2 = {1, 2, 3, 3};
assert(backtracking::Subarrays::subarray_sum(6, array2) == 2); // here we are expecting 2 subsets which sum up to 6 i.e. {(1,2,3),(3,3)} assert(backtracking::Subarrays::subarray_sum(6, array2) ==
2); // here we are expecting 2 subsets which sum up to 6 i.e.
// {(1,2,3),(3,3)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 3 // Test 3
std::cout << "3rd test "; std::cout << "3rd test ";
std::vector<int> array3 = {1, 1, 1, 1}; std::vector<int> array3 = {1, 1, 1, 1};
assert(backtracking::Subarrays::subarray_sum(1, array3) == 4); // here we are expecting 4 subsets which sum up to 1 i.e. {(1),(1),(1),(1)} assert(backtracking::Subarrays::subarray_sum(1, array3) ==
4); // here we are expecting 4 subsets which sum up to 1 i.e.
// {(1),(1),(1),(1)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 4 // Test 4
std::cout << "4th test "; std::cout << "4th test ";
std::vector<int> array4 = {3, 3, 3, 3}; std::vector<int> array4 = {3, 3, 3, 3};
assert(backtracking::Subarrays::subarray_sum(6, array4) == 3); // here we are expecting 3 subsets which sum up to 6 i.e. {(3,3),(3,3),(3,3)} assert(backtracking::Subarrays::subarray_sum(6, array4) ==
3); // here we are expecting 3 subsets which sum up to 6 i.e.
// {(3,3),(3,3),(3,3)}
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
// Test 5 // Test 5
std::cout << "5th test "; std::cout << "5th test ";
std::vector<int> array5 = {}; std::vector<int> array5 = {};
assert(backtracking::Subarrays::subarray_sum(6, array5) == 0); // here we are expecting 0 subsets which sum up to 6 i.e. we cannot select anything from an empty array assert(backtracking::Subarrays::subarray_sum(6, array5) ==
0); // here we are expecting 0 subsets which sum up to 6 i.e. we
// cannot select anything from an empty array
std::cout << "passed" << std::endl; std::cout << "passed" << std::endl;
} }

View File

@ -1,43 +1,45 @@
/** /**
* @file * @file
* @brief Iterative version of Preorder, Postorder, and preorder [Traversal of the Tree] * @brief Iterative version of Preorder, Postorder, and preorder [Traversal of
* (https://en.wikipedia.org/wiki/Tree_traversal) * the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)
* @author [Motasim](https://github.com/motasimmakki) * @author [Motasim](https://github.com/motasimmakki)
* @details * @details
* *
* ### Iterative Preorder Traversal of a tree * ### Iterative Preorder Traversal of a tree
* Create a Stack that will store the Node of Tree. * Create a Stack that will store the Node of Tree.
* Push the root node into the stack. * Push the root node into the stack.
* Save the root into the variabe named as current, and pop and elemnt from the stack. * Save the root into the variabe named as current, and pop and elemnt from the
* Store the data of current into the result array, and start traversing from it. * stack. Store the data of current into the result array, and start traversing
* Push both the child node of the current node into the stack, first right child then left child. * from it. Push both the child node of the current node into the stack, first
* Repeat the same set of steps untill the Stack becomes empty. * right child then left child. Repeat the same set of steps untill the Stack
* And return the result array as the preorder traversal of a tree. * becomes empty. And return the result array as the preorder traversal of a
* tree.
* *
* ### Iterative Postorder Traversal of a tree * ### Iterative Postorder Traversal of a tree
* Create a Stack that will store the Node of Tree. * Create a Stack that will store the Node of Tree.
* Push the root node into the stack. * Push the root node into the stack.
* Save the root into the variabe named as current, and pop and elemnt from the stack. * Save the root into the variabe named as current, and pop and elemnt from the
* Store the data of current into the result array, and start traversing from it. * stack. Store the data of current into the result array, and start traversing
* Push both the child node of the current node into the stack, first left child then right child. * from it. Push both the child node of the current node into the stack, first
* Repeat the same set of steps untill the Stack becomes empty. * left child then right child. Repeat the same set of steps untill the Stack
* Now reverse the result array and then return it to the calling function as a postorder traversal of a tree. * becomes empty. Now reverse the result array and then return it to the calling
* function as a postorder traversal of a tree.
* *
* ### Iterative Inorder Traversal of a tree * ### Iterative Inorder Traversal of a tree
* Create a Stack that will store the Node of Tree. * Create a Stack that will store the Node of Tree.
* Push the root node into the stack. * Push the root node into the stack.
* Save the root into the variabe named as current. * Save the root into the variabe named as current.
* Now iterate and take the current to the extreme left of the tree by traversing only to its left. * Now iterate and take the current to the extreme left of the tree by
* Pop the elemnt from the stack and assign it to the current. * traversing only to its left. Pop the elemnt from the stack and assign it to
* Store the data of current into the result array. * the current. Store the data of current into the result array. Repeat the same
* Repeat the same set of steps until the Stack becomes empty or the current becomes NULL. * set of steps until the Stack becomes empty or the current becomes NULL. And
* And return the result array as the inorder traversal of a tree. * return the result array as the inorder traversal of a tree.
*/ */
#include <algorithm> /// for `reverse`
#include <cassert> /// for `assert`
#include <iostream> /// for I/O operations #include <iostream> /// for I/O operations
#include <stack> /// for `stack` #include <stack> /// for `stack`
#include <vector> /// for `vector` #include <vector> /// for `vector`
#include <algorithm> /// for `reverse`
#include <cassert> /// for `assert`
/** /**
* @namespace others * @namespace others
@ -46,35 +48,44 @@
namespace others { namespace others {
/** /**
* @namespace iterative_tree_traversals * @namespace iterative_tree_traversals
* @brief Functions for the [Traversal of the Tree](https://en.wikipedia.org/wiki/Tree_traversal) algorithm * @brief Functions for the [Traversal of the
* Tree](https://en.wikipedia.org/wiki/Tree_traversal) algorithm
*/ */
namespace iterative_tree_traversals { namespace iterative_tree_traversals {
/** /**
* @brief defines the structure of a node of the tree * @brief defines the structure of a node of the tree
*/ */
struct Node { struct Node {
int64_t data = 0; ///< The value/key of the node. int64_t data = 0; ///< The value/key of the node.
struct Node *left; ///< struct pointer to left subtree. struct Node *left{}; ///< struct pointer to left subtree.
struct Node *right; ///< struct pointer to right subtree. struct Node *right{}; ///< struct pointer to right subtree.
}; };
/** /**
* @brief defines the functions associated with the binary tree * @brief defines the functions associated with the binary tree
*/ */
class BinaryTree { class BinaryTree {
public: public:
Node *createNewNode(int64_t); ///< function that will create new node for insertion. Node *createNewNode(
std::vector<int64_t> preOrderIterative(Node *); ///< function that takes root of the tree as an argument, and returns its preorder traversal. int64_t); ///< function that will create new node for insertion.
std::vector<int64_t> postOrderIterative(Node *); ///< function that takes root of the tree as an argument, and returns its postorder traversal. std::vector<int64_t> preOrderIterative(
std::vector<int64_t> inOrderIterative(Node *); ///< function that takes root of the tree as an argument, and returns its inorder traversal. Node *); ///< function that takes root of the tree as an argument, and
///< returns its preorder traversal.
std::vector<int64_t> postOrderIterative(
Node *); ///< function that takes root of the tree as an argument, and
///< returns its postorder traversal.
std::vector<int64_t> inOrderIterative(
Node *); ///< function that takes root of the tree as an argument, and
///< returns its inorder traversal.
}; };
/** /**
* @brief will allocate the memory for a node and, along the data and return the node. * @brief will allocate the memory for a node and, along the data and return the
* node.
* @param data value that a particular node will contain. * @param data value that a particular node will contain.
* @return pointer to the newly created node with assigned data. * @return pointer to the newly created node with assigned data.
*/ */
Node * BinaryTree::createNewNode(int64_t data) { Node *BinaryTree::createNewNode(int64_t data) {
Node *node = new Node(); Node *node = new Node();
node->data = data; node->data = data;
node->left = node->right = nullptr; node->left = node->right = nullptr;
@ -82,26 +93,28 @@ Node * BinaryTree::createNewNode(int64_t data) {
} }
/** /**
* @brief preOrderIterative() function that will perform the preorder traversal iteratively, * @brief preOrderIterative() function that will perform the preorder traversal
* and return the result array that contain the preorder traversal of a tree. * iteratively, and return the result array that contain the preorder traversal
* of a tree.
* @param root head/root node of a tree * @param root head/root node of a tree
* @return result that is containing the preorder traversal of a tree * @return result that is containing the preorder traversal of a tree
*/ */
std::vector<int64_t> BinaryTree::preOrderIterative(Node *root) { std::vector<int64_t> BinaryTree::preOrderIterative(Node *root) {
std::stack<Node *> stack; ///< is used to find and traverse the child nodes. std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< list of values, sorted in pre-order. std::vector<int64_t> result; ///< list of values, sorted in pre-order.
stack.push(root); stack.push(root);
while(!stack.empty()) { while (!stack.empty()) {
result.push_back(stack.top()->data); result.push_back(stack.top()->data);
Node *current = stack.top(); Node *current = stack.top();
stack.pop(); stack.pop();
if(current->right) { if (current->right) {
stack.push(current->right); stack.push(current->right);
} }
if(current->left) { if (current->left) {
stack.push(current->left); stack.push(current->left);
} }
} }
@ -110,26 +123,28 @@ std::vector<int64_t> BinaryTree::preOrderIterative(Node *root) {
} }
/** /**
* @brief postOrderIterative() function that will perform the postorder traversal iteratively, * @brief postOrderIterative() function that will perform the postorder
* and return the result array that contain the postorder traversal of a tree. * traversal iteratively, and return the result array that contain the postorder
* traversal of a tree.
* @param root head/root node of a tree * @param root head/root node of a tree
* @return result that is containing the postorder traversal of a tree * @return result that is containing the postorder traversal of a tree
*/ */
std::vector<int64_t> BinaryTree::postOrderIterative(Node *root) { std::vector<int64_t> BinaryTree::postOrderIterative(Node *root) {
std::stack<Node *> stack; ///< is used to find and traverse the child nodes. std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in post-order. std::vector<int64_t> result; ///< List of values, sorted in post-order.
stack.push(root); stack.push(root);
while(!stack.empty()) { while (!stack.empty()) {
result.push_back(stack.top()->data); result.push_back(stack.top()->data);
Node *current = stack.top(); Node *current = stack.top();
stack.pop(); stack.pop();
if(current->left) { if (current->left) {
stack.push(current->left); stack.push(current->left);
} }
if(current->right) { if (current->right) {
stack.push(current->right); stack.push(current->right);
} }
} }
@ -140,19 +155,21 @@ std::vector<int64_t> BinaryTree::postOrderIterative(Node *root) {
} }
/** /**
* @brief inOrderIterative() function that will perform the inorder traversal iteratively, * @brief inOrderIterative() function that will perform the inorder traversal
* and return the result array that contain the inorder traversal of a tree. * iteratively, and return the result array that contain the inorder traversal
* of a tree.
* @param root head/root node of a tree * @param root head/root node of a tree
* @return result that is containing the inorder traversal of a tree * @return result that is containing the inorder traversal of a tree
*/ */
std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) { std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
std::stack<Node *> stack; ///< is used to find and traverse the child nodes. std::stack<Node *>
stack; ///< is used to find and traverse the child nodes.
std::vector<int64_t> result; ///< List of values, sorted in in-order. std::vector<int64_t> result; ///< List of values, sorted in in-order.
Node *current = root; Node *current = root;
while(!stack.empty() || current) { while (!stack.empty() || current) {
while(current) { while (current) {
stack.push(current); stack.push(current);
current = current->left; current = current->left;
} }
@ -171,22 +188,25 @@ std::vector<int64_t> BinaryTree::inOrderIterative(Node *root) {
* @param binaryTree instance of the BinaryTree class * @param binaryTree instance of the BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test1(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test1(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{1, 2, 4, 5, 3}; std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
std::vector<int64_t> result; ///< result stores the preorder traversal of the binary tree std::vector<int64_t>
result; ///< result stores the preorder traversal of the binary tree
// Calling preOrderIterative() function by passing a root node, // Calling preOrderIterative() function by passing a root node,
// and storing the preorder traversal in result. // and storing the preorder traversal in result.
result = binaryTree.preOrderIterative(root); result = binaryTree.preOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing preorder. // Printing the result storing preorder.
std::cout<< "\nPreOrder Traversal Is : "<< std::endl; std::cout << "\nPreOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
@ -195,22 +215,25 @@ static void test1(others::iterative_tree_traversals::BinaryTree binaryTree, othe
* @param binaryTree instance of BinaryTree class * @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test2(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test2(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{4, 5, 2, 3, 1}; std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
std::vector<int64_t> result; ///< result stores the postorder traversal of the binary tree. std::vector<int64_t>
result; ///< result stores the postorder traversal of the binary tree.
// Calling postOrderIterative() function by passing a root node, // Calling postOrderIterative() function by passing a root node,
// and storing the postorder traversal in result. // and storing the postorder traversal in result.
result = binaryTree.postOrderIterative(root); result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder. // Printing the result storing postorder.
std::cout<< "\nPostOrder Traversal Is : "<< std::endl; std::cout << "\nPostOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
@ -219,22 +242,25 @@ static void test2(others::iterative_tree_traversals::BinaryTree binaryTree, othe
* @param binaryTree instance of BinaryTree class * @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test3(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test3(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{4, 2, 5, 1, 3}; std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
std::vector<int64_t> result; ///< result stores the inorder traversal of the binary tree. std::vector<int64_t>
result; ///< result stores the inorder traversal of the binary tree.
// Calling inOrderIterative() function by passing a root node, // Calling inOrderIterative() function by passing a root node,
// and storing the inorder traversal in result. // and storing the inorder traversal in result.
result = binaryTree.inOrderIterative(root); result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder. // Printing the result storing inorder.
std::cout<< "\nInOrder Traversal Is : "<< std::endl; std::cout << "\nInOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
@ -243,46 +269,53 @@ static void test3(others::iterative_tree_traversals::BinaryTree binaryTree, othe
* @param binaryTree instance of BinaryTree class * @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test4(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-1, -2, -4, -5, -3}; std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
std::vector<int64_t> result; ///< result stores the preorder traversal of the binary tree std::vector<int64_t>
result; ///< result stores the preorder traversal of the binary tree
// Calling preOrderIterative() function by passing a root node, // Calling preOrderIterative() function by passing a root node,
// and storing the preorder traversal in result. // and storing the preorder traversal in result.
result = binaryTree.preOrderIterative(root); result = binaryTree.preOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing preorder. // Printing the result storing preorder.
std::cout<< "\nPreOrder Traversal Is : "<< std::endl; std::cout << "\nPreOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
/** /**
* @brief Test the computed postorder with the actual postorder on negative value. * @brief Test the computed postorder with the actual postorder on negative
* value.
* @param binaryTree instance of BinaryTree class * @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test5(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-4, -5, -2, -3, -1}; std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
std::vector<int64_t> result; ///< result stores the postorder traversal of the binary tree. std::vector<int64_t>
result; ///< result stores the postorder traversal of the binary tree.
// Calling postOrderIterative() function by passing a root node, // Calling postOrderIterative() function by passing a root node,
// and storing the postorder traversal in result. // and storing the postorder traversal in result.
result = binaryTree.postOrderIterative(root); result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder. // Printing the result storing postorder.
std::cout<< "\nPostOrder Traversal Is : "<< std::endl; std::cout << "\nPostOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
@ -291,22 +324,25 @@ static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, othe
* @param binaryTree instance of BinaryTree class * @param binaryTree instance of BinaryTree class
* @param root head/root node of a tree * @param root head/root node of a tree
*/ */
static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root){ static void test6(others::iterative_tree_traversals::BinaryTree binaryTree,
others::iterative_tree_traversals::Node *root) {
std::vector<int64_t> actual_result{-4, -2, -5, -1, -3}; std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
std::vector<int64_t> result; ///< result stores the inorder traversal of the binary tree. std::vector<int64_t>
result; ///< result stores the inorder traversal of the binary tree.
// Calling inOrderIterative() function by passing a root node, // Calling inOrderIterative() function by passing a root node,
// and storing the inorder traversal in result. // and storing the inorder traversal in result.
result = binaryTree.inOrderIterative(root); result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for (int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder. // Printing the result storing inorder.
std::cout<< "\nInOrder Traversal Is : "<< std::endl; std::cout << "\nInOrder Traversal Is : " << std::endl;
for(auto i: result) { for (auto i : result) {
std::cout<< i<< " "; std::cout << i << " ";
} }
} }
@ -324,22 +360,24 @@ int main() {
4 5 4 5
*/ */
others::iterative_tree_traversals::BinaryTree binaryTree; ///< instace of BinaryTree, used to access its members functions. others::iterative_tree_traversals::BinaryTree
binaryTree; ///< instace of BinaryTree, used to access its members
///< functions.
others::iterative_tree_traversals::Node *root = binaryTree.createNewNode(1); others::iterative_tree_traversals::Node *root = binaryTree.createNewNode(1);
root->left = binaryTree.createNewNode(2); root->left = binaryTree.createNewNode(2);
root->right = binaryTree.createNewNode(3); root->right = binaryTree.createNewNode(3);
root->left->left = binaryTree.createNewNode(4); root->left->left = binaryTree.createNewNode(4);
root->left->right = binaryTree.createNewNode(5); root->left->right = binaryTree.createNewNode(5);
std::cout<< "\n| Tests for positive data value |"<< std::endl; std::cout << "\n| Tests for positive data value |" << std::endl;
test1(binaryTree, root); // run preorder-iterative test test1(binaryTree, root); // run preorder-iterative test
std::cout<< "\nPre-order test Passed!"<< std::endl; std::cout << "\nPre-order test Passed!" << std::endl;
test2(binaryTree, root); // run postorder-iterative test test2(binaryTree, root); // run postorder-iterative test
std::cout<< "\nPost-order test Passed!"<< std::endl; std::cout << "\nPost-order test Passed!" << std::endl;
test3(binaryTree, root); // run inorder-iterative test test3(binaryTree, root); // run inorder-iterative test
std::cout<< "\nIn-order test Passed!"<< std::endl; std::cout << "\nIn-order test Passed!" << std::endl;
// Modifying tree for negative values. // Modifying tree for negative values.
root->data = -1; root->data = -1;
@ -348,15 +386,15 @@ int main() {
root->left->left->data = -4; root->left->left->data = -4;
root->left->right->data = -5; root->left->right->data = -5;
std::cout<< "\n| Tests for negative data values |"<< std::endl; std::cout << "\n| Tests for negative data values |" << std::endl;
test4(binaryTree, root); // run preorder-iterative test on negative values test4(binaryTree, root); // run preorder-iterative test on negative values
std::cout<< "\nPre-order test on-negative value Passed!"<< std::endl; std::cout << "\nPre-order test on-negative value Passed!" << std::endl;
test5(binaryTree, root); // run postorder-iterative test on negative values test5(binaryTree, root); // run postorder-iterative test on negative values
std::cout<< "\nPost-order test on-negative value Passed!"<< std::endl; std::cout << "\nPost-order test on-negative value Passed!" << std::endl;
test6(binaryTree, root); // run inorder-iterative test on negative values test6(binaryTree, root); // run inorder-iterative test on negative values
std::cout<< "\nIn-order test on-negative value Passed!"<< std::endl; std::cout << "\nIn-order test on-negative value Passed!" << std::endl;
return 0; return 0;
} }