#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 inorder(struct node *q);
void postorder(struct node *q);
int stack[10];
int top=-1;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.insert \n 2.preorder");
printf("\n 3.inorder \n 4.postorder");
printf("\n 5.exit");
printf("\n enter your choice:-");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : preorder(root);
break;
case 3 : inorder(root);
break;
case 4 : postorder(root);
break;
case 5 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
int x,i,n;
printf("\n\n enter how many nodes 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;
}
}
}
}
}
}
void preorder(struct node *q)
{
top++;
stack[top]=q;
while(top!=-1)
{
q=stack[top];
top--;
if(q!=NULL)
{
printf(" %d->",q->info);
top++;
stack[top]=q->rlink;
top++;
stack[top]=q->llink;
}
}
}
void inorder(struct node *q)
{
while(top!=-1||q!=NULL)
{
if(q!=NULL)
{
top++;
stack[top]=q;
q=q->llink;
}
else
{
q=stack[top];
top--;
printf(" %d->",q->info);
q=q->rlink;
}
}
}
void postorder(struct node *q)
{
int f[5];
int top_p;
stack[++top]=NULL;
do
{
while(q!=NULL)
{
stack[++top]=q;
f[top]=1;
if(q->rlink!=NULL)
{
stack[++top]=q->rlink;
f[top]=-1;
}
q=q->llink;
}
top_p=top;
q=stack[top--];
while(f[top_p]==1)
{
printf(" %d->",q->info);
top_p=top;
q=stack[top--];
}
}
while(q!=NULL);
}
#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 inorder(struct node *q);
void postorder(struct node *q);
int stack[10];
int top=-1;
void main()
{
int n;
clrscr();
while(1)
{
printf("\n **menu**");
printf("\n 1.insert \n 2.preorder");
printf("\n 3.inorder \n 4.postorder");
printf("\n 5.exit");
printf("\n enter your choice:-");
scanf("%d",&n);
switch(n)
{
case 1 : insert();
break;
case 2 : preorder(root);
break;
case 3 : inorder(root);
break;
case 4 : postorder(root);
break;
case 5 : exit(0);
}
}
getch();
}
void insert()
{
struct node *temp,*q;
int x,i,n;
printf("\n\n enter how many nodes 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;
}
}
}
}
}
}
void preorder(struct node *q)
{
top++;
stack[top]=q;
while(top!=-1)
{
q=stack[top];
top--;
if(q!=NULL)
{
printf(" %d->",q->info);
top++;
stack[top]=q->rlink;
top++;
stack[top]=q->llink;
}
}
}
void inorder(struct node *q)
{
while(top!=-1||q!=NULL)
{
if(q!=NULL)
{
top++;
stack[top]=q;
q=q->llink;
}
else
{
q=stack[top];
top--;
printf(" %d->",q->info);
q=q->rlink;
}
}
}
void postorder(struct node *q)
{
int f[5];
int top_p;
stack[++top]=NULL;
do
{
while(q!=NULL)
{
stack[++top]=q;
f[top]=1;
if(q->rlink!=NULL)
{
stack[++top]=q->rlink;
f[top]=-1;
}
q=q->llink;
}
top_p=top;
q=stack[top--];
while(f[top_p]==1)
{
printf(" %d->",q->info);
top_p=top;
q=stack[top--];
}
}
while(q!=NULL);
}