Displaying a Tree - both Recursive and Non Recursive methods
/*tree traversal inorder,preorder,postorder with recursive and nonrecursive*/
#include<stdio.h>
#include<alloc.h>
#include<conio.h>
typedef struct s
{ int val;
struct s *left,*right;
}node;
typedef struct ss
{ node *root;
}tree;
tree* maktr(void)
{
tree* t;
t=(tree*)malloc(sizeof(tree));
t->root=NULL;
return t;
}
void instr(tree *t,int v)
{
node *newn,*nd,*par;
nd=t->root;
newn=(node*)malloc(sizeof(node));
newn->val=v;
newn->left=newn->right=NULL;
if(nd==NULL)//tree is empty
{ t->root=newn;
return;
}
while(nd!=NULL)
{ if(v<nd->val)
{ par=nd;
nd=nd->left;
}
else if(v>nd->val)
{ par=nd;
nd=nd->right;
}
else
{
printf("\n the node with same exists");
return;
}
}
if(v<par->val)
par->left=newn;
else
par->right=newn;
}
node *instr1(node *nd,int v)
{
if(nd==NULL)
{ nd=(node*)malloc(sizeof(node));
nd->val=v;
nd->left=nd->right=NULL;
}
else if(v<nd->val)
nd->left=instr1(nd->left,v);
else
nd->right=instr1(nd->right,v);
return nd;
}
void inrdsp(node *nd)
{ if(nd)
{ inrdsp(nd->left);
printf(" %d",nd->val);
inrdsp(nd->right);
}
}
void predsp(node *nd)
{ if(nd)
{ printf(" %d",nd->val);
predsp(nd->left);
predsp(nd->right);
}
}
void posdsp(node *nd)
{ if(nd)
{ posdsp(nd->left);
posdsp(nd->right);
printf(" %d",nd->val);
}
}
typedef struct st
{ node *info;
struct st *next;
}node1;
typedef struct st1
{ node1 *top;
}stack;
stack* crst(void)
{
stack *st;
st=(stack*)malloc(sizeof(stack));
st->top=NULL;
return st;
}
void pushst(stack* st,node *kv)
{
node1* newn;
newn=(node1*)malloc(sizeof(node1));
newn->info=kv;
newn->next=NULL;
if(st->top==NULL)
st->top =newn;
else
{ newn->next=st->top;
st->top=newn;
}
}
node* popst(stack* s)
{ node *p;
if(s->top==NULL)
{ printf("\n The stack is empty");
return NULL;
}
p=s->top->info;
s->top=s->top->next;
return p;
}
void pretrav(tree *t)
{
node *nk=t->root;
stack *s=crst();
do
{
while(nk!=NULL)
{ printf(" %d",nk->val);
if(nk->right!=NULL)
pushst(s,nk->right);
nk=nk->left;
}
if(s->top!=NULL)
nk=popst(s);
else
nk=NULL;
}while(nk!=NULL);//end while
}
void inrtrav(tree *t)
{
node *k=t->root;
stack* s=crst();
while(k!=NULL)
{ while(k!=NULL)
{ pushst(s,k);
k=k->left;
}
while(s->top!=NULL)
{
k=popst(s);
printf(" %d",k->val);
if(k->right!=NULL)
{ k=k->right; break;
}
else
k=NULL;
}// end while 2
}//end while 1
}
void postrav(tree *t)
{
node *nk=t->root,*nk1;
stack *s=crst();
do
{
while(nk!=NULL)
{
pushst(s,nk);
if(nk->right!=NULL)
{ nk1=nk->right;
nk1->val=-nk1->val;
pushst(s,nk1);
}
nk=nk->left;
}
while(s->top!=NULL)
{ nk=popst(s);
if(nk->val>=0)
printf(" %d",nk->val);
else
{ nk->val=-nk->val;
break;
}
}
}while(s->top!=NULL);
}
void main()
{
tree *t;
int v,i;
t=maktr();
clrscr();
for(i=0;i<5;i++)
{
printf("\n Enter the value of node");
scanf("%d",&v);
t->root=instr1(t->root,v);
}
printf("\nThe inorder display of the tree\n");
inrdsp(t->root);
printf("\nThe preorder display of the tree\n");
predsp(t->root);
printf("\nThe postorder display of the tree\n");
posdsp(t->root);
printf("\nThe inorder display of the tree\n");
inrtrav(t);
printf("\nThe preorder display of the tree\n");
pretrav(t);
printf("\nThe postorder display of the tree\n");
postrav(t);
getch();
}
No comments:
Post a Comment