Preorder Traversal of a Binary Tree without using Recursion in C

«
»
This is a C Program to perform pre order traversal. Time Complexity: O(n)

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

Output:

advertisement
$ gcc PreorderNonRecursive.c
$ ./a.out
 
10 8 3 5 2 2

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

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

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 & technical discussions at Telegram SanfoundryClasses.