I have to make the program of inorder ,preorder,postorder traversal of the binary search tree using recursion in stacks.I have made it by recursion only.How can I make this .
I have written the code for the recursive method.
void gettree(struct node **, int);
void inorder(struct node *);
void postorder(struct node *);
void preorder(struct node *);
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
struct node *left;
struct node *right;
int data;
};
void main()
{
struct node *bt;
int a,n,i;
clrscr();
bt=NULL;
printf("\n\nEnter the no of the nodes you want to enter :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\n\nEnter the elments of the tree :");
scanf("%d",&a);
gettree (&bt,a);
}
printf("\n\nElements in INORDER are : \n\n");
inorder(bt);
printf("\n\nElements in PREORDER are : \n\n");
preorder(bt);
printf("\n\nElements in POSTORDER are : \n\n");
postorder(bt);
getch();
}
void gettree(struct node **root,int a)
{
struct node *temp;
if(*root==NULL)
{
temp=(struct node*)malloc(sizeof(struct node));
temp->left=NULL;
temp->right=NULL;
temp->data=a;
*root=temp;
return;
}
if((*root)->data>a)
gettree(&((*root)->left),a);
else
gettree(&((*root)->right),a);
}
void inorder(struct node *bt)
{
if(bt!=NULL)
{
inorder(bt->left);
printf("[ %d ]\t",bt->data);
inorder(bt->right);
}
else
return ;
}
void postorder(struct node *bt)
{
if(bt!=NULL)
{
postorder(bt->left);
postorder(bt->right);
printf("[ %d ]\t",bt->data);
}
else
return ;
}
void preorder(struct node *bt)
{
if(bt!=NULL)
{
printf("[ %d ]\t",bt->data);
preorder(bt->left);
preorder(bt->right);
}
else
return ;
}