From c760166965dab71e12ef51ea8bdc5fa72b1d317c Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 05:31:00 +0530 Subject: [PATCH 01/16] Create reverse_binary_tree.cpp --- .../reverse_binary_tree.cpp | 110 ++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 operations_on_datastructures/reverse_binary_tree.cpp diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp new file mode 100644 index 00000000000..f22acb3714d --- /dev/null +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -0,0 +1,110 @@ +/** + * @file + * @brief An implemention for Reversing a Binary Tree recursively. + * + */ + +#include /// For assert +#include /// For IO operations +#include /// For std::queue +#include /// For std::vector + +/** + * @namespace operations_on_datastructures + * @brief Operations on Data Structures + */ +namespace operations_on_datastructures { + +/** + * @namespace reverse_binary_tree + * @brief Functions for Creating and Reversing a Binary Tree + */ +namespace reverse_binary_tree { + +/** + * @brief A Node struct that represents a single node in a Binary Tree + */ +struct Node { + int data; ///< The value of the Node + Node* left; ///< The Node's left child + Node* right; ///< The Node's right child + /** + * @brief Creates a new Node with some initial data + */ + Node(int _data) { + data = _data; ///< Set value of Node data + left = NULL; ///< Initialize left child to NULL + right = NULL; ///< Initialize right child to NULL + } +}; + +/** + * @brief A Binary Tree class that implements a Binary Search Tree + *(BST) by default. + */ +class BinaryTree { + private: + Node* root; ///< Pointer to root node of Binary Tree + /** + * @brief inserts a node in the Binary Tree, with the behaviouur of + * a Binary Search Tree. + * @details Nodes with smaller values are inserted in the left + * subtree, and Nodes with larger values are inserted into the + * right subtree recursively. Time Complexity: O(log(n)) + * @param data The data/value of the Node to be inserted + * @param pivot A pointer to the root node of the (sub)tree + * @returns Node pointer to the root + */ + Node* insert(int data, Node* pivot) { + if (pivot == NULL) { + return new Node(data); + } + if (data <= pivot->data) { + pivot->left = insert(data, pivot->left); + } else { + pivot->right = insert(data, pivot->right); + } + return pivot; + } + Node* reverseBinaryTree(Node* pivot) { + if (pivot == NULL) { + return pivot; + } + Node* temp = pivot->left; + pivot->left = reverseBinaryTree(pivot->right); + pivot->right = reverseBinaryTree(temp); + return pivot; + } + + public: + BinaryTree() { root = NULL; } + BinaryTree(Node* _root) { root = _root; } + BinaryTree(int data) { root = new Node(data); } + void add(int data) { root = insert(data, root); } + void reverse() { root = reverseBinaryTree(root); } + std::vector get_level_order() { + std::vector data; + if (root == NULL) { + return data; + } + std::queue nodes; + nodes.push(root); + while (!nodes.empty()) { + Node* temp = nodes.back(); + data.push_back(temp); + nodes.pop(); + if (temp->left != NULL) { + nodes.push(temp->left); + } + if (temp->right != NULL) { + nodes.push(temp->right); + } + } + return data; + } +}; + +} // namespace reverse_binary_tree +} // namespace operations_on_datastructures + +int main() {} \ No newline at end of file From cbad18381e5b3b70b35f0d6bf1134e137bfd0251 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 05:46:56 +0530 Subject: [PATCH 02/16] Added documentation Added Documentation for the level_order_traversal() function, and implemented a print() function to display the tree to STDOUT --- .../reverse_binary_tree.cpp | 41 ++++++++++++++----- 1 file changed, 31 insertions(+), 10 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index f22acb3714d..3bb78389a9f 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -82,29 +82,50 @@ class BinaryTree { BinaryTree(int data) { root = new Node(data); } void add(int data) { root = insert(data, root); } void reverse() { root = reverseBinaryTree(root); } + /** + * @brief Level order traversal of a tree consists of visiting its + * elements, top to bottom, left to right. This function performs + * level order traversal and returns the node datas as a vector. + * @details The function uses a queue to append and remove elements + * as they are visited, and then adds their children, if any. This + * ensures that the elements are visited layer-by-layer, starting + * from the root of the Tree. + * @returns vector of nodes of the tree. + */ std::vector get_level_order() { - std::vector data; + std::vector data; ///< Result vector of int if (root == NULL) { - return data; + return data; ///< Return empty vector if root is Invalid } - std::queue nodes; - nodes.push(root); + std::queue nodes; ///< Queue of the nodes in the tree + nodes.push(root); ///< Insert root into the queue while (!nodes.empty()) { - Node* temp = nodes.back(); - data.push_back(temp); - nodes.pop(); + Node* temp = nodes.back(); ///< Copy the first element + data.push_back(temp->data); ///< Add the element to the data + nodes.pop(); ///< Remove element if (temp->left != NULL) { - nodes.push(temp->left); + nodes.push(temp->left); ///< Insert left node } if (temp->right != NULL) { - nodes.push(temp->right); + nodes.push(temp->right); ///< Insert right node } - } + } /// Add nodes while Tree is not empty return data; } + /** + * @brief Prints all of the elements in the tree to stdout + * level-by-level, using the get_level_order() function. + */ + void print() { + for (int i : get_level_order()) { + std::cout << i << " "; /// Print each element in the tree + } + std::cout << "\n"; /// Print newline + } }; } // namespace reverse_binary_tree } // namespace operations_on_datastructures +static void tests() {} int main() {} \ No newline at end of file From 54143f2450ebe35f448112cf88f29e60ad5e7b3a Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 06:05:40 +0530 Subject: [PATCH 03/16] Added documentation --- .../reverse_binary_tree.cpp | 66 ++++++++++++++++--- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 3bb78389a9f..a2c3d4b25f9 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -57,30 +57,49 @@ class BinaryTree { */ Node* insert(int data, Node* pivot) { if (pivot == NULL) { - return new Node(data); + return new Node(data); ///< Create new node } if (data <= pivot->data) { - pivot->left = insert(data, pivot->left); + pivot->left = + insert(data, pivot->left); ///< Insert Node to the left } else { - pivot->right = insert(data, pivot->right); + pivot->right = + insert(data, pivot->right); ///< Insert node to the right } return pivot; } + /** + * @brief Reverses a Binary Tree recursively by swapping the left and + * right subtrees and their children. + * @param pivot A reference to the root of the (sub)tree + * @returns Node pointer to root node + */ Node* reverseBinaryTree(Node* pivot) { if (pivot == NULL) { - return pivot; + return pivot; ///< Base case } - Node* temp = pivot->left; - pivot->left = reverseBinaryTree(pivot->right); - pivot->right = reverseBinaryTree(temp); + Node* temp = pivot->left; ///< pointer to the left subtree + pivot->left = reverseBinaryTree(pivot->right); ///< Swap + pivot->right = reverseBinaryTree(temp); ///< Swap return pivot; } public: + /** + * @brief Creates a BinaryTree with a root pointing to NULL. + */ BinaryTree() { root = NULL; } - BinaryTree(Node* _root) { root = _root; } + /** + * @brief Creates a BinaryTree with a root with an initial value. + */ BinaryTree(int data) { root = new Node(data); } + /** + * @brief Adds a new Node to the Binary Tree + */ void add(int data) { root = insert(data, root); } + /** + * Reverses the Binary Tree + */ void reverse() { root = reverseBinaryTree(root); } /** * @brief Level order traversal of a tree consists of visiting its @@ -115,6 +134,7 @@ class BinaryTree { /** * @brief Prints all of the elements in the tree to stdout * level-by-level, using the get_level_order() function. + * @returns void */ void print() { for (int i : get_level_order()) { @@ -126,6 +146,32 @@ class BinaryTree { } // namespace reverse_binary_tree } // namespace operations_on_datastructures -static void tests() {} -int main() {} \ No newline at end of file +/** + * @namespace tests + */ +namespace tests { +using operations_on_datastructures::reverse_binary_tree; +void test1() { + BinaryTree bst; + bst.add(5); + bst.print(); +} +void test2() { + BinaryTree bst; + bst.add(7); + bst.add(5); + bst.add(9); + bst.print(); +} +} // namespace tests + +static void tests() { + tests::test1(); + tests::test2(); +} + +int main() { + tests(); + return 0; +} \ No newline at end of file From 41af1e4d2142e5ec178ffafc8cb2bd8fdc5543d8 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 06:24:52 +0530 Subject: [PATCH 04/16] Renamed tests to test --- operations_on_datastructures/reverse_binary_tree.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index a2c3d4b25f9..475f9afa975 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -166,12 +166,12 @@ void test2() { } } // namespace tests -static void tests() { +static void test() { tests::test1(); tests::test2(); } int main() { - tests(); + test(); return 0; } \ No newline at end of file From fb86292d80f8a7cde7b9ac0440e59d1ac62db530 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 06:32:31 +0530 Subject: [PATCH 05/16] Fixed issue with incorrect using statement --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 475f9afa975..0d774c47bc0 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -151,7 +151,7 @@ class BinaryTree { * @namespace tests */ namespace tests { -using operations_on_datastructures::reverse_binary_tree; +using operations_on_datastructures::reverse_binary_tree::BinaryTree; void test1() { BinaryTree bst; bst.add(5); From 6c5794a3d63792c61fec525c68eba2965c59248e Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 19 Oct 2021 01:03:14 +0000 Subject: [PATCH 06/16] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 3f55016aad7..cc5247d6f11 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -241,6 +241,7 @@ * [Inorder Successor Of Bst](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/inorder_successor_of_bst.cpp) * [Intersection Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/intersection_of_2_arrays.cpp) * [Reverse A Linked List Using Recusion](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_a_linked_list_using_recusion.cpp) + * [Reverse Binary Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/reverse_binary_tree.cpp) * [Selectionsortlinkedlist](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/selectionsortlinkedlist.cpp) * [Trie Multiple Search](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/trie_multiple_search.cpp) * [Union Of 2 Arrays](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/operations_on_datastructures/union_of_2_arrays.cpp) From e9cd4fd10b1c826afa49dce7e5e18e91b51c1183 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Tue, 19 Oct 2021 01:03:23 +0000 Subject: [PATCH 07/16] clang-format and clang-tidy fixes for fb86292d --- .../reverse_binary_tree.cpp | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 0d774c47bc0..dbaa761b0f1 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -31,10 +31,10 @@ struct Node { /** * @brief Creates a new Node with some initial data */ - Node(int _data) { - data = _data; ///< Set value of Node data - left = NULL; ///< Initialize left child to NULL - right = NULL; ///< Initialize right child to NULL + explicit Node(int _data) { + data = _data; ///< Set value of Node data + left = nullptr; ///< Initialize left child to NULL + right = nullptr; ///< Initialize right child to NULL } }; @@ -56,7 +56,7 @@ class BinaryTree { * @returns Node pointer to the root */ Node* insert(int data, Node* pivot) { - if (pivot == NULL) { + if (pivot == nullptr) { return new Node(data); ///< Create new node } if (data <= pivot->data) { @@ -75,7 +75,7 @@ class BinaryTree { * @returns Node pointer to root node */ Node* reverseBinaryTree(Node* pivot) { - if (pivot == NULL) { + if (pivot == nullptr) { return pivot; ///< Base case } Node* temp = pivot->left; ///< pointer to the left subtree @@ -88,11 +88,11 @@ class BinaryTree { /** * @brief Creates a BinaryTree with a root pointing to NULL. */ - BinaryTree() { root = NULL; } + BinaryTree() { root = nullptr; } /** * @brief Creates a BinaryTree with a root with an initial value. */ - BinaryTree(int data) { root = new Node(data); } + explicit BinaryTree(int data) { root = new Node(data); } /** * @brief Adds a new Node to the Binary Tree */ @@ -113,7 +113,7 @@ class BinaryTree { */ std::vector get_level_order() { std::vector data; ///< Result vector of int - if (root == NULL) { + if (root == nullptr) { return data; ///< Return empty vector if root is Invalid } std::queue nodes; ///< Queue of the nodes in the tree @@ -122,10 +122,10 @@ class BinaryTree { Node* temp = nodes.back(); ///< Copy the first element data.push_back(temp->data); ///< Add the element to the data nodes.pop(); ///< Remove element - if (temp->left != NULL) { + if (temp->left != nullptr) { nodes.push(temp->left); ///< Insert left node } - if (temp->right != NULL) { + if (temp->right != nullptr) { nodes.push(temp->right); ///< Insert right node } } /// Add nodes while Tree is not empty From b222cb264adc238fb053f653e5290f08e99b2493 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:21:12 +0530 Subject: [PATCH 08/16] Added Test cases --- .../reverse_binary_tree.cpp | 94 +++++++++++++++++-- 1 file changed, 85 insertions(+), 9 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 0d774c47bc0..a69d1f033cf 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -1,7 +1,12 @@ /** * @file - * @brief An implemention for Reversing a Binary Tree recursively. - * + * @brief An implemention for [Reversing a Binary + * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively. + * @details A binary tree can be reversed by swapping the left and + * right child of a node at each node, starting from the root, and + * cascading below. This solution aims to provide an implementation of + * a recursive reversal of a binary tree. + * @author [Alvin](https://github.com/polarvoid) */ #include /// For assert @@ -119,7 +124,7 @@ class BinaryTree { std::queue nodes; ///< Queue of the nodes in the tree nodes.push(root); ///< Insert root into the queue while (!nodes.empty()) { - Node* temp = nodes.back(); ///< Copy the first element + Node* temp = nodes.front(); ///< Copy the first element data.push_back(temp->data); ///< Add the element to the data nodes.pop(); ///< Remove element if (temp->left != NULL) { @@ -149,29 +154,100 @@ class BinaryTree { /** * @namespace tests + * @brief Testcases to check Reversal of Binary Tree. */ namespace tests { -using operations_on_datastructures::reverse_binary_tree::BinaryTree; +using operations_on_datastructures::reverse_binary_tree:: + BinaryTree; ///< Use the BinaryTree +/** + * @brief A Test to check an edge case (single element reversal) + */ void test1() { BinaryTree bst; + std::vector pre_reversal, post_reversal; + std::cout << "TEST CASE 1\n"; + std::cout << "Initializing tree with a single element (5)\n"; bst.add(5); + pre_reversal = bst.get_level_order(); + std::cout << "Before reversal: "; + bst.print(); + std::cout << "After reversal: "; + bst.reverse(); + post_reversal = bst.get_level_order(); + assert(pre_reversal.size() == + post_reversal.size()); ///< Check for equal sizes + assert(pre_reversal.size() == + 1); ///< Ensure that there is only one element + assert(pre_reversal[0] == + post_reversal[0]); ///< Check if both elements are same bst.print(); + std::cout << "TEST PASSED!\n\n"; } +/** + * @brief A Test to check an edge case (NULL root element) + */ void test2() { BinaryTree bst; - bst.add(7); + std::vector pre_reversal, post_reversal; + std::cout << "TEST CASE 2\n"; + std::cout << "Creating empty tree (root points to NULL)\n"; + pre_reversal = bst.get_level_order(); + std::cout << "Before reversal: "; + bst.print(); + std::cout << "After reversal: "; + bst.reverse(); + post_reversal = bst.get_level_order(); + assert(pre_reversal.size() == + post_reversal.size()); ///< Check for equal sizes + assert(pre_reversal.size() == + 0); ///< Ensure that there is only one element + bst.print(); + std::cout << "TEST PASSED!\n\n"; +} +/** + * @brief A Test to check correct reversal of a Binary Tree + */ +void test3() { + BinaryTree bst; + std::vector pre_reversal, post_reversal; + std::vector pre_res = {4, 3, 6, 2, 5, 7, 1}; + std::vector post_res = {4, 6, 3, 7, 5, 2, 1}; + std::cout << "TEST CASE 3\n"; + std::cout << "Creating tree with elements (4, 6, 3, 2, 5, 7, 1)\n"; + bst.add(4); + bst.add(6); + bst.add(3); + bst.add(2); bst.add(5); - bst.add(9); + bst.add(7); + bst.add(1); + pre_reversal = bst.get_level_order(); + assert(pre_reversal == pre_res); ///< Check for equality + std::cout << "Before reversal: "; + bst.print(); + std::cout << "After reversal: "; + bst.reverse(); + post_reversal = bst.get_level_order(); + assert(post_reversal == post_res); ///< Check for equality bst.print(); + std::cout << "TEST PASSED!\n\n"; } } // namespace tests +/** + * @brief Function to test the correctness of the Tree Reversal + */ static void test() { - tests::test1(); - tests::test2(); + tests::test1(); ///< Single element test + tests::test2(); ///< No element test + tests::test3(); ///< Correct reversal test } +/** + * @brief main function + * @returns 0 on exit + */ int main() { - test(); + test(); ///< Run our Test cases return 0; } \ No newline at end of file From 0421f55ab0c4e00d954239d2b1bc20ad672dca87 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:50:37 +0530 Subject: [PATCH 09/16] Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index a0fc22a4caf..658ac51ec3e 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -1,6 +1,6 @@ /** * @file - * @brief An implemention for [Reversing a Binary + * @brief Implementation for the [Reversing a Binary * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively. * @details A binary tree can be reversed by swapping the left and * right child of a node at each node, starting from the root, and From 657f759cf85896282926df224224e50ba95d71a5 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:51:09 +0530 Subject: [PATCH 10/16] Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 658ac51ec3e..ce9dd8a72dd 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -1,7 +1,7 @@ /** * @file * @brief Implementation for the [Reversing a Binary - * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively. + * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively algorithm. * @details A binary tree can be reversed by swapping the left and * right child of a node at each node, starting from the root, and * cascading below. This solution aims to provide an implementation of From aabd5a200a1b2f130a13f8b10625d0acc0f8daf8 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:51:16 +0530 Subject: [PATCH 11/16] Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index ce9dd8a72dd..75512f08396 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -22,7 +22,7 @@ namespace operations_on_datastructures { /** * @namespace reverse_binary_tree - * @brief Functions for Creating and Reversing a Binary Tree + * @brief Functions for the [Reverse a Binary Tree](https://www.geeksforgeeks.org/reverse-tree-path/) implementation */ namespace reverse_binary_tree { From ff091f7b45dd4b108d8b9c21a9dc2979cdeb0364 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:51:21 +0530 Subject: [PATCH 12/16] Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 75512f08396..38e27fd5e44 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -250,4 +250,4 @@ static void test() { int main() { test(); ///< Run our Test cases return 0; -} \ No newline at end of file +} From 76255dd780efbc738214205044c1239907e99745 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:53:45 +0530 Subject: [PATCH 13/16] Update operations_on_datastructures/reverse_binary_tree.cpp Co-authored-by: David Leal --- operations_on_datastructures/reverse_binary_tree.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 38e27fd5e44..e91dac58880 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -248,6 +248,6 @@ static void test() { * @returns 0 on exit */ int main() { - test(); ///< Run our Test cases + test(); // run self-test implementations return 0; } From 14a26c3f2e9ab2555541be6d85efd433fe164b53 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 07:58:19 +0530 Subject: [PATCH 14/16] Changed int to int64_t --- .../reverse_binary_tree.cpp | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index e91dac58880..40cea104b05 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -1,7 +1,12 @@ /** * @file +<<<<<<< Updated upstream * @brief Implementation for the [Reversing a Binary * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively algorithm. +======= + * @brief Implemention for [Reversing a Binary + * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively. +>>>>>>> Stashed changes * @details A binary tree can be reversed by swapping the left and * right child of a node at each node, starting from the root, and * cascading below. This solution aims to provide an implementation of @@ -30,13 +35,13 @@ namespace reverse_binary_tree { * @brief A Node struct that represents a single node in a Binary Tree */ struct Node { - int data; ///< The value of the Node - Node* left; ///< The Node's left child - Node* right; ///< The Node's right child + int64_t data; ///< The value of the Node + Node* left; ///< The Node's left child + Node* right; ///< The Node's right child /** * @brief Creates a new Node with some initial data */ - explicit Node(int _data) { + explicit Node(int64_t _data) { data = _data; ///< Set value of Node data left = nullptr; ///< Initialize left child to NULL right = nullptr; ///< Initialize right child to NULL @@ -60,7 +65,7 @@ class BinaryTree { * @param pivot A pointer to the root node of the (sub)tree * @returns Node pointer to the root */ - Node* insert(int data, Node* pivot) { + Node* insert(int64_t data, Node* pivot) { if (pivot == nullptr) { return new Node(data); ///< Create new node } @@ -97,11 +102,11 @@ class BinaryTree { /** * @brief Creates a BinaryTree with a root with an initial value. */ - explicit BinaryTree(int data) { root = new Node(data); } + explicit BinaryTree(int64_t data) { root = new Node(data); } /** * @brief Adds a new Node to the Binary Tree */ - void add(int data) { root = insert(data, root); } + void add(int64_t data) { root = insert(data, root); } /** * Reverses the Binary Tree */ @@ -114,10 +119,10 @@ class BinaryTree { * as they are visited, and then adds their children, if any. This * ensures that the elements are visited layer-by-layer, starting * from the root of the Tree. - * @returns vector of nodes of the tree. + * @returns vector of nodes of the tree. */ - std::vector get_level_order() { - std::vector data; ///< Result vector of int + std::vector get_level_order() { + std::vector data; ///< Result vector of int if (root == nullptr) { return data; ///< Return empty vector if root is Invalid } @@ -164,7 +169,7 @@ using operations_on_datastructures::reverse_binary_tree:: */ void test1() { BinaryTree bst; - std::vector pre_reversal, post_reversal; + std::vector pre_reversal, post_reversal; std::cout << "TEST CASE 1\n"; std::cout << "Initializing tree with a single element (5)\n"; bst.add(5); @@ -188,7 +193,7 @@ void test1() { */ void test2() { BinaryTree bst; - std::vector pre_reversal, post_reversal; + std::vector pre_reversal, post_reversal; std::cout << "TEST CASE 2\n"; std::cout << "Creating empty tree (root points to NULL)\n"; pre_reversal = bst.get_level_order(); @@ -209,9 +214,9 @@ void test2() { */ void test3() { BinaryTree bst; - std::vector pre_reversal, post_reversal; - std::vector pre_res = {4, 3, 6, 2, 5, 7, 1}; - std::vector post_res = {4, 6, 3, 7, 5, 2, 1}; + std::vector pre_reversal, post_reversal; + std::vector pre_res = {4, 3, 6, 2, 5, 7, 1}; + std::vector post_res = {4, 6, 3, 7, 5, 2, 1}; std::cout << "TEST CASE 3\n"; std::cout << "Creating tree with elements (4, 6, 3, 2, 5, 7, 1)\n"; bst.add(4); From 39bb9b9a0df9c8405139f4b62f3f9b224a56f9f2 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Tue, 19 Oct 2021 15:30:51 +0530 Subject: [PATCH 15/16] Updated documentation wording --- operations_on_datastructures/reverse_binary_tree.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/operations_on_datastructures/reverse_binary_tree.cpp b/operations_on_datastructures/reverse_binary_tree.cpp index 40cea104b05..46b742efcb0 100644 --- a/operations_on_datastructures/reverse_binary_tree.cpp +++ b/operations_on_datastructures/reverse_binary_tree.cpp @@ -1,12 +1,8 @@ /** * @file -<<<<<<< Updated upstream * @brief Implementation for the [Reversing a Binary - * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively algorithm. -======= - * @brief Implemention for [Reversing a Binary - * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively. ->>>>>>> Stashed changes + * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) recursively + * algorithm. * @details A binary tree can be reversed by swapping the left and * right child of a node at each node, starting from the root, and * cascading below. This solution aims to provide an implementation of @@ -27,7 +23,8 @@ namespace operations_on_datastructures { /** * @namespace reverse_binary_tree - * @brief Functions for the [Reverse a Binary Tree](https://www.geeksforgeeks.org/reverse-tree-path/) implementation + * @brief Functions for the [Reverse a Binary + * Tree](https://www.geeksforgeeks.org/reverse-tree-path/) implementation */ namespace reverse_binary_tree { From b2fb6c450de0ddc129ae301e4033fe5271b3bfc3 Mon Sep 17 00:00:00 2001 From: Alvin Philips Date: Mon, 25 Oct 2021 05:03:21 +0530 Subject: [PATCH 16/16] Added reference to intersection of two arrays --- operations_on_datastructures/union_of_two_arrays.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/operations_on_datastructures/union_of_two_arrays.cpp b/operations_on_datastructures/union_of_two_arrays.cpp index d2a6214405d..8fd75bde740 100644 --- a/operations_on_datastructures/union_of_two_arrays.cpp +++ b/operations_on_datastructures/union_of_two_arrays.cpp @@ -7,6 +7,7 @@ * in the first array, combined with all of the unique elements of a second * array. This implementation uses ordered arrays, and an algorithm to correctly * order them and return the result as a new array (vector). + * @see intersection_of_two_arrays.cpp * @author [Alvin](https://github.com/polarvoid) */