41 explicit Node(int64_t _data) {
66 if (pivot ==
nullptr) {
85 if (pivot ==
nullptr) {
108 std::vector<Node*> nodes;
109 nodes.emplace_back(
root);
110 while (!nodes.empty()) {
111 const auto cur_node = nodes.back();
114 nodes.emplace_back(cur_node->left);
115 nodes.emplace_back(cur_node->right);
140 std::vector<int64_t>
data;
141 if (
root ==
nullptr) {
144 std::queue<Node*> nodes;
146 while (!nodes.empty()) {
147 Node* temp = nodes.front();
150 if (temp->
left !=
nullptr) {
151 nodes.push(temp->
left);
153 if (temp->
right !=
nullptr) {
154 nodes.push(temp->
right);
166 std::cout << i <<
" ";
180using operations_on_datastructures::reverse_binary_tree::
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";
192 std::cout <<
"Before reversal: ";
194 std::cout <<
"After reversal: ";
197 assert(pre_reversal.size() ==
198 post_reversal.size());
199 assert(pre_reversal.size() ==
201 assert(pre_reversal[0] ==
204 std::cout <<
"TEST PASSED!\n\n";
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";
215 std::cout <<
"Before reversal: ";
217 std::cout <<
"After reversal: ";
220 assert(pre_reversal.size() ==
221 post_reversal.size());
222 assert(pre_reversal.size() ==
225 std::cout <<
"TEST PASSED!\n\n";
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";
245 assert(pre_reversal == pre_res);
246 std::cout <<
"Before reversal: ";
248 std::cout <<
"After reversal: ";
251 assert(post_reversal == post_res);
253 std::cout <<
"TEST PASSED!\n\n";
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...
Node * root
Pointer to root node of Binary Tree.
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.
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.
A Node struct that represents a single node in a Binary Tree.
Node(int64_t _data)
Creates a new Node with some initial data.
Node * right
The Node's right child.
int64_t data
The value of the Node.
Node * left
The Node's left child.