C Programs For Trees
C Programs For Trees
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct tree
{
int data;
struct tree *left,*right;
};
void main()
{
struct tree *root=NULL;
int option,item,n,i;
char ch;
struct tree* insertnode(struct tree*,int);
void inorder(struct tree*);
void preorder(struct tree*);
void postorder(struct tree*);
clrscr();
do
{
printf(“\n1. Create”);
printf(“\n2. Inorder Traversal”);
printf(“\n3.Preorder Traversal”);
printf(“\n4.Postorder Traversal”);
printf(“\nEnter your option(1-4):”);
scanf(“%d”,&option);
switch(option)
{
case 1: printf(“\nEnter how many
elements:”);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“\nEnter the item to be
inserted into BST:”);
scanf(“%d”,&item);
root=insertnode(root,item);
}
break;
case 2: printf(“\nInorder Traversal is:”);
inorder(root);
break;
case 3: printf(“\nPreorder Traversal is:”);
preorder(root);
break;
case 4: printf(“\nPostorder Traversal is :”);
postorder(root);
break;
default: printf(“\nWrong option”);
}
printf(“\nDo you wish to continue(y/n):”);
fflush(stdin);
scanf(“%c”,&ch);
}while(ch==’y’ || ch==’Y’);
getch();
}