TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
reverse_binary_tree.cpp
Go to the documentation of this file.
1
13#include <cassert>
14#include <iostream>
15#include <queue>
16#include <vector>
17
23
29namespace reverse_binary_tree {
30
34struct Node {
35 int64_t data;
41 explicit Node(int64_t _data) {
42 data = _data;
43 left = nullptr;
44 right = nullptr;
45 }
46};
47
53 private:
65 Node* insert(int64_t data, Node* pivot) {
66 if (pivot == nullptr) {
67 return new Node(data);
68 }
70 pivot->left =
71 insert(data, pivot->left);
72 } else {
73 pivot->right =
74 insert(data, pivot->right);
75 }
76 return pivot;
77 }
85 if (pivot == nullptr) {
86 return pivot;
87 }
88 Node* temp = pivot->left;
89 pivot->left = reverseBinaryTree(pivot->right);
90 pivot->right = reverseBinaryTree(temp);
91 return pivot;
92 }
93
94 BinaryTree(const BinaryTree&) = delete;
95 BinaryTree& operator=(const BinaryTree&) = delete;
96
97 public:
101 BinaryTree() { root = nullptr; }
105 explicit BinaryTree(int64_t data) { root = new Node(data); }
106
107 ~BinaryTree() {
108 std::vector<Node*> nodes;
109 nodes.emplace_back(root);
110 while (!nodes.empty()) {
111 const auto cur_node = nodes.back();
112 nodes.pop_back();
113 if (cur_node) {
114 nodes.emplace_back(cur_node->left);
115 nodes.emplace_back(cur_node->right);
116 delete cur_node;
117 }
118 }
119 }
120
124 void add(int64_t data) { root = insert(data, root); }
139 std::vector<int64_t> get_level_order() {
140 std::vector<int64_t> data;
141 if (root == nullptr) {
142 return data;
143 }
144 std::queue<Node*> nodes;
145 nodes.push(root);
146 while (!nodes.empty()) {
147 Node* temp = nodes.front();
148 data.push_back(temp->data);
149 nodes.pop();
150 if (temp->left != nullptr) {
151 nodes.push(temp->left);
152 }
153 if (temp->right != nullptr) {
154 nodes.push(temp->right);
155 }
156 }
157 return data;
158 }
164 void print() {
165 for (int i : get_level_order()) {
166 std::cout << i << " ";
167 }
168 std::cout << "\n";
169 }
170};
171
172} // namespace reverse_binary_tree
173} // namespace operations_on_datastructures
174
179namespace tests {
180using operations_on_datastructures::reverse_binary_tree::
181 BinaryTree;
185void test1() {
186 BinaryTree bst;
187 std::vector<int64_t> pre_reversal, post_reversal;
188 std::cout << "TEST CASE 1\n";
189 std::cout << "Initializing tree with a single element (5)\n";
190 bst.add(5);
191 pre_reversal = bst.get_level_order();
192 std::cout << "Before reversal: ";
193 bst.print();
194 std::cout << "After reversal: ";
195 bst.reverse();
196 post_reversal = bst.get_level_order();
197 assert(pre_reversal.size() ==
198 post_reversal.size());
199 assert(pre_reversal.size() ==
200 1);
201 assert(pre_reversal[0] ==
202 post_reversal[0]);
203 bst.print();
204 std::cout << "TEST PASSED!\n\n";
205}
209void test2() {
210 BinaryTree bst;
211 std::vector<int64_t> pre_reversal, post_reversal;
212 std::cout << "TEST CASE 2\n";
213 std::cout << "Creating empty tree (root points to NULL)\n";
214 pre_reversal = bst.get_level_order();
215 std::cout << "Before reversal: ";
216 bst.print();
217 std::cout << "After reversal: ";
218 bst.reverse();
219 post_reversal = bst.get_level_order();
220 assert(pre_reversal.size() ==
221 post_reversal.size());
222 assert(pre_reversal.size() ==
223 0);
224 bst.print();
225 std::cout << "TEST PASSED!\n\n";
226}
230void test3() {
231 BinaryTree bst;
232 std::vector<int64_t> pre_reversal, post_reversal;
233 std::vector<int64_t> pre_res = {4, 3, 6, 2, 5, 7, 1};
234 std::vector<int64_t> post_res = {4, 6, 3, 7, 5, 2, 1};
235 std::cout << "TEST CASE 3\n";
236 std::cout << "Creating tree with elements (4, 6, 3, 2, 5, 7, 1)\n";
237 bst.add(4);
238 bst.add(6);
239 bst.add(3);
240 bst.add(2);
241 bst.add(5);
242 bst.add(7);
243 bst.add(1);
244 pre_reversal = bst.get_level_order();
245 assert(pre_reversal == pre_res);
246 std::cout << "Before reversal: ";
247 bst.print();
248 std::cout << "After reversal: ";
249 bst.reverse();
250 post_reversal = bst.get_level_order();
251 assert(post_reversal == post_res);
252 bst.print();
253 std::cout << "TEST PASSED!\n\n";
254}
255} // namespace tests
256
260static void test() {
261 tests::test1();
262 tests::test2();
263 tests::test3();
264}
265
270int main() {
271 test(); // run self-test implementations
272 return 0;
273}
A Binary Tree class that implements a Binary Search Tree (BST) by default.
std::vector< int64_t > get_level_order()
Level order traversal of a tree consists of visiting its elements, top to bottom, left to right....
void add(int64_t data)
Adds a new Node to the Binary Tree.
void print()
Prints all of the elements in the tree to stdout level-by-level, using the get_level_order() function...
BinaryTree(int64_t data)
Creates a BinaryTree with a root with an initial value.
BinaryTree()
Creates a BinaryTree with a root pointing to NULL.
Node * insert(int64_t data, Node *pivot)
inserts a node in the Binary Tree, with the behaviouur of a Binary Search Tree.
Node * reverseBinaryTree(Node *pivot)
Reverses a Binary Tree recursively by swapping the left and right subtrees and their children.
int data[MAX]
test data
Functions for the Reverse a Binary Tree implementation.
Testcases to check Union of Two Arrays.
void test1()
A Test to check an simple case.
void test3()
A Test to check an invalid shift value.
void test2()
A Test to check an empty vector.
static void test()
Function to test the correctness of the Tree Reversal.
int main()
main function
A Node struct that represents a single node in a Binary Tree.
Node(int64_t _data)
Creates a new Node with some initial data.