Please login


Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Inorder Tree Traversal without Recursion in C

Inorder Tree Traversal Implementation in C

We can perform Inorder Tree Traversal without Recursion in C in two ways, First we can use stack and other one is iterative method known as morris traversal. In given example we are using stack method to perform inorder traversal on tree. Inorder traversal of tree is Left->Root->Right.  Here we will learn how to implement inorder traversal of tree using stack and its algorithm.

Inorder Tree Traversal without Recursion in C

Steps for Implementing Inorder Tree traversal in C

In Following Steps we have define how to implement Inorder tree traversal without recursion in C :

  1. Take an empty stack.
  2. Push the root of the tree in stack.
  3. Take the temporary node and initialize it with root node of tree.
  4. push the left node of temp node in stack and initialize temp from temp->left.
  5. do the same task till left node temp is not null.
  6. If temp become null and stack is not empty then pop the element from the stack and print it and initialize temp from Poped element right node.
  7. do the same task of pushing left node of and initializing temp from temp left.
  8. If temp is null and stack is empty then return.

Algorithm for Inorder Traversing in Tree

This is the main traversing function Algorithm which is used in Inorder tree traversing without recursion program:



  • Flag = 1
  • WHILE(Flag)
  • IF(Temp)
  • push(Temp)
  • Temp = Temp->Left
  • END IF
  • ELSE
  • IF(isEmpty() == 0)
  • Temp = pop()
  • Print(Temp)
  • Temp = Temp->Right
  • ELSE
  • Flag =0

C Program for Inorder Tree Traversing without Recursion

struct node // node defining for tree
	struct node* left;
	struct node* right;
	int data;
struct stack // node defining for stack
	struct node* data;
	struct stack* next;

void push(struct stack** top,struct node* n); //function declation
struct node* pop(struct stack** top);
int isEmpty(struct stack* top);

int tree_traversal(struct node* root) //Inorder Traversing function
	struct node* temp = root;
	struct stack* s_temp = NULL;
	int flag = 1;
	while(flag) //Loop run untill temp is null and stack is empty
			temp = temp->left;
				temp = pop(&s_temp);
				temp = temp->right;
				flag = 0;
void push(struct stack** top,struct node* n) //push node in stack
	struct stack* new_n = (struct stack*)malloc(sizeof(struct stack));
	new_n->data = n;
	new_n->next = (*top);
	(*top) = new_n;
int isEmpty(struct stack* top) // check if stack is empty
		return 1;
		return 0;
struct node* pop(struct stack** top_n) // pop the node from stack
	struct node* item;
	struct stack* top;
	top = *top_n;
	item = top->data;
	*top_n = top->next;
	return item;
struct node* create_node(int data) // create a node for tree
	struct node* new_n = (struct node*)malloc(sizeof(struct node));
	new_n->data = data;
	new_n->left = NULL;
	new_n->right = NULL;
	return (new_n);

int main() 
	struct node* root;
	root = create_node(8);
	root->left = create_node(5);
	root->right = create_node(4);
	root->left->left = create_node(7);
	root->left->right = create_node(6);
	return 0;
output: 75684

Program Explanation:

Struct Node{} :

  •  We are defining a structure node with integer data and two pointer pointers for left and right node.

Struct Stack{} :

  • Stack is the structure with a node data type data element and a pointer next.

Tree_traversal() :

  • In tree traversal function we traverse the tree left node untill we found null when there is null we backtrack in tree with the help of stack and pop the element and print it.
  • After printing that element we chack if there is right child of that node if yes then again we traverse it left of the node.
  • We do the same task again and again till temp node is empty and stack is empty.

Push() :

  • Push function is use to push node from the tree into stack.

Pop() :

  • Pop function is use for remove the node from stack and return it.

IsEmpty() :

  • isEmpty checks if stack is empty or not if yes then it returns 1 and else it returns 0.


  • We are using create node function to create nodes for tree.

Different types of Traversing without Recursion

Here we have already learned implementation of Inorder traversing without recursion click the button below to learn the implementation diffrent type of traversing without recursion.