Algorithms_in_C++ 1.0.0
Set of 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 <iostream>
#include <vector>
Include dependency graph for recursive_tree_traversal.cpp:

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
 
 

Functions

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

Function Documentation

◆ main()

int main ( void )

Main function.

Returns
0 on exit
373 {
374 tests(); // run self-test implementations
375 return 0;
376}
static void tests()
Self-test implementations.
Definition recursive_tree_traversal.cpp:361
Here is the call graph for this function:

◆ 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

191 {
194 root->left = obj1.createNewNode(7);
195 root->right = obj1.createNewNode(5);
196 root->left->left = obj1.createNewNode(2);
197 root->left->right = obj1.createNewNode(6);
198 root->right->right = obj1.createNewNode(9);
199 root->left->right->left = obj1.createNewNode(5);
200 root->left->right->right = obj1.createNewNode(11);
201 root->right->right->left = obj1.createNewNode(4);
202
203 std::vector<uint64_t> actual_result_inorder{2, 7, 5, 6, 11, 2, 5, 4, 9};
204 std::vector<uint64_t> actual_result_preorder{2, 7, 2, 6, 5, 11, 5, 9, 4};
205 std::vector<uint64_t> actual_result_postorder{2, 5, 11, 6, 7, 4, 9, 5, 2};
206 std::vector<uint64_t> result_inorder; ///< result stores the inorder
207 ///< traversal of the binary tree
208 std::vector<uint64_t> result_preorder; ///< result stores the preorder
209 ///< traversal of the binary tree
210 std::vector<uint64_t> result_postorder; ///< result stores the postorder
211 ///< traversal of the binary tree
212
213 uint64_t size = actual_result_inorder.size();
214
215 // Calling inorder() function by passing a root node,
216 // and storing the inorder traversal in result_inorder.
217 result_inorder = obj1.inorder(root);
218 std::cout << "Testcase #1: Inorder Traversal...";
219 for (auto i = 0; i < size; ++i) {
220 assert(actual_result_inorder[i] == result_inorder[i]);
221 }
222 std::cout << "Passed!" << std::endl;
223
224 // Calling preorder() function by passing a root node,
225 // and storing the preorder traversal in result_preorder.
226 result_preorder = obj1.preorder(root);
227 std::cout << "Testcase #1: Preorder Traversal...";
228 for (auto i = 0; i < size; ++i) {
229 assert(actual_result_preorder[i] == result_preorder[i]);
230 }
231 std::cout << "Passed!" << std::endl;
232
233 // Calling postorder() function by passing a root node,
234 // and storing the postorder traversal in result_postorder.
235 result_postorder = obj1.postorder(root);
236 std::cout << "Testcase #1: Postorder Traversal...";
237 for (auto i = 0; i < size; ++i) {
238 assert(actual_result_postorder[i] == result_postorder[i]);
239 }
240 std::cout << "Passed!" << std::endl;
241
243}
BT used to make the entire structure of the binary tree and the functions associated with the binary ...
Definition recursive_tree_traversal.cpp:87
Node * createNewNode(uint64_t)
will allocate the memory for a node and, along the data and return the node.
Definition recursive_tree_traversal.cpp:116
std::vector< uint64_t > postorder(Node *)
postorder function that will perform the postorder traversal recursively, and return the result vecto...
Definition recursive_tree_traversal.cpp:170
std::vector< uint64_t > preorder(Node *)
preorder function that will perform the preorder traversal recursively, and return the resultant vect...
Definition recursive_tree_traversal.cpp:150
T endl(T... args)
The structure to hold Nodes of the tree.
Definition recursive_tree_traversal.cpp:78
Here is the call graph for this function:

◆ 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

