Preorder Tree Traversal without Recursion in C

Preorder Tree Traversal Implementation in C

We can perform Preorder 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 Preorder traversal on tree. Preorder traversal of tree is Root->Left->Right.  Here we will learn how to implement preorder traversal of tree using stack and its algorithm.

Preorder Tree Traversal without Recursion in C

Steps for Implementing Preorder Tree traversal in C

In Following Steps we have define how to implement Preorder 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. Print the node and 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  initialize temp from Poped element’s 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 Preorder Traversing in Tree

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

Inorder_Traversal()

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

C Program for Preorder Tree Traversing without Recursion

#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 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
	{
		if(temp){
			printf("%d",temp->data);
                        push(&s_temp,temp);
			temp = temp->left;
		}
		else{
			if(!isEmpty(s_temp)){
				temp = pop(&s_temp);
				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);
	tree_traversal(root); 
	return 0;
}
output: 85764

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 Print the node and 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.
  •  Check 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.

Different types of Traversing without Recursion

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