Algorithms_in_C++  1.0.0
Set of algorithms implemented in C++.
iterative_tree_traversals.cpp File Reference

Iterative version of Preorder, Postorder, and preorder Traversal of the Tree More...

#include <iostream>
#include <stack>
#include <vector>
#include <algorithm>
#include <cassert>
Include dependency graph for iterative_tree_traversals.cpp:

Classes

struct  others::iterative_tree_traversals::Node
 defines the structure of a node of the tree More...
 
class  others::iterative_tree_traversals::BinaryTree
 defines the functions associated with the binary tree More...
 

Namespaces

 others
 for assert
 
 iterative_tree_traversals
 Functions for the Traversal of the Tree algorithm.
 

Functions

static void test1 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed preorder with the actual preorder. More...
 
static void test2 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed postorder with the actual postorder. More...
 
static void test3 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed inorder with the actual inorder. More...
 
static void test4 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed preorder with the actual preorder on negative value. More...
 
static void test5 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed postorder with the actual postorder on negative value. More...
 
static void test6 (others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
 Test the computed inorder with the actual inorder on negative value. More...
 
int main ()
 Main function. More...
 

Detailed Description

Iterative version of Preorder, Postorder, and preorder Traversal of the Tree

Author
Motasim

Iterative Preorder Traversal of a tree

Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first right child then left child. Repeat the same set of steps untill the Stack becomes empty. And return the result array as the preorder traversal of a tree.

Iterative Postorder Traversal of a tree

Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current, and pop and elemnt from the stack. Store the data of current into the result array, and start traversing from it. Push both the child node of the current node into the stack, first left child then right child. Repeat the same set of steps untill the Stack becomes empty. Now reverse the result array and then return it to the calling function as a postorder traversal of a tree.

Iterative Inorder Traversal of a tree

Create a Stack that will store the Node of Tree. Push the root node into the stack. Save the root into the variabe named as current. Now iterate and take the current to the extreme left of the tree by traversing only to its left. Pop the elemnt from the stack and assign it to the current. Store the data of current into the result array. Repeat the same set of steps until the Stack becomes empty or the current becomes NULL. And return the result array as the inorder traversal of a tree.

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit

< instace of BinaryTree, used to access its members functions.

317  {
318  // Creating a tree with the following structure,
319  /*
320  1
321  / \
322  2 3
323  / \
324  4 5
325  */
326 
327  others::iterative_tree_traversals::BinaryTree binaryTree; ///< instace of BinaryTree, used to access its members functions.
329  root->left = binaryTree.createNewNode(2);
330  root->right = binaryTree.createNewNode(3);
331  root->left->left = binaryTree.createNewNode(4);
332  root->left->right = binaryTree.createNewNode(5);
333 
334  std::cout<< "\n| Tests for positive data value |"<< std::endl;
335  test1(binaryTree, root); // run preorder-iterative test
336  std::cout<< "\nPre-order test Passed!"<< std::endl;
337 
338  test2(binaryTree, root); // run postorder-iterative test
339  std::cout<< "\nPost-order test Passed!"<< std::endl;
340 
341  test3(binaryTree, root); // run inorder-iterative test
342  std::cout<< "\nIn-order test Passed!"<< std::endl;
343 
344  // Modifying tree for negative values.
345  root->data = -1;
346  root->left->data = -2;
347  root->right->data = -3;
348  root->left->left->data = -4;
349  root->left->right->data = -5;
350 
351  std::cout<< "\n| Tests for negative data values |"<< std::endl;
352  test4(binaryTree, root); // run preorder-iterative test on negative values
353  std::cout<< "\nPre-order test on-negative value Passed!"<< std::endl;
354 
355  test5(binaryTree, root); // run postorder-iterative test on negative values
356  std::cout<< "\nPost-order test on-negative value Passed!"<< std::endl;
357 
358  test6(binaryTree, root); // run inorder-iterative test on negative values
359  std::cout<< "\nIn-order test on-negative value Passed!"<< std::endl;
360 
361  return 0;
362 }
defines the functions associated with the binary tree
Definition: iterative_tree_traversals.cpp:64
Node * createNewNode(int64_t)
function that will create new node for insertion.
Definition: iterative_tree_traversals.cpp:77
T data(T... args)
T endl(T... args)
static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed preorder with the actual preorder on negative value.
Definition: iterative_tree_traversals.cpp:246
static void test1(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed preorder with the actual preorder.
Definition: iterative_tree_traversals.cpp:174
static void test3(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed inorder with the actual inorder.
Definition: iterative_tree_traversals.cpp:222
static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed postorder with the actual postorder on negative value.
Definition: iterative_tree_traversals.cpp:270
static void test2(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed postorder with the actual postorder.
Definition: iterative_tree_traversals.cpp:198
static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, others::iterative_tree_traversals::Node *root)
Test the computed inorder with the actual inorder on negative value.
Definition: iterative_tree_traversals.cpp:294
defines the structure of a node of the tree
Definition: iterative_tree_traversals.cpp:55

◆ test1()

static void test1 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed preorder with the actual preorder.

Parameters
binaryTreeinstance of the BinaryTree class
roothead/root node of a tree

< result stores the preorder traversal of the binary tree

174  {
175  std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
176  std::vector<int64_t> result; ///< result stores the preorder traversal of the binary tree
177 
178  // Calling preOrderIterative() function by passing a root node,
179  // and storing the preorder traversal in result.
180  result = binaryTree.preOrderIterative(root);
181 
182  // Self-testing the result using `assert`
183  for(int i = 0; i < result.size(); i++)
184  assert(actual_result[i] == result[i]);
185 
186  // Printing the result storing preorder.
187  std::cout<< "\nPreOrder Traversal Is : "<< std::endl;
188  for(auto i: result) {
189  std::cout<< i<< " ";
190  }
191 }
std::vector< int64_t > preOrderIterative(Node *)
function that takes root of the tree as an argument, and returns its preorder traversal.
Definition: iterative_tree_traversals.cpp:90
uint64_t result(uint64_t n)
Definition: fibonacci_sum.cpp:76

◆ test2()

static void test2 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed postorder with the actual postorder.

Parameters
binaryTreeinstance of BinaryTree class
roothead/root node of a tree

< result stores the postorder traversal of the binary tree.

198  {
199  std::vector<int64_t> actual_result{4, 5, 2, 3, 1};
200  std::vector<int64_t> result; ///< result stores the postorder traversal of the binary tree.
201 
202  // Calling postOrderIterative() function by passing a root node,
203  // and storing the postorder traversal in result.
204  result = binaryTree.postOrderIterative(root);
205 
206  // Self-testing the result using `assert`
207  for(int i = 0; i < result.size(); i++)
208  assert(actual_result[i] == result[i]);
209 
210  // Printing the result storing postorder.
211  std::cout<< "\nPostOrder Traversal Is : "<< std::endl;
212  for(auto i: result) {
213  std::cout<< i<< " ";
214  }
215 }
std::vector< int64_t > postOrderIterative(Node *)
function that takes root of the tree as an argument, and returns its postorder traversal.
Definition: iterative_tree_traversals.cpp:118

◆ test3()

static void test3 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed inorder with the actual inorder.

Parameters
binaryTreeinstance of BinaryTree class
roothead/root node of a tree

< result stores the inorder traversal of the binary tree.

222  {
223  std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
224  std::vector<int64_t> result; ///< result stores the inorder traversal of the binary tree.
225 
226  // Calling inOrderIterative() function by passing a root node,
227  // and storing the inorder traversal in result.
228  result = binaryTree.inOrderIterative(root);
229 
230  // Self-testing the result using `assert`
231  for(int i = 0; i < result.size(); i++)
232  assert(actual_result[i] == result[i]);
233 
234  // Printing the result storing inorder.
235  std::cout<< "\nInOrder Traversal Is : "<< std::endl;
236  for(auto i: result) {
237  std::cout<< i<< " ";
238  }
239 }
std::vector< int64_t > inOrderIterative(Node *)
function that takes root of the tree as an argument, and returns its inorder traversal.
Definition: iterative_tree_traversals.cpp:148

◆ test4()

static void test4 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed preorder with the actual preorder on negative value.

Parameters
binaryTreeinstance of BinaryTree class
roothead/root node of a tree

< result stores the preorder traversal of the binary tree

246  {
247  std::vector<int64_t> actual_result{-1, -2, -4, -5, -3};
248  std::vector<int64_t> result; ///< result stores the preorder traversal of the binary tree
249 
250  // Calling preOrderIterative() function by passing a root node,
251  // and storing the preorder traversal in result.
252  result = binaryTree.preOrderIterative(root);
253 
254  // Self-testing the result using `assert`
255  for(int i = 0; i < result.size(); i++)
256  assert(actual_result[i] == result[i]);
257 
258  // Printing the result storing preorder.
259  std::cout<< "\nPreOrder Traversal Is : "<< std::endl;
260  for(auto i: result) {
261  std::cout<< i<< " ";
262  }
263 }

◆ test5()

static void test5 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed postorder with the actual postorder on negative value.

Parameters
binaryTreeinstance of BinaryTree class
roothead/root node of a tree

< result stores the postorder traversal of the binary tree.

270  {
271  std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
272  std::vector<int64_t> result; ///< result stores the postorder traversal of the binary tree.
273 
274  // Calling postOrderIterative() function by passing a root node,
275  // and storing the postorder traversal in result.
276  result = binaryTree.postOrderIterative(root);
277 
278  // Self-testing the result using `assert`
279  for(int i = 0; i < result.size(); i++)
280  assert(actual_result[i] == result[i]);
281 
282  // Printing the result storing postorder.
283  std::cout<< "\nPostOrder Traversal Is : "<< std::endl;
284  for(auto i: result) {
285  std::cout<< i<< " ";
286  }
287 }

◆ test6()

static void test6 ( others::iterative_tree_traversals::BinaryTree  binaryTree,
others::iterative_tree_traversals::Node root 
)
static

Test the computed inorder with the actual inorder on negative value.

Parameters
binaryTreeinstance of BinaryTree class
roothead/root node of a tree

< result stores the inorder traversal of the binary tree.

294  {
295  std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
296  std::vector<int64_t> result; ///< result stores the inorder traversal of the binary tree.
297 
298  // Calling inOrderIterative() function by passing a root node,
299  // and storing the inorder traversal in result.
300  result = binaryTree.inOrderIterative(root);
301 
302  // Self-testing the result using `assert`
303  for(int i = 0; i < result.size(); i++)
304  assert(actual_result[i] == result[i]);
305 
306  // Printing the result storing inorder.
307  std::cout<< "\nInOrder Traversal Is : "<< std::endl;
308  for(auto i: result) {
309  std::cout<< i<< " ";
310  }
311 }