249 {
252 root->left = obj2.createNewNode(2);
253 root->right = obj2.createNewNode(3);
254 root->left->left = obj2.createNewNode(4);
255 root->right->left = obj2.createNewNode(5);
256 root->right->right = obj2.createNewNode(6);
257 root->right->left->left = obj2.createNewNode(7);
258 root->right->left->right = obj2.createNewNode(8);
259
260 std::vector<uint64_t> actual_result_inorder{4, 2, 1, 7, 5, 8, 3, 6};
261 std::vector<uint64_t> actual_result_preorder{1, 2, 4, 3, 5, 7, 8, 6};
262 std::vector<uint64_t> actual_result_postorder{4, 2, 7, 8, 5, 6, 3, 1};
263 std::vector<uint64_t> result_inorder; ///< result stores the inorder
264 ///< traversal of the binary tree
265 std::vector<uint64_t> result_preorder; ///< result stores the preorder
266 ///< traversal of the binary tree
267 std::vector<uint64_t> result_postorder; ///< result stores the postorder
268 ///< traversal of the binary tree
269
270 uint64_t size = actual_result_inorder.size();
271
272 // Calling inorder() function by passing a root node,
273 // and storing the inorder traversal in result_inorder.
274 result_inorder = obj2.inorder(root);
275 std::cout << "Testcase #2: Inorder Traversal...";
276 for (auto i = 0; i < size; ++i) {
277 assert(actual_result_inorder[i] == result_inorder[i]);
278 }
279 std::cout << "Passed!" << std::endl;
280
281 // Calling preorder() function by passing a root node,
282 // and storing the preorder traversal in result_preorder.
283 result_preorder = obj2.preorder(root);
284 std::cout << "Testcase #2: Preorder Traversal...";
285 for (auto i = 0; i < size; ++i) {
286 assert(actual_result_preorder[i] == result_preorder[i]);
287 }
288 std::cout << "Passed!" << std::endl;
289
290 // Calling postorder() function by passing a root node,
291 // and storing the postorder traversal in result_postorder.
292 result_postorder = obj2.postorder(root);
293 std::cout << "Testcase #2: Postorder Traversal...";
294 for (auto i = 0; i < size; ++i) {
295 assert(actual_result_postorder[i] == result_postorder[i]);
296 }
297 std::cout << "Passed!" << std::endl;
298
300}
Here is the call graph for this function:

◆ 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

306 {
309 root->left = obj3.createNewNode(2);
310 root->right = obj3.createNewNode(3);
311 root->left->left = obj3.createNewNode(4);
312 root->left->right = obj3.createNewNode(5);
313
314 std::vector<uint64_t> actual_result_inorder{4, 2, 5, 1, 3};
315 std::vector<uint64_t> actual_result_preorder{1, 2, 4, 5, 3};
316 std::vector<uint64_t> actual_result_postorder{4, 5, 2, 3, 1};
317 std::vector<uint64_t> result_inorder; ///< result stores the inorder
318 ///< traversal of the binary tree
319 std::vector<uint64_t> result_preorder; ///< result stores the preorder
320 ///< traversal of the binary tree
321 std::vector<uint64_t> result_postorder; ///< result stores the postorder
322 ///< traversal of the binary tree
323
324 uint64_t size = actual_result_inorder.size();
325
326 // Calling inorder() function by passing a root node,
327 // and storing the inorder traversal in result_inorder.
328
329 result_inorder = obj3.inorder(root);
330 std::cout << "Testcase #3: Inorder Traversal...";
331 for (auto i = 0; i < size; ++i) {
332 assert(actual_result_inorder[i] == result_inorder[i]);
333 }
334 std::cout << "Passed!" << std::endl;
335
336 // Calling preorder() function by passing a root node,
337 // and storing the preorder traversal in result_preorder.
338 result_preorder = obj3.preorder(root);
339 std::cout << "Testcase #3: Preorder Traversal...";
340 for (auto i = 0; i < size; ++i) {
341 assert(actual_result_preorder[i] == result_preorder[i]);
342 }
343 std::cout << "Passed!" << std::endl;
344
345 // Calling postorder() function by passing a root node,
346 // and storing the postorder traversal in result_postorder.
347 result_postorder = obj3.postorder(root);
348 std::cout << "Testcase #3: Postorder Traversal...";
349 for (auto i = 0; i < size; ++i) {
350 assert(actual_result_postorder[i] == result_postorder[i]);
351 }
352 std::cout << "Passed!" << std::endl;
353
355}
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
361 {
362 std::cout << "1st test-case" << std::endl;
363 test1(); // run 1st test-case
364 std::cout << "2nd test-case" << std::endl;
365 test2(); // run 2nd test-case
366 std::cout << "3rd test-case" << std::endl;
367 test3(); // run 3rd test-case
368}
void test2()
2nd test-case
Definition recursive_tree_traversal.cpp:249
void test1()
1st test-case
Definition recursive_tree_traversal.cpp:191
void test3()
3rd test-case
Definition recursive_tree_traversal.cpp:306
Here is the call graph for this function: