#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
}*root=NULL;
void insert();
void preorder(struct node *q);
void mirror(struct node *q);
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.insert \n 2.preorder");
printf("\n 3.mirror \n 4.exit");
printf("\n enter your choice:-");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : printf("\n\n our tree in preorder \n\n");
preorder(root);
break;
case 3 :
mirror(root);
printf("\n\n mirror image is:-\n\n");
preorder(root);
break;
case 4 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
int x,i,n;
printf("\n\n enter how many node you want:=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n insufficient memory");
}
else
{
printf("\n\n enter number:-");
scanf("%d",&x);
temp->info=x;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
{
root=temp;
}
else
{
q=root;
while(1)
{
if(temp->info==q->info)
{
printf("\n\n element already exist");
break;
}
if(temp->info>q->info)
{
if(q->rlink==NULL)
{
q->rlink=temp;
break;
}
q=q->rlink;
}
if(temp->info<q->info)
{
if(q->llink==NULL)
{
q->llink=temp;
break;
}
q=q->llink;
}//end of if
}//end of while
}//end of else2
}//end of else1
}//end of for loop
}
void preorder(struct node *q)
{
if(q!=NULL)
{
printf(" %d->",q->info);
preorder(q->llink);
preorder(q->rlink);
}
}
void mirror(struct node *q)
{
struct node *temp1;
if(q!=NULL)
{
mirror(q->llink);
mirror(q->rlink);
temp1=q->llink;
q->llink=q->rlink;
q->rlink=temp1;
}
}
#include<conio.h>
#include<process.h>
#include<stdlib.h>
struct node
{
int info;
struct node *llink;
struct node *rlink;
}*root=NULL;
void insert();
void preorder(struct node *q);
void mirror(struct node *q);
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.insert \n 2.preorder");
printf("\n 3.mirror \n 4.exit");
printf("\n enter your choice:-");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : printf("\n\n our tree in preorder \n\n");
preorder(root);
break;
case 3 :
mirror(root);
printf("\n\n mirror image is:-\n\n");
preorder(root);
break;
case 4 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
int x,i,n;
printf("\n\n enter how many node you want:=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n insufficient memory");
}
else
{
printf("\n\n enter number:-");
scanf("%d",&x);
temp->info=x;
temp->llink=NULL;
temp->rlink=NULL;
if(root==NULL)
{
root=temp;
}
else
{
q=root;
while(1)
{
if(temp->info==q->info)
{
printf("\n\n element already exist");
break;
}
if(temp->info>q->info)
{
if(q->rlink==NULL)
{
q->rlink=temp;
break;
}
q=q->rlink;
}
if(temp->info<q->info)
{
if(q->llink==NULL)
{
q->llink=temp;
break;
}
q=q->llink;
}//end of if
}//end of while
}//end of else2
}//end of else1
}//end of for loop
}
void preorder(struct node *q)
{
if(q!=NULL)
{
printf(" %d->",q->info);
preorder(q->llink);
preorder(q->rlink);
}
}
void mirror(struct node *q)
{
struct node *temp1;
if(q!=NULL)
{
mirror(q->llink);
mirror(q->rlink);
temp1=q->llink;
q->llink=q->rlink;
q->rlink=temp1;
}
}