Ads La3 - Binary Tree Non Recursive Traversal
Ads La3 - Binary Tree Non Recursive Traversal
TREE TRAVERSAL
Name: Om Lohade
Class: IT – C
PRN: 12320123
Roll no.: SEDA-3
C code for creating BT/BST and inserting element of user choice, Non-Recursive
Inorder, Preorder, Tree Traversal
Explanation:
The provided code implements Preorder and Inorder Traversals of a binary tree using
iterative methods with a custom stack data structure. In Preorder Traversal, the root node is
visited first, followed by the left subtree, and then the right subtree. In Inorder Traversal, the
left subtree is visited first, then the root node, and finally the right subtree. Both functions
utilize a temporary pointer and a stack to perform iterative node processing, avoiding
recursion. The traversal sequences are printed for the binary tree created using user-input
values, offering an efficient and space-saving approach to traverse the tree nodes.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
};
struct stack
};
new_n->data = n;
new_n->next = (*top);
(*top) = new_n;
top = *top_n;
item = top->data;
*top_n = top->next;
free (top);
return item;
{
if (top == NULL)
return 1;
else
return 0;
int flag = 1;
while (flag) //Loop run untill temp is null and stack is empty
if (temp)
temp = temp->left;
else
if (!isEmpty (s_temp))
temp = temp->right;
else
flag = 0;
}
int flag = 1;
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;
new_n->left = NULL;
new_n->right = NULL;
return (new_n);
int main ()
int a[5];
scanf("%d", &a[i]);
pre_traversal(root);
inorder_traversal(root);
return 0;
}
C code for creating BT/BST and inserting element of user choice, Non-Recursive
Postorder Traversal
Explanation:
This code implements an iterative Postorder Traversal of a binary tree using a custom stack
data structure. It defines a binary tree node structure and a stack structure. The `postorder`
function traverses the tree in a postorder manner without recursion. It uses a stack to keep
track of nodes during traversal. The function iteratively processes the left child nodes,
pushes nodes with right children onto the stack, and then processes the right child nodes.
Finally, it prints the data of the current node in postorder sequence. The Postorder Traversal
result is printed for the binary tree constructed using user-input values.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
};
struct Stack
int size;
int top;
};
node->data = data;
return node;
stack->size = size;
stack->top = -1;
stack->array =
return stack;
{
return stack->top - 1 == stack->size;
if (isFull (stack))
return;
stack->array[++stack->top] = node;
if (isEmpty (stack))
return NULL;
return stack->array[stack->top--];
if (isEmpty (stack))
return NULL;
return stack->array[stack->top];
{
if (root == NULL)
return;
do
while (root)
if (root->right)
root = root->left;
pop (stack);
root = root->right;
else
root = NULL;
int main ()
int a[7];
printf("Enter 7 integers:");
scanf("%d", &a[i]);
postorder(root);
return 0;