Preorder Inorder and postorder traversal of binary search tree in C.

 program:-

#include<stdio.h>
#include<stdlib.h>
struct binary_s_tree
{
int data;
struct binary_s_tree *left;
struct binary_s_tree *right;
};
struct binary_s_tree *root=NULL;

// function for create createnode

struct binary_s_tree *createnode()
{
struct binary_s_tree *n;
n=(struct binary_s_tree*)malloc(sizeof(struct binary_s_tree));
return n;
}

// function insert element into tree

void insert()
{
struct binary_s_tree *temp,*n;
n=createnode();
printf(“Enter valuen”);
scanf(“%d”,&n->data);
n->left=NULL;
n->right=NULL;
if(root==NULL)
root=n;
else
{
temp=root;
while(temp!=NULL)
{
if(temp->data>n->data)
{
if(temp->left==NULL)
{
temp->left=n;
temp=temp->left;
}
temp=temp->left;
}
else
{
if(temp->right==NULL)
{
temp->right=n;
temp=temp->right;
}
temp=temp->right;
}
}
}
}

// function for preorder traversal

void preorder_traversal(struct binary_s_tree *temp)
{
if(temp)
{
printf(“%d “,temp->data);
preorder_traversal(temp->left);
preorder_traversal(temp->right);
}
}

// function for post order traversal

void postorder_traversal(struct binary_s_tree *temp)
{
if(temp)
{
postorder_traversal(temp->left);
postorder_traversal(temp->right);
printf(“%d “,temp->data);
}
}

// function for inorder traversal

void inorder_traversal(struct binary_s_tree *temp)
{
if(temp)
{
inorder_traversal(temp->left);
printf(“%d “,temp->data);
inorder_traversal(temp->right);
}
}
int main()
{
int ch;
printf(“1. insert element into treen”);
printf(“2. preorder traversaln”);
printf(“3. postorder traversaln”);
printf(“4. inorder traversaln”);
printf(“5. exitn”);
while(1)
{
printf(“nEnter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert();
break;
case 2: preorder_traversal(root);
break;
case 3: postorder_traversal(root);
break;
case 4: inorder_traversal(root);
break;
case 5: exit(0);
default: printf(“Wrong keyn”);
}
}
return 0;
}


2 thoughts on “Preorder Inorder and postorder traversal of binary search tree in C.

Leave a Reply

Your email address will not be published.