preorder, inorder and postorder traversal of binary tree or binary search tree(BST) with program.

Traversal of binary tree (बाइनरी ट्री ट्रेवर्सल):-

There are following three types of binary tree traversal:-

यह निम्न तीन प्रकार से किया जा सकता है:- 

1. Pre-order Traversal प्री-ऑर्डर ट्रेवर्सल (NLR):-

In this type of binary tree traversal first of all we traverse root node(N) after that left sub tree(L) and in the end right sub tree(R) and this process is applied recursively in all sub-trees of the binary tree again. 

इस प्रकार के बाइनरी ट्री ट्रेवर्सल में पहले रूट नोड (N) को फिर लेफ्ट सब ट्री (L) और अंत में राईट सब ट्री (R) में क्रमश ट्रेवर्सल किया जाता है यही प्रक्रिया पुनः ट्री के प्रत्येक सब ट्री पर पुनरावर्ती रूप से लागू की जाती है।

void preorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
printf("%d",T->info);
preorder_traverse(T->lchild);
preorder_traverse(T->rchild);

}

2. In-order Traversal इन-ऑर्डर ट्रेवर्सल(LNR):-

In this type of binary tree traversal first of all we traverse left sub tree(L) after that root node(N) and in the end right sub tree(R) and this process is applied recursively in all sub-trees of the binary tree again. 

इस प्रकार के बाइनरी ट्री ट्रेवर्सल में पहले लेफ्ट सब ट्री  (L) को फिर रूट नोड (N) और अंत में राईट सब ट्री (R) में क्रमश ट्रेवर्सल किया जाता है यही प्रक्रिया पुनः ट्री के प्रत्येक सब ट्री पर पुनरावर्ती रूप से लागू की जाती है।

void inorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
inorder_traverse(T->lchild);
printf("%d",T->info);
inorder_traverse(T->rchild);
}

3. Post-order Traversal पोस्ट-ऑर्डर ट्रेवर्सल(LRN):-

In this type of binary tree traversal first of all we traverse left sub tree(L) after that right sub tree(R) and in the end root node(N)  and this process is applied recursively in all sub-trees of the binary tree again. 

इस प्रकार के बाइनरी ट्री ट्रेवर्सल में पहले लेफ्ट सब ट्री  (L) को फिर राईट सब ट्री (R) और अंत में रूट नोड (N) में क्रमश ट्रेवर्सल किया जाता है यही प्रक्रिया पुनः ट्री के प्रत्येक सब ट्री पर पुनरावर्ती रूप से लागू की जाती है।

void postorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
postorder_traverse(T->lchild);
postorder_traverse(T->rchild);
printf("%d",T->info);
}

C program for pre-order, in-order and post-order traversal of binary tree or binary search tree(BST).

#include<stdio.h>
#include<conio.h>
//definition of node
struct node{
struct node *lchild;
int info;
struct node *rchild;
};
typedef struct node NODE;
NODE *T=NULL;

//creation of binary tree 
NODE create_btree(int info,NODE *T){
if(T==NULL){
T=malloc(sizeof(NODE));
T->info=info;
T->lchild=NULL;
T->rchild=NULL;
return(T);
}
else{
if(info<T->info){
T->lchild=create_btree(info,T->lchild);
}
else{
T->rchild=create_btree(info,T->rchild);
}}}
//pre-order traversal of binary tree or binary search tree(BST)
void preorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
printf("%d",T->info);
preorder_traverse(T->lchild);
preorder_traverse(T->rchild);
}
//inorder traversal of binary tree or binary search tree(BST)
void inorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
inorder_traverse(T->lchild);
printf("%d",T->info);
inorder_traverse(T->rchild);
}
//postorder traversal of binary tree or binary search tree(BST)
void postorder_traverse(NODE *T){
if(T==NULL){
printf("Node is NULL\n");
getch();
return;
}
postorder_traverse(T->lchild);
postorder_traverse(T->rchild);
printf("%d",T->info);
}
//main function
void main(){
NODE *Tree=NULL;
int ch;
clrscr();
while(1){
printf("Operations of BST\n1. creation\n2.Preorder Traverse\n3.Inorder Traverse\n4.Postorder Traverse\n5.exit\nenter your choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter info\n");
scanf("%d",&info);
create_btree(info,T);
break;
case 2:
preorder_traverse(T);
break;
case:3
inorder_ traverse(T);
break;
case 4:
postorder_traverse(T);
break;
case 5:
exit(0);
}}}

No comments:

Post a Comment

Stack Data Structure, Push, Pop and Peek Operations , Applications of Stack

Stack is a linear data structure. It is collection of ordered data elements. It is also known as LIFO system (last in first out). It means i...