TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
recursive_tree_traversal.cpp
Go to the documentation of this file.
1
54#include <cassert>
55#include <cstdint>
56#include <iostream>
57#include <vector>
58
63namespace others {
64
71namespace recursive_tree_traversals {
72
79struct Node {
80 std::uint64_t data = 0;
81 struct Node *left{};
82 struct Node *right{};
83};
88class BT {
89 public:
90 std::vector<std::uint64_t>
91 inorder_result; // vector to store the inorder traversal of the tree.
92 std::vector<std::uint64_t>
93 preorder_result; // vector to store the preorder traversal of the tree.
94 std::vector<std::uint64_t>
95 postorder_result; // vector to store the preorder
96 // traversal of the tree.
97
99 std::uint64_t); // function that will create new node for insertion.
100
101 std::vector<std::uint64_t> inorder(
102 Node *); // function that takes root of the tree as an argument and
103 // returns its inorder traversal.
104 std::vector<std::uint64_t> preorder(
105 Node *); // function that takes root of the tree as an argument and
106 // returns its preorder traversal.
107 std::vector<std::uint64_t> postorder(
108 Node *); // function that takes root of the tree as an argument and
109 // returns its postorder traversal.
110};
111
118Node *BT::createNewNode(std::uint64_t data) {
119 Node *node = new Node();
120 node->data = data;
121 node->left = node->right = nullptr;
122 return node;
123}
124
125/*
126 * @brief inorder() function that will perform the inorder traversal
127 * recursively, and return the resultant vector that contain the inorder
128 * traversal of a tree.
129 * @param root head/root node of a tree
130 * @return result that is containing the inorder traversal of a tree
131 **/
132std::vector<std::uint64_t> BT::inorder(Node *root) {
133 if (root == nullptr) { // return if the current node is empty
134 return {};
135 }
136
137 inorder(root->left); // Traverse the left subtree
138 BT::inorder_result.push_back(
139 root->data); // Display the data part of the root (or current node)
140 inorder(root->right); // Traverse the right subtree
141
142 return inorder_result;
143}
144
152std::vector<std::uint64_t> BT::preorder(Node *root) {
153 if (root == nullptr) { // if the current node is empty
154 return {};
155 }
156
157 BT::preorder_result.push_back(
158 root->data); // Display the data part of the root (or current node)
159 preorder(root->left); // Traverse the left subtree
160 preorder(root->right); // Traverse the right subtree
161
162 return preorder_result;
163}
164
172std::vector<std::uint64_t> BT::postorder(Node *root) {
173 if (root == nullptr) { // if the current node is empty
174 return {};
175 }
176
177 postorder(root->left); // Traverse the left subtree
178 postorder(root->right); // Traverse the right subtree
179 BT::postorder_result.push_back(
180 root->data); // Display the data part of the root (or current node)
181
182 return postorder_result;
183}
184
185void deleteAll(const Node *const root) {
186 if (root) {
187 deleteAll(root->left);
188 deleteAll(root->right);
189 delete root;
190 }
191}
192
193} // namespace recursive_tree_traversals
194
195} // namespace others
196
201void test1() {
204 root->left = obj1.createNewNode(7);
205 root->right = obj1.createNewNode(5);
206 root->left->left = obj1.createNewNode(2);
207 root->left->right = obj1.createNewNode(6);
208 root->right->right = obj1.createNewNode(9);
209 root->left->right->left = obj1.createNewNode(5);
210 root->left->right->right = obj1.createNewNode(11);
211 root->right->right->left = obj1.createNewNode(4);
212
213 std::vector<std::uint64_t> actual_result_inorder{2, 7, 5, 6, 11,
214 2, 5, 4, 9};
215 std::vector<std::uint64_t> actual_result_preorder{2, 7, 2, 6, 5,
216 11, 5, 9, 4};
217 std::vector<std::uint64_t> actual_result_postorder{2, 5, 11, 6, 7,
218 4, 9, 5, 2};
219 std::vector<std::uint64_t>
220 result_inorder;
222 std::vector<std::uint64_t>
223 result_preorder;
225 std::vector<std::uint64_t>
226 result_postorder;
228
229 std::uint64_t size = actual_result_inorder.size();
230
231 // Calling inorder() function by passing a root node,
232 // and storing the inorder traversal in result_inorder.
233 result_inorder = obj1.inorder(root);
234 std::cout << "Testcase #1: Inorder Traversal...";
235 for (auto i = 0; i < size; ++i) {
236 assert(actual_result_inorder[i] == result_inorder[i]);
237 }
238 std::cout << "Passed!" << std::endl;
239
240 // Calling preorder() function by passing a root node,
241 // and storing the preorder traversal in result_preorder.
242 result_preorder = obj1.preorder(root);
243 std::cout << "Testcase #1: Preorder Traversal...";
244 for (auto i = 0; i < size; ++i) {
245 assert(actual_result_preorder[i] == result_preorder[i]);
246 }
247 std::cout << "Passed!" << std::endl;
248
249 // Calling postorder() function by passing a root node,
250 // and storing the postorder traversal in result_postorder.
251 result_postorder = obj1.postorder(root);
252 std::cout << "Testcase #1: Postorder Traversal...";
253 for (auto i = 0; i < size; ++i) {
254 assert(actual_result_postorder[i] == result_postorder[i]);
255 }
256 std::cout << "Passed!" << std::endl;
257
258 std::cout << std::endl;
259 deleteAll(root);
260}
261
266void test2() {
269 root->left = obj2.createNewNode(2);
270 root->right = obj2.createNewNode(3);
271 root->left->left = obj2.createNewNode(4);
272 root->right->left = obj2.createNewNode(5);
273 root->right->right = obj2.createNewNode(6);
274 root->right->left->left = obj2.createNewNode(7);
275 root->right->left->right = obj2.createNewNode(8);
276
277 std::vector<std::uint64_t> actual_result_inorder{4, 2, 1, 7, 5, 8, 3, 6};
278 std::vector<std::uint64_t> actual_result_preorder{1, 2, 4, 3, 5, 7, 8, 6};
279 std::vector<std::uint64_t> actual_result_postorder{4, 2, 7, 8, 5, 6, 3, 1};
280 std::vector<std::uint64_t>
281 result_inorder;
283 std::vector<std::uint64_t>
284 result_preorder;
286 std::vector<std::uint64_t>
287 result_postorder;
289
290 std::uint64_t size = actual_result_inorder.size();
291
292 // Calling inorder() function by passing a root node,
293 // and storing the inorder traversal in result_inorder.
294 result_inorder = obj2.inorder(root);
295 std::cout << "Testcase #2: Inorder Traversal...";
296 for (auto i = 0; i < size; ++i) {
297 assert(actual_result_inorder[i] == result_inorder[i]);
298 }
299 std::cout << "Passed!" << std::endl;
300
301 // Calling preorder() function by passing a root node,
302 // and storing the preorder traversal in result_preorder.
303 result_preorder = obj2.preorder(root);
304 std::cout << "Testcase #2: Preorder Traversal...";
305 for (auto i = 0; i < size; ++i) {
306 assert(actual_result_preorder[i] == result_preorder[i]);
307 }
308 std::cout << "Passed!" << std::endl;
309
310 // Calling postorder() function by passing a root node,
311 // and storing the postorder traversal in result_postorder.
312 result_postorder = obj2.postorder(root);
313 std::cout << "Testcase #2: Postorder Traversal...";
314 for (auto i = 0; i < size; ++i) {
315 assert(actual_result_postorder[i] == result_postorder[i]);
316 }
317 std::cout << "Passed!" << std::endl;
318
319 std::cout << std::endl;
320 deleteAll(root);
321}
322
327void test3() {
330 root->left = obj3.createNewNode(2);
331 root->right = obj3.createNewNode(3);
332 root->left->left = obj3.createNewNode(4);
333 root->left->right = obj3.createNewNode(5);
334
335 std::vector<std::uint64_t> actual_result_inorder{4, 2, 5, 1, 3};
336 std::vector<std::uint64_t> actual_result_preorder{1, 2, 4, 5, 3};
337 std::vector<std::uint64_t> actual_result_postorder{4, 5, 2, 3, 1};
338 std::vector<std::uint64_t>
339 result_inorder;
341 std::vector<std::uint64_t>
342 result_preorder;
344 std::vector<std::uint64_t>
345 result_postorder;
347
348 std::uint64_t size = actual_result_inorder.size();
349
350 // Calling inorder() function by passing a root node,
351 // and storing the inorder traversal in result_inorder.
352
353 result_inorder = obj3.inorder(root);
354 std::cout << "Testcase #3: Inorder Traversal...";
355 for (auto i = 0; i < size; ++i) {
356 assert(actual_result_inorder[i] == result_inorder[i]);
357 }
358 std::cout << "Passed!" << std::endl;
359
360 // Calling preorder() function by passing a root node,
361 // and storing the preorder traversal in result_preorder.
362 result_preorder = obj3.preorder(root);
363 std::cout << "Testcase #3: Preorder Traversal...";
364 for (auto i = 0; i < size; ++i) {
365 assert(actual_result_preorder[i] == result_preorder[i]);
366 }
367 std::cout << "Passed!" << std::endl;
368
369 // Calling postorder() function by passing a root node,
370 // and storing the postorder traversal in result_postorder.
371 result_postorder = obj3.postorder(root);
372 std::cout << "Testcase #3: Postorder Traversal...";
373 for (auto i = 0; i < size; ++i) {
374 assert(actual_result_postorder[i] == result_postorder[i]);
375 }
376 std::cout << "Passed!" << std::endl;
377
378 std::cout << std::endl;
379 deleteAll(root);
380}
381
386static void tests() {
387 std::cout << "1st test-case" << std::endl;
388 test1(); // run 1st test-case
389 std::cout << "2nd test-case" << std::endl;
390 test2(); // run 2nd test-case
391 std::cout << "3rd test-case" << std::endl;
392 test3(); // run 3rd test-case
393}
398int main() {
399 tests(); // run self-test implementations
400 return 0;
401}
struct node { int data; int height; struct node *left; struct node *right;} node
for std::queue
Definition avltree.cpp:13
BT used to make the entire structure of the binary tree and the functions associated with the binary ...
std::vector< std::uint64_t > preorder(Node *)
preorder function that will perform the preorder traversal recursively, and return the resultant vect...
std::vector< std::uint64_t > postorder(Node *)
postorder function that will perform the postorder traversal recursively, and return the result vecto...
Node * createNewNode(std::uint64_t)
will allocate the memory for a node and, along the data and return the node.
int data[MAX]
test data
for vector
void test2()
2nd test-case
void test1()
1st test-case
static void tests()
Self-test implementations.
void test3()
3rd test-case
int main()
Main function.
The structure to hold Nodes of the tree.
std::uint64_t data
The value/key of the node.
struct Node * right
struct pointer to right subtree.