Prog 15
Prog 15
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node * left;
struct node * right;
};
struct node *root=NULL;
void insert(int);
void create(int);
void display(struct node *,int);
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);
struct node * search(struct node *,int);
void main()
{
int item,ch,i,n;
clrscr();
while(1)
{
printf("Binary Search Tree\n");
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Dispaly\n");
printf("4.Inorder Traversal\n");
printf("5.Preorder Traversal\n");
printf("6.Postorder Traversal\n");
printf("7.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter how many elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter value of the node\n");
scanf("%d",&item);
create(item);
}
break;
case 2:printf("Enter an item to insert\n");
scanf("%d",&item);
if(search(root,item)!=NULL)
printf("Item found and no duplicate value alowed\n");
else
create(item);
break;
case 3:printf("Binary Search Tree\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
display(root,1);
break;
case 4:printf("Inorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
inorder(root);
break;
case 5:printf("Preorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
preorder(root);
break;
case 6:printf("Postorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
postorder(root);
break;
case 7:exit(0);
}
}
}
Algorithm
[[Here, NODE contains information field info. The left field of a node points to the address of
the left child of the node. The right field of a node points to the address of the right child of
the node. Root is the address of the root node of the tree .When root=null, tree is empty.]
Step1: Start
Step2: Display menu and accept user choice
Step3: If choice=1
a. Accept the ‘item’
b. Call the function search(root,item) to check whether the item already
exists.
c. If the item is found, print “Duplicates not allowed”
Else, Call the function create(item) to insert a new element to the tree
End if
Step5: If choice=2
Call the function inorder(root) to print the elements in the tree using inorder
traversal.
End if
Step6: If choice=3
Call the function preorder(root) to print the elements in the tree using preorder
traversal.
End if
Step7: If choice=4
Call the function postorder(root) to print the elements in the tree using
postorder traversal.
End if
Step8: If choice=5
Exit the program
End if
Step97: Stop
Step 1: [Traverse the left sub tree of the root in inorder recursively]
If root->left!=NULL then
Call inorder(root->left)
Step 2: [Process the root node]
Write root->info
Step 3: [Traverse the right sub tree of the root in inorder recursively]
If root->right!=NULL then
Call inorder(root->right)
Step 4: Return
Step 1: [Traverse the left sub tree of the root in postorder recursively]
If root->left!=NULL then
Call postorder(root->left)
Step 2: [Traverse the right sub tree of the root in postorder recursively]
If root->right!=NULL then
Call postorder(root->right)
Step 3: [Process the root node]
Write root->info
Step 4: Return