-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathreverse_binary_tree.cpp
273 lines (260 loc) · 8.78 KB
/
reverse_binary_tree.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/**
* @file
* @brief Implementation for the [Reversing a Binary
* 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
* a recursive reversal of a binary tree.
* @author [Alvin](https://github.com/polarvoid)
*/
#include <cassert> /// For assert
#include <iostream> /// For IO operations
#include <queue> /// For std::queue
#include <vector> /// For std::vector
/**
* @namespace operations_on_datastructures
* @brief Operations on Data Structures
*/
namespace operations_on_datastructures {
/**
* @namespace reverse_binary_tree
* @brief Functions for the [Reverse a Binary
* Tree](https://www.geeksforgeeks.org/reverse-tree-path/) implementation
*/
namespace reverse_binary_tree {
/**
* @brief A Node struct that represents a single node in a Binary Tree
*/
struct Node {
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(int64_t _data) {
data = _data; ///< Set value of Node data
left = nullptr; ///< Initialize left child to NULL
right = nullptr; ///< 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(int64_t data, Node* pivot) {
if (pivot == nullptr) {
return new Node(data); ///< Create new node
}
if (data <= pivot->data) {
pivot->left =
insert(data, pivot->left); ///< Insert Node to the left
} else {
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 == nullptr) {
return pivot; ///< Base case
}
Node* temp = pivot->left; ///< pointer to the left subtree
pivot->left = reverseBinaryTree(pivot->right); ///< Swap
pivot->right = reverseBinaryTree(temp); ///< Swap
return pivot;
}
BinaryTree(const BinaryTree&) = delete;
BinaryTree& operator=(const BinaryTree&) = delete;
public:
/**
* @brief Creates a BinaryTree with a root pointing to NULL.
*/
BinaryTree() { root = nullptr; }
/**
* @brief Creates a BinaryTree with a root with an initial value.
*/
explicit BinaryTree(int64_t data) { root = new Node(data); }
~BinaryTree() {
std::vector<Node*> nodes;
nodes.emplace_back(root);
while (!nodes.empty()) {
const auto cur_node = nodes.back();
nodes.pop_back();
if (cur_node) {
nodes.emplace_back(cur_node->left);
nodes.emplace_back(cur_node->right);
delete cur_node;
}
}
}
/**
* @brief Adds a new Node to the Binary Tree
*/
void add(int64_t 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
* 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<int64_t> of nodes of the tree.
*/
std::vector<int64_t> get_level_order() {
std::vector<int64_t> data; ///< Result vector of int
if (root == nullptr) {
return data; ///< Return empty vector if root is Invalid
}
std::queue<Node*> nodes; ///< Queue of the nodes in the tree
nodes.push(root); ///< Insert root into the queue
while (!nodes.empty()) {
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 != nullptr) {
nodes.push(temp->left); ///< Insert left node
}
if (temp->right != nullptr) {
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.
* @returns void
*/
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
/**
* @namespace tests
* @brief Testcases to check Reversal of Binary Tree.
*/
namespace tests {
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<int64_t> 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;
std::vector<int64_t> 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<int64_t> pre_reversal, post_reversal;
std::vector<int64_t> pre_res = {4, 3, 6, 2, 5, 7, 1};
std::vector<int64_t> 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(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(); ///< Single element test
tests::test2(); ///< No element test
tests::test3(); ///< Correct reversal test
}
/**
* @brief main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}