Preorder Traversal of a Binary Tree without using Recursion in C++

This is a C++ Program to print preorder traversal of a given binray tree without using recursion.

Here is source code of the C++ Program to Perform Preorder Non-Recursive Traversal of a Given Binary Tree. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <stack>
  5.  
  6. using namespace std;
  7.  
  8. /* A binary tree node has data, left child and right child */
  9. struct node
  10. {
  11.         int data;
  12.         struct node* left;
  13.         struct node* right;
  14. };
  15.  
  16. /* Helper function that allocates a new node with the given data and
  17.  NULL left and right  pointers.*/
  18. struct node* newNode(int data)
  19. {
  20.     struct node* node = new struct node;
  21.     node->data = data;
  22.     node->left = NULL;
  23.     node->right = NULL;
  24.     return (node);
  25. }
  26.  
  27. // An iterative process to print preorder traversal of Binary tree
  28. void iterativePreorder(node *root)
  29. {
  30.     // Base Case
  31.     if (root == NULL)
  32.         return;
  33.  
  34.     // Create an empty stack and push root to it
  35.     stack<node *> nodeStack;
  36.     nodeStack.push(root);
  37.  
  38.     /* Pop all items one by one. Do following for every popped item
  39.      a) print it
  40.      b) push its right child
  41.      c) push its left child
  42.      Note that right child is pushed first so that left is processed first */
  43.     while (nodeStack.empty() == false)
  44.     {
  45.         // Pop the top item from stack and print it
  46.         struct node *node = nodeStack.top();
  47.         printf("%d ", node->data);
  48.         nodeStack.pop();
  49.  
  50.         // Push right and left children of the popped node to stack
  51.         if (node->right)
  52.             nodeStack.push(node->right);
  53.         if (node->left)
  54.             nodeStack.push(node->left);
  55.     }
  56. }
  57.  
  58. int main()
  59. {
  60.     /* Constructed binary tree is
  61.            10
  62.          /   \
  63.         8      2
  64.       /  \    /
  65.      3     5  2
  66.      */
  67.     struct node *root = newNode(10);
  68.     root->left = newNode(8);
  69.     root->right = newNode(2);
  70.     root->left->left = newNode(3);
  71.     root->left->right = newNode(5);
  72.     root->right->left = newNode(2);
  73.     cout<<"Pre order traversal: ";
  74.     iterativePreorder(root);
  75.     return 0;
  76. }

Output:

$ g++ NonRecursivePreorder.cpp
$ a.out
 
Pre order traversal: 10 8 3 5 2 2 
 
------------------
(program exited with code: 0)
Press return to continue

Sanfoundry Global Education & Learning Series – 1000 C++ Programs.

advertisement
advertisement

Here’s the list of Best Books in C++ Programming, Data Structures and Algorithms.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.