C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Data Structures in C

Inorder Traversal - Iterative Method

Concept Overview

The recursive version of inorder traversal uses the call stack internally.
In the iterative approach, we explicitly use a stack data structure to simulate recursion.

Traversal Order:
    Left Subtree → Root → Right Subtree

Algorithm Steps

  1. Start at the root node.
  2. Push all left nodes to the stack until reaching NULL.
  3. Pop a node from the stack, visit (print) it.
  4. Move to its right subtree.
  5. Repeat steps 2–4 until both stack is empty and current node is NULL.

 

C Program: Inorder Traversal - Iterative Method

C

#include <stdio.h>

#include <stdlib.h>

 

// Binary Tree Node Structure

struct Node {

    int data;

    struct Node *left, *right;

};

 

// Stack structure to simulate recursion

struct Stack {

    int top;

    int capacity;

    struct Node **array;

};

 

// Function to create a new tree node

struct Node* createNode(int data) {

    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

    newNode->data = data;

    newNode->left = newNode->right = NULL;

    return newNode;

}

 

// Function to create a stack

struct Stack* createStack(int capacity) {

    struct Stack* stack = (struct Stack*)malloc(sizeof(struct Stack));

    stack->top = -1;

    stack->capacity = capacity;

    stack->array = (struct Node**)malloc(stack->capacity * sizeof(struct Node*));

    return stack;

}

 

// Stack utility functions

int isEmpty(struct Stack* stack) {

    return stack->top == -1;

}

 

void push(struct Stack* stack, struct Node* node) {

    stack->array[++stack->top] = node;

}

 

struct Node* pop(struct Stack* stack) {

    if (isEmpty(stack))

        return NULL;

    return stack->array[stack->top--];

}

 

// Iterative Inorder Traversal

void inorderTraversal(struct Node* root) {

    struct Stack* stack = createStack(100); // Allocate stack space

    struct Node* current = root;

 

    printf("Inorder Traversal (Iterative): ");

 

    while (current != NULL || !isEmpty(stack)) {

        // Reach the leftmost node of the current node

        while (current != NULL) {

            push(stack, current);

            current = current->left;

        }

 

        // Pop from stack and visit node

        current = pop(stack);

        printf("%d ", current->data);

 

        // Visit the right subtree

        current = current->right;

    }

 

    printf("\n");

}

 

// Main function

int main() {

    /*

              1

             / \

            2   3

           / \

          4   5

    */

 

    struct Node* root = createNode(1);

    root->left = createNode(2);

    root->right = createNode(3);

    root->left->left = createNode(4);

    root->left->right = createNode(5);

 

    inorderTraversal(root);

 

    return 0;

}

Output

 
OUTPUT :
Inorder Traversal (Iterative): 4 2 5 1 3

Step-by-Step Working

Using the tree:

       1

      / \

     2   3

    / \

   4   5

Process simulation:

Step

Current Node

Stack (Top → Bottom)

Output

1

1

1

2

2

2,1

3

4

4,2,1

4

NULL

2,1

4

5

5

5,1

4 2

6

NULL

1

4 2 5

7

NULL

4 2 5 1 3

Final Output → 4 2 5 1 3

Key Points

Feature

Description

Approach

Non-recursive (uses stack explicitly)

Complexity

Time: O(n), Space: O(n)

Advantage

Avoids recursion (useful in systems with limited stack size)

Use Case

Common in iterative tree algorithms, compilers, and memory-sensitive systems