#include
struct tree
{
int info;
struct tree *left;
struct tree *right;
};
main()
{
struct tree *root;
int ch,ele;
root=NULL;
clrscr();
while(1)
{
printf("\nTREE OPERATIONS\n");
printf("1.INSERT\n");
printf("2.IN ORDER TRAVERSAL\n");
printf("3.PRE ORDER TRAVERSAL\n");
printf("4.POST ORDER TRAVERSAL\n");
printf("5.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element to insert");
scanf("%d",&ele);
insert(&root,ele);
printf("\nSuccessfully Inserted\n");
break;
case 2: inorder(root);
break;
case 3: preorder(root);
break;
case 4: postorder(root);
break;
case 5: exit(1);
}
}
}
inorder(struct tree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ",root->info);
inorder(root->right);
}
}
preorder(struct tree *root)
{
if(root!=NULL)
{
preorder(root->left);
preorder(root->right);
printf(" %d ",root->info);
}
}
postorder(struct tree *root)
{
if(root!=NULL)
{
printf(" %d ",root->info);
postorder(root->left);
postorder(root->right);
}
}
insert(struct tree **root,int ele)
{
struct tree *x,*y,*temp;
temp=(struct tree *)malloc(sizeof(struct tree));
temp->info=ele;
temp->left=temp->right=NULL;
x=y=*root;
if(*root==NULL)
*root=temp;
else
{
while(x!=NULL)
{
y=x;
if(ele
x=x->left;
else
x=x->right;
}
if(ele
y->left=temp;
else
y->right=temp;
}
}
No comments:
Post a Comment