clang-format and clang-tidy fixes for c69b85b5

This commit is contained in:
David
2022-01-04 09:10:41 +00:00
parent afa6641bba
commit 1bed798107

View File

@@ -15,9 +15,9 @@
* (R) Recursively traverse its right subtree. When this step is finished,
* we are back at n again.
*
* In normal inorder traversal, we visit the left subtree before the right subtree.
* If we visit the right subtree before visiting the left subtree, it is referred
* to as reverse inorder traversal.
* In normal inorder traversal, we visit the left subtree before the right
*subtree. If we visit the right subtree before visiting the left subtree, it is
*referred to as reverse inorder traversal.
*
* ### Iterative Preorder Traversal of a tree
* For traversing a (non-empty) binary tree in a preorder fashion, we must do
@@ -29,9 +29,9 @@
* (R) Recursively traverse its right subtree. When this step is finished,
* we are back at n again.
*
* In normal preorder traversal, visit the left subtree before the right subtree.
* If we visit the right subtree before visiting the left subtree, it is referred
* to as reverse preorder traversal.
* In normal preorder traversal, visit the left subtree before the right
*subtree. If we visit the right subtree before visiting the left subtree, it is
*referred to as reverse preorder traversal.
*
* ### Iterative Postorder Traversal of a tree
* For traversing a (non-empty) binary tree in a postorder fashion, we must do
@@ -43,17 +43,17 @@
* we are back at n again.
* (N) Process n itself.
*
* In normal postorder traversal, visit the left subtree before the right subtree.
* If we visit the right subtree before visiting the left subtree, it is referred
* to as reverse postorder traversal.
* In normal postorder traversal, visit the left subtree before the right
*subtree. If we visit the right subtree before visiting the left subtree, it is
*referred to as reverse postorder traversal.
*
* @author [Lajat Manekar](https://github.com/Lazeeez)
*
******************************************************************************/
******************************************************************************/
#include <iostream> /// for I/O operations
#include <cassert> /// for assert
#include <vector> /// for vector
#include <cassert> /// for assert
#include <iostream> /// for I/O operations
#include <vector> /// for vector
/******************************************************************************
* @namespace others
@@ -61,117 +61,126 @@
*******************************************************************************/
namespace others {
/******************************************************************************
* @namespace interpolation_search
* @brief Functions for the Recursive version of Inorder, Preorder, and Postorder
* [Traversal of the Tree](https://en.wikipedia.org/wiki/Tree_traversal) algorithm
* implementation
*******************************************************************************/
namespace recursive_tree_traversals {
/******************************************************************************
* @namespace interpolation_search
* @brief Functions for the Recursive version of Inorder, Preorder, and
*Postorder [Traversal of the
*Tree](https://en.wikipedia.org/wiki/Tree_traversal) algorithm implementation
*******************************************************************************/
namespace recursive_tree_traversals {
/******************************************************************************
* @brief The structure to hold Nodes of the tree.
* @param data Value that will be stored in the node.
* @param left follow up left subtree.
* @param right follow up right subtree.
*******************************************************************************/
struct Node {
uint64_t data = 0; ///< The value/key of the node.
struct Node *left{}; ///< struct pointer to left subtree.
struct Node *right{}; ///< struct pointer to right subtree.
};
/******************************************************************************
* @brief BT used to make the entire structure of the binary tree and the functions
* associated with the binary tree
*******************************************************************************/
class BT {
/******************************************************************************
* @brief The structure to hold Nodes of the tree.
* @param data Value that will be stored in the node.
* @param left follow up left subtree.
* @param right follow up right subtree.
*******************************************************************************/
struct Node {
uint64_t data = 0; ///< The value/key of the node.
struct Node *left{}; ///< struct pointer to left subtree.
struct Node *right{}; ///< struct pointer to right subtree.
};
/******************************************************************************
* @brief BT used to make the entire structure of the binary tree and the
*functions associated with the binary tree
*******************************************************************************/
class BT {
public:
std::vector<uint64_t>
inorder_result; // vector to store the inorder traversal of the tree.
std::vector<uint64_t>
preorder_result; // vector to store the preorder traversal of the tree.
std::vector<uint64_t> postorder_result; // vector to store the preorder
// traversal of the tree.
public:
std::vector<uint64_t> inorder_result; // vector to store the inorder traversal of the tree.
std::vector<uint64_t> preorder_result; // vector to store the preorder traversal of the tree.
std::vector<uint64_t> postorder_result; // vector to store the preorder traversal of the tree.
Node *createNewNode(
uint64_t); // function that will create new node for insertion.
Node *createNewNode(uint64_t); // function that will create new node for insertion.
std::vector<uint64_t> inorder(
Node *); // function that takes root of the tree as an argument and
// returns its inorder traversal.
std::vector<uint64_t> preorder(
Node *); // function that takes root of the tree as an argument and
// returns its preorder traversal.
std::vector<uint64_t> postorder(
Node *); // function that takes root of the tree as an argument and
// returns its postorder traversal.
};
std::vector<uint64_t> inorder(Node *); // function that takes root of the tree as an argument and returns its inorder traversal.
std::vector<uint64_t> preorder(Node *); // function that takes root of the tree as an argument and returns its preorder traversal.
std::vector<uint64_t> postorder(Node *); // function that takes root of the tree as an argument and returns its postorder traversal.
};
/******************************************************************************
* @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.
* @return pointer to the newly created node with assigned data.
*******************************************************************************/
Node *BT::createNewNode(uint64_t data) {
Node *node = new Node();
node->data = data;
node->left = node->right = nullptr;
return 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.
* @return pointer to the newly created node with assigned data.
*******************************************************************************/
Node *BT::createNewNode(uint64_t data) {
Node *node = new Node();
node->data = data;
node->left = node->right = nullptr;
return node;
}
/******************************************************************************
* @brief inorder() function that will perform the inorder traversal
* recursively, and return the resultant vector that contain the inorder
*traversal of a tree.
* @param root head/root node of a tree
* @return result that is containing the inorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::inorder(Node *root) {
if (root == nullptr) { // return if the current node is empty
return {};
}
/******************************************************************************
* @brief inorder() function that will perform the inorder traversal
* recursively, and return the resultant vector that contain the inorder traversal
* of a tree.
* @param root head/root node of a tree
* @return result that is containing the inorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::inorder(Node *root) {
inorder(root->left); // Traverse the left subtree
BT::inorder_result.push_back(
root->data); // Display the data part of the root (or current node)
inorder(root->right); // Traverse the right subtree
if (root == nullptr) { // return if the current node is empty
return {};
}
return inorder_result;
}
inorder(root -> left); // Traverse the left subtree
BT::inorder_result.push_back(root -> data); // Display the data part of the root (or current node)
inorder(root -> right); // Traverse the right subtree
/******************************************************************************
* @brief preorder function that will perform the preorder traversal
* recursively, and return the resultant vector that contain the preorder
*traversal of a tree.
* @param root head/root node of a tree
* @return result that is containing the preorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::preorder(Node *root) {
if (root == nullptr) { // if the current node is empty
return {};
}
return inorder_result;
}
BT::preorder_result.push_back(
root->data); // Display the data part of the root (or current node)
preorder(root->left); // Traverse the left subtree
preorder(root->right); // Traverse the right subtree
/******************************************************************************
* @brief preorder function that will perform the preorder traversal
* recursively, and return the resultant vector that contain the preorder traversal
* of a tree.
* @param root head/root node of a tree
* @return result that is containing the preorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::preorder(Node* root) {
return preorder_result;
}
if (root == nullptr) { // if the current node is empty
return {};
}
/******************************************************************************
* @brief postorder function that will perform the postorder traversal
* recursively, and return the result vector that contain the postorder
*traversal of a tree.
* @param root head/root node of a tree
* @return result that is containing the postorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::postorder(Node *root) {
if (root == nullptr) { // if the current node is empty
return {};
}
BT::preorder_result.push_back(root -> data); // Display the data part of the root (or current node)
preorder(root -> left); // Traverse the left subtree
preorder(root -> right); // Traverse the right subtree
postorder(root->left); // Traverse the left subtree
postorder(root->right); // Traverse the right subtree
BT::postorder_result.push_back(
root->data); // Display the data part of the root (or current node)
return preorder_result;
}
return postorder_result;
}
/******************************************************************************
* @brief postorder function that will perform the postorder traversal
* recursively, and return the result vector that contain the postorder traversal
* of a tree.
* @param root head/root node of a tree
* @return result that is containing the postorder traversal of a tree
*******************************************************************************/
std::vector<uint64_t> BT::postorder(Node* root) {
if (root == nullptr) { // if the current node is empty
return {};
}
postorder(root -> left); // Traverse the left subtree
postorder(root -> right); // Traverse the right subtree
BT::postorder_result.push_back(root -> data); // Display the data part of the root (or current node)
return postorder_result;
}
} // namespace recursive_tree_traversals
} // namespace recursive_tree_traversals
} // namespace others
@@ -179,24 +188,27 @@ namespace others {
* @brief 1st test-case
* @returns void
*******************************************************************************/
void test1(){
void test1() {
others::recursive_tree_traversals::BT obj1;
others::recursive_tree_traversals::Node* root = obj1.createNewNode(2);
root -> left = obj1.createNewNode(7);
root -> right = obj1.createNewNode(5);
root -> left -> left = obj1.createNewNode(2);
root -> left -> right = obj1.createNewNode(6);
root -> right -> right = obj1.createNewNode(9);
root -> left -> right -> left = obj1.createNewNode(5);
root -> left -> right -> right = obj1.createNewNode(11);
root -> right -> right -> left = obj1.createNewNode(4);
others::recursive_tree_traversals::Node *root = obj1.createNewNode(2);
root->left = obj1.createNewNode(7);
root->right = obj1.createNewNode(5);
root->left->left = obj1.createNewNode(2);
root->left->right = obj1.createNewNode(6);
root->right->right = obj1.createNewNode(9);
root->left->right->left = obj1.createNewNode(5);
root->left->right->right = obj1.createNewNode(11);
root->right->right->left = obj1.createNewNode(4);
std::vector<uint64_t> actual_result_inorder{2, 7, 5, 6, 11, 2, 5, 4, 9};
std::vector<uint64_t> actual_result_preorder{2, 7, 2, 6, 5, 11, 5, 9, 4};
std::vector<uint64_t> actual_result_postorder{2, 5, 11, 6, 7, 4, 9, 5, 2};
std::vector<uint64_t> result_inorder; ///< result stores the inorder traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder traversal of the binary tree
std::vector<uint64_t> result_inorder; ///< result stores the inorder
///< traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder
///< traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder
///< traversal of the binary tree
uint64_t size = actual_result_inorder.size();
@@ -234,23 +246,26 @@ void test1(){
* @brief 2nd test-case
* @returns void
*******************************************************************************/
void test2(){
void test2() {
others::recursive_tree_traversals::BT obj2;
others::recursive_tree_traversals::Node* root = obj2.createNewNode(1);
root -> left = obj2.createNewNode(2);
root -> right = obj2.createNewNode(3);
root -> left -> left = obj2.createNewNode(4);
root -> right -> left = obj2.createNewNode(5);
root -> right -> right = obj2.createNewNode(6);
root -> right -> left -> left = obj2.createNewNode(7);
root -> right -> left -> right = obj2.createNewNode(8);
others::recursive_tree_traversals::Node *root = obj2.createNewNode(1);
root->left = obj2.createNewNode(2);
root->right = obj2.createNewNode(3);
root->left->left = obj2.createNewNode(4);
root->right->left = obj2.createNewNode(5);
root->right->right = obj2.createNewNode(6);
root->right->left->left = obj2.createNewNode(7);
root->right->left->right = obj2.createNewNode(8);
std::vector<uint64_t> actual_result_inorder{4, 2, 1, 7, 5, 8, 3, 6};
std::vector<uint64_t> actual_result_preorder{1, 2, 4, 3, 5, 7, 8, 6};
std::vector<uint64_t> actual_result_postorder{4, 2, 7, 8, 5, 6, 3, 1};
std::vector<uint64_t> result_inorder; ///< result stores the inorder traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder traversal of the binary tree
std::vector<uint64_t> result_inorder; ///< result stores the inorder
///< traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder
///< traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder
///< traversal of the binary tree
uint64_t size = actual_result_inorder.size();
@@ -288,20 +303,23 @@ void test2(){
* @brief 3rd test-case
* @returns void
*******************************************************************************/
void test3(){
void test3() {
others::recursive_tree_traversals::BT obj3;
others::recursive_tree_traversals::Node* root = obj3.createNewNode(1);
root -> left = obj3.createNewNode(2);
root -> right = obj3.createNewNode(3);
root -> left -> left = obj3.createNewNode(4);
root -> left -> right = obj3.createNewNode(5);
others::recursive_tree_traversals::Node *root = obj3.createNewNode(1);
root->left = obj3.createNewNode(2);
root->right = obj3.createNewNode(3);
root->left->left = obj3.createNewNode(4);
root->left->right = obj3.createNewNode(5);
std::vector<uint64_t> actual_result_inorder{4, 2, 5, 1, 3};
std::vector<uint64_t> actual_result_preorder{1, 2, 4, 5, 3};
std::vector<uint64_t> actual_result_postorder{4, 5, 2, 3, 1};
std::vector<uint64_t> result_inorder; ///< result stores the inorder traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder traversal of the binary tree
std::vector<uint64_t> result_inorder; ///< result stores the inorder
///< traversal of the binary tree
std::vector<uint64_t> result_preorder; ///< result stores the preorder
///< traversal of the binary tree
std::vector<uint64_t> result_postorder; ///< result stores the postorder
///< traversal of the binary tree
uint64_t size = actual_result_inorder.size();
@@ -353,8 +371,7 @@ static void tests() {
* @brief Main function
* @returns 0 on exit
*******************************************************************************/
int main()
{
int main() {
tests();
return 0;
}