#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct
node
{
int
data;
struct
node *left,*right;
};
struct
node *root;
void
ins(struct node *n,int val,int opt)
{
   struct node *t;
   t=(struct node *)malloc(sizeof(struct
node));
   t->data=val;
   t->right=t->left=NULL;
   if (opt==1)
    n->left=t;
   else
     n->right=t;
   printf("\n %d is inserted",val);
   if (opt==1)
   {
   printf("\tat the left\n");
   getch();
   }
   else
   {
   printf("\tat the right\n");
   getch();
   }
}
void
inser(struct node *t,int x)
{
if
(t->data >x)
if
(t->left==NULL)
ins(t,x,1);
else
inser(t->left,x);
else
if (t->data < x)
if
(t->right==NULL)
ins(t,x,2);
else
inser(t->right,x);
else
  printf("\n Element is already present in
the list\n");
}
void
inorder(struct node *p)
{
  if (p!=NULL)
  {
  inorder(p->left);
  printf("\n %5d",p->data);
  inorder (p->right);
  }
}
void
preorder(struct node *p)
{
  if (p!=NULL)
  {
  printf("\n %5d",p->data);
  preorder(p->left);
  preorder (p->right);
  }
}
 void postorder(struct node *p)
{
  if (p!=NULL)
  {
  preorder(p->left);
  preorder (p->right);
  printf("\n %5d",p->data);
  }
}
 void main()
{
int
op,n;
root=(struct
node *)malloc(sizeof(struct node));
root->data=30;
root->right=root->left=NULL;
clrscr();
do
{
    printf("\n 1.Insertion");
    printf("\n 2.Preorder");
    printf("\n 3.Inorder");
    printf("\n 4.Postorder");
    printf("\n 5.Quit");
    printf("\n Enter your choice\n");
    scanf("%d",&op);
  switch (op)
   {
    case 1: printf("\n Enter the element
to insert\n");
    scanf("%d",&n);
    inser(root,n);
    break;
    case 2: printf("\n The preorder
elements are\n");
    preorder(root);
    getch();
     break;
    case 3: printf("\n The inorder
elements are\n");
     inorder(root);
    getch();
     break;
    case 4: printf("\n The postorder
elements are\n");
     postorder(root);
     getch();
break;
     default: exit(0);
   }
  }while(op<5);
  getch();
   }
Output:
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
2
 2 is inserted 
at the left
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
3
 3 is inserted 
at the right
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
4
 4 is inserted 
at the right
 Enter the element to insert
5
 5 is inserted 
at the right
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
6
 6 is inserted 
at the right
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
8
 8 is inserted 
at the right
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
1
 Enter the element to insert
9
 9 is inserted 
at the right
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
2
 The preorder elements are
             30
             
2
             
3
             
4
             
5
             
6
             
8
             
9
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
3
 The inorder elements are
              2
             
3
             
4
             
5
             
6
             
8
             
9
             30
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
4
 The postorder elements are
             
2
             
3
             
4
             
5
             
6
             
8
             
9
             30
 1.Insertion
 2.Preorder
 3.Inorder
 4.Postorder
 5.Quit
 Enter your choice
5
 
 
No comments:
Post a Comment