TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
recursive_tree_traversal.cpp File Reference

Recursive version of Inorder, Preorder, and Postorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal) More...

#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Include dependency graph for recursive_tree_traversal.cpp:

Go to the source code of this file.

Classes

struct  others::recursive_tree_traversals::Node
 The structure to hold Nodes of the tree. More...
 
class  others::recursive_tree_traversals::BT
 BT used to make the entire structure of the binary tree and the functions associated with the binary tree. More...
 

Namespaces

namespace  others
 for vector
 
namespace  interpolation_search
 

Functions

void others::recursive_tree_traversals::deleteAll (const Node *const root)
 
void test1 ()
 1st test-case
 
void test2 ()
 2nd test-case
 
void test3 ()
 3rd test-case
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

Recursive version of Inorder, Preorder, and Postorder [Traversal of the Tree] (https://en.wikipedia.org/wiki/Tree_traversal)

Iterative Inorder Traversal of a tree

For traversing a (non-empty) binary tree in an inorder fashion, we must do these three things for every node n starting from the tree’s root:

(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (N) Process n itself. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.

In normal inorder traversal, we visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse inorder traversal.

Iterative Preorder Traversal of a tree

For traversing a (non-empty) binary tree in a preorder fashion, we must do these three things for every node n starting from the tree’s root:

(N) Process n itself. (L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again.

In normal preorder traversal, visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse preorder traversal.

Iterative Postorder Traversal of a tree

For traversing a (non-empty) binary tree in a postorder fashion, we must do these three things for every node n starting from the tree’s root:

(L) Recursively traverse its left subtree. When this step is finished, we are back at n again. (R) Recursively traverse its right subtree. When this step is finished, we are back at n again. (N) Process n itself.

In normal postorder traversal, visit the left subtree before the right subtree. If we visit the right subtree before visiting the left subtree, it is referred to as reverse postorder traversal.

Author
Lajat Manekar

Definition in file recursive_tree_traversal.cpp.

Function Documentation

◆ deleteAll()

void others::recursive_tree_traversals::deleteAll ( const Node *const root)

Definition at line 185 of file recursive_tree_traversal.cpp.

185 {
186 if (root) {
187 deleteAll(root->left);
188 deleteAll(root->right);
189 delete root;
190 }
191}

◆ main()

int main ( void )

Main function.

Returns
0 on exit

Definition at line 398 of file recursive_tree_traversal.cpp.

398 {
399 tests(); // run self-test implementations
400 return 0;
401}
static void tests()
Self-test implementations.

◆ test1()

void test1 ( )

1st test-case

Returns
void

< result stores the inorder traversal of the binary tree

< result stores the preorder traversal of the binary tree

< result stores the postorder traversal of the binary tree

Definition at line 201 of file recursive_tree_traversal.cpp.

201 {
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}
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.
The structure to hold Nodes of the tree.

◆ test2()

void test2 ( )

2nd test-case

Returns
void

< result stores the inorder traversal of the binary tree

< result stores the preorder traversal of the binary tree

< result stores the postorder traversal of the binary tree

Definition at line 266 of file recursive_tree_traversal.cpp.

266 {
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}

◆ test3()

void test3 ( )

3rd test-case

Returns
void

< result stores the inorder traversal of the binary tree

< result stores the preorder traversal of the binary tree

< result stores the postorder traversal of the binary tree

Definition at line 327 of file recursive_tree_traversal.cpp.

327 {
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}

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void

Definition at line 386 of file recursive_tree_traversal.cpp.

386 {
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}
void test2()
2nd test-case
void test1()
1st test-case
void test3()
3rd test-case