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 <algorithm>
#include <cassert>
#include <iostream>
#include <stack>
#include <vector>
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 vector
 
 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.

353  {
354  // Creating a tree with the following structure,
355  /*
356  1
357  / \
358  2 3
359  / \
360  4 5
361  */
362 
364  binaryTree; ///< instace of BinaryTree, used to access its members
365  ///< functions.
367  root->left = binaryTree.createNewNode(2);
368  root->right = binaryTree.createNewNode(3);
369  root->left->left = binaryTree.createNewNode(4);
370  root->left->right = binaryTree.createNewNode(5);
371 
372  std::cout << "\n| Tests for positive data value |" << std::endl;
373  test1(binaryTree, root); // run preorder-iterative test
374  std::cout << "\nPre-order test Passed!" << std::endl;
375 
376  test2(binaryTree, root); // run postorder-iterative test
377  std::cout << "\nPost-order test Passed!" << std::endl;
378 
379  test3(binaryTree, root); // run inorder-iterative test
380  std::cout << "\nIn-order test Passed!" << std::endl;
381 
382  // Modifying tree for negative values.
383  root->data = -1;
384  root->left->data = -2;
385  root->right->data = -3;
386  root->left->left->data = -4;
387  root->left->right->data = -5;
388 
389  std::cout << "\n| Tests for negative data values |" << std::endl;
390  test4(binaryTree, root); // run preorder-iterative test on negative values
391  std::cout << "\nPre-order test on-negative value Passed!" << std::endl;
392 
393  test5(binaryTree, root); // run postorder-iterative test on negative values
394  std::cout << "\nPost-order test on-negative value Passed!" << std::endl;
395 
396  test6(binaryTree, root); // run inorder-iterative test on negative values
397  std::cout << "\nIn-order test on-negative value Passed!" << std::endl;
398 
399  return 0;
400 }
defines the functions associated with the binary tree
Definition: iterative_tree_traversals.cpp:67
Node * createNewNode(int64_t)
function that will create new node for insertion.
Definition: iterative_tree_traversals.cpp:88
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:272
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:191
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:245
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:300
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:218
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:327
defines the structure of a node of the tree
Definition: iterative_tree_traversals.cpp:58

◆ 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

192  {
193  std::vector<int64_t> actual_result{1, 2, 4, 5, 3};
195  result; ///< result stores the preorder traversal of the binary tree
196 
197  // Calling preOrderIterative() function by passing a root node,
198  // and storing the preorder traversal in result.
199  result = binaryTree.preOrderIterative(root);
200 
201  // Self-testing the result using `assert`
202  for (int i = 0; i < result.size(); i++) {
203  assert(actual_result[i] == result[i]);
204  }
205 
206  // Printing the result storing preorder.
207  std::cout << "\nPreOrder Traversal Is : " << std::endl;
208  for (auto i : result) {
209  std::cout << i << " ";
210  }
211 }
std::vector< int64_t > preOrderIterative(Node *)
preOrderIterative() function that will perform the preorder traversal iteratively,...
Definition: iterative_tree_traversals.cpp:102
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.

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

◆ 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.

246  {
247  std::vector<int64_t> actual_result{4, 2, 5, 1, 3};
249  result; ///< result stores the inorder traversal of the binary tree.
250 
251  // Calling inOrderIterative() function by passing a root node,
252  // and storing the inorder traversal in result.
253  result = binaryTree.inOrderIterative(root);
254 
255  // Self-testing the result using `assert`
256  for (int i = 0; i < result.size(); i++) {
257  assert(actual_result[i] == result[i]);
258  }
259 
260  // Printing the result storing inorder.
261  std::cout << "\nInOrder Traversal Is : " << std::endl;
262  for (auto i : result) {
263  std::cout << i << " ";
264  }
265 }
std::vector< int64_t > inOrderIterative(Node *)
inOrderIterative() function that will perform the inorder traversal iteratively, and return the resul...
Definition: iterative_tree_traversals.cpp:164

◆ 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

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

◆ 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.

301  {
302  std::vector<int64_t> actual_result{-4, -5, -2, -3, -1};
304  result; ///< result stores the postorder traversal of the binary tree.
305 
306  // Calling postOrderIterative() function by passing a root node,
307  // and storing the postorder traversal in result.
308  result = binaryTree.postOrderIterative(root);
309 
310  // Self-testing the result using `assert`
311  for (int i = 0; i < result.size(); i++) {
312  assert(actual_result[i] == result[i]);
313  }
314 
315  // Printing the result storing postorder.
316  std::cout << "\nPostOrder Traversal Is : " << std::endl;
317  for (auto i : result) {
318  std::cout << i << " ";
319  }
320 }

◆ 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.

328  {
329  std::vector<int64_t> actual_result{-4, -2, -5, -1, -3};
331  result; ///< result stores the inorder traversal of the binary tree.
332 
333  // Calling inOrderIterative() function by passing a root node,
334  // and storing the inorder traversal in result.
335  result = binaryTree.inOrderIterative(root);
336 
337  // Self-testing the result using `assert`
338  for (int i = 0; i < result.size(); i++) {
339  assert(actual_result[i] == result[i]);
340  }
341 
342  // Printing the result storing inorder.
343  std::cout << "\nInOrder Traversal Is : " << std::endl;
344  for (auto i : result) {
345  std::cout << i << " ";
346  }
347 }