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.

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
  • END ELSE
  • END ELSE

C Program for Inorder Tree Traversing without Recursion

Run
#include<stdio.h>
#include<stdlib.h>
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 Inorder(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
	{
		if(temp){
			push(&s_temp,temp);
			temp = temp->left;
		}
		else{
			if(!isEmpty(s_temp)){
				temp = pop(&s_temp);
				printf("%d ",temp->data);
				temp = temp->right;
			}
			else
				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
{
	if(top==NULL)
		return 1;
	else
		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;
	free(top);
	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);
	Inorder(root); 
	return 0;
}

Output:

7 5 6 8 4

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.
Create_node():
  • We are using create node function to create nodes for tree.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java