Postorder Traversal of a Binary Tree without using Recursion in C

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

Here is source code of the C Program to Perform Postorder 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. // C program for iterative postorder traversal using one stack
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4.  
  5. // Maximum stack size
  6. #define MAX_SIZE 100
  7.  
  8. // A tree node
  9. struct Node {
  10.     int data;
  11.     struct Node *left, *right;
  12. };
  13.  
  14. // Stack type
  15. struct Stack {
  16.     int size;
  17.     int top;
  18.     struct Node* *array;
  19. };
  20.  
  21. // A utility function to create a new tree node
  22. struct Node* newNode(int data) {
  23.     struct Node* node = (struct Node*) malloc(sizeof(struct Node));
  24.     node->data = data;
  25.     node->left = node->right = NULL;
  26.     return node;
  27. }
  28.  
  29. // A utility function to create a stack of given size
  30. struct Stack* createStack(int size) {
  31.     struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
  32.     stack->size = size;
  33.     stack->top = -1;
  34.     stack->array = (struct Node**) malloc(stack->size * sizeof(struct Node*));
  35.     return stack;
  36. }
  37.  
  38. // BASIC OPERATIONS OF STACK
  39. int isFull(struct Stack* stack) {
  40.     return stack->top - 1 == stack->size;
  41. }
  42.  
  43. int isEmpty(struct Stack* stack) {
  44.     return stack->top == -1;
  45. }
  46.  
  47. void push(struct Stack* stack, struct Node* node) {
  48.     if (isFull(stack))
  49.         return;
  50.     stack->array[++stack->top] = node;
  51. }
  52.  
  53. struct Node* pop(struct Stack* stack) {
  54.     if (isEmpty(stack))
  55.         return NULL;
  56.     return stack->array[stack->top--];
  57. }
  58.  
  59. struct Node* peek(struct Stack* stack) {
  60.     if (isEmpty(stack))
  61.         return NULL;
  62.     return stack->array[stack->top];
  63. }
  64.  
  65. // An iterative function to do postorder traversal of a given binary tree
  66. void postOrderIterative(struct Node* root) {
  67.     // Check for empty tree
  68.     if (root == NULL)
  69.         return;
  70.  
  71.     struct Stack* stack = createStack(MAX_SIZE);
  72.     do {
  73.         // Move to leftmost node
  74.         while (root) {
  75.             // Push root's right child and then root to stack.
  76.             if (root->right)
  77.                 push(stack, root->right);
  78.             push(stack, root);
  79.  
  80.             // Set root as root's left child
  81.             root = root->left;
  82.         }
  83.  
  84.         // Pop an item from stack and set it as root
  85.         root = pop(stack);
  86.  
  87.         // If the popped item has a right child and the right child is not
  88.         // processed yet, then make sure right child is processed before root
  89.         if (root->right && peek(stack) == root->right) {
  90.             pop(stack); // remove right child from stack
  91.             push(stack, root); // push root back to stack
  92.             root = root->right; // change root so that the right
  93.             // child is processed next
  94.         } else // Else print root's data and set root as NULL
  95.         {
  96.             printf("%d ", root->data);
  97.             root = NULL;
  98.         }
  99.     } while (!isEmpty(stack));
  100. }
  101.  
  102. // Driver program to test above functions
  103. int main() {
  104.     // Let us construct the tree shown in above figure
  105.     struct Node* root = NULL;
  106.     root = newNode(1);
  107.     root->left = newNode(2);
  108.     root->right = newNode(3);
  109.     root->left->left = newNode(4);
  110.     root->left->right = newNode(5);
  111.     root->right->left = newNode(6);
  112.     root->right->right = newNode(7);
  113.  
  114.     postOrderIterative(root);
  115.  
  116.     return 0;
  117. }

Output:

$ gcc PostorderNonRecursive.c
$ ./a.out
 
4 5 2 6 7 3 1

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.