You are on page 1of 6

//Constructing a Binary tree with Prefix expression.

//Performing Recursive and non recursive traversals


#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<malloc.h>
#define MAX 30
//----------------------------------------------------------------------//
typedef struct node node;
struct node
{

//To form tree nodes


char data;
node *rlink; //For right link
node *llink; //For left link

};
//----------------------------------------------------------------------//
typedef struct stack stack; //To store nodes for non recursive traversals
struct stack
{
int top;
node *address[MAX];
}s1;
//Stack object created
//----------------------------------------------------------------------//
void initialize(stack *s)
{
s->top=-1;
}

//Initialising stack

//----------------------------------------------------------------------//
node* pop(stack *s)
{
node *addr;

//To pop nodes from stack

if(s->top!=-1)
{
addr=s->address[s->top];
s->top = s->top-1;
return (addr);
}
else
return 0;
}
//----------------------------------------------------------------------//
void push(stack *s,node *addr)
//To push nodes into stack
{
if(s->top!=(MAX-1))
{
s->top = 1+s->top ;

s->address[s->top]=addr;
}
}
//----------------------------------------------------------------------//
node *allocate_operator_space(node* next,char a)
{
next=(node*)malloc(sizeof(node));
next->data=a;
next->llink=NULL;
next->rlink=NULL;
push(&s1,next);
return(next);
}
//----------------------------------------------------------------------//
node* create_tree()
{

//Function To create prefix expression tree

node *root=NULL,*next=NULL,*operand,*temp=NULL;
char prefix[MAX];
int i,k=0;
printf("\nEnter an prefix expression.\n\n");
scanf("%s",prefix);
initialize(&s1);

// Initialising stack

for(i=0;prefix[i]!='\0';i++)
{
if( (prefix[i]>='a' && prefix[i]<='z') || (prefix[i]>='A' && prefix[i]<
='Z') ) //for operands
{
operand=(node*)malloc(sizeof(node));
operand->data=prefix[i];
operand->llink=NULL;
operand->rlink=NULL;
if( next->llink == NULL )
{
next->llink=operand;
}
else
{
next=pop(&s1);
next->rlink=operand;
}
}
else
{

//for operators
if(k==0)
{

//To store the address of root


next=allocate_operator_space(temp,prefix[i]);
root=next;
k=1;

else
{
if(next->llink==NULL)
{
next->llink=allocate_operator_space(temp,prefix[i]);
next=next->llink;
}
else
{
next=pop(&s1);
next->rlink=allocate_operator_space(temp,prefix[i]);
next=next->rlink;
}
}
}
}
return (root);
}
//----------------------------------------------------------------------//
void preorder_R(node*);
void inorder_R(node*);
void postorder_R(node*);
//----------------------------------------------------------------------//
void preorder_NR(node*);
void inorder_NR(node*);
void postorder_NR(node*);
//...........................Main Function................................//
main(void)
{
node *root;
int ch;
clrscr();
printf("\n To perform different types of traversals on a tree.\n");
root=create_tree();
do
{
printf("\n1:Preorder(R)\n2:Inorder(R)\n3:Postorder(R)");
printf("\n4:Preorder(N-R)\n5:Inorder(N-R)\n6:Postorder(N-R)\n7:Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
preorder_R(root);
break;
case 2:
inorder_R(root);
break;

case 3:
postorder_R(root);
break;
case 4:
preorder_NR(root);
break;
case 5:
inorder_NR(root);
break;
case 6:
postorder_NR(root);
break;
case 7:
exit(0);
break;
}
}while(ch!=7);
getch();
}
/*........................Recursive traversals...........................*/
void preorder_R(node *link)
{
if(link!=NULL)
{
printf("%c",link->data);
preorder_R(link->llink);
preorder_R(link->rlink);
}
}
void inorder_R(node *link)
{
if(link!=NULL)
{
inorder_R(link->llink);
printf("%c",link->data);
inorder_R(link->rlink);
}
}
void postorder_R(node *link)
{
if(link!=NULL)
{
postorder_R(link->llink);
postorder_R(link->rlink);
printf("%c",link->data);
}
}

/*........................Non-Recursive traversals...........................*/
void preorder_NR(node *root)
{
stack s;
node *link=NULL;
initialize(&s);
push(&s,NULL);
link=root;
while(link!=NULL)
{
printf("%c",link->data);
if(link!=NULL);
push(&s,link);
link=link->llink;
}
while(s.top!=-1)
{
link=pop(&s);
if(link!=NULL)
link=link->rlink;
while(link!=NULL)
{
printf("%c",link->data);
push(&s,link);
link=link->llink;
}
}
}
//----------------------------------------------------------------------//
void inorder_NR(node *root)
{
stack s;
node *link=NULL;
initialize(&s);
push(&s,NULL);
link=root;
while(link!=NULL)
{
push(&s,link);
if(link!=NULL);
link=link->llink;
}
while(s.top!=-1)
{
link=pop(&s);
if(link!=NULL)
{

printf("%c",link->data);
link=link->rlink;
}
while(link!=NULL)
{
push(&s,link);
link=link->llink;
}
}
}
//----------------------------------------------------------------------//
void postorder_NR(node *link)
{
stack s;
node *link1=NULL;
initialize(&s);
push(&s,NULL);
while(link!=NULL)
{
while(link!=NULL)
{
push(&s,link);
if(link->rlink!=NULL)
push(&s,link);
link=link->llink;
}
link=pop(&s);
link1=pop(&s);
while(link!=link1)
{
printf("%c",link->data);
link=link1;
if(link==NULL)
break;
link1=pop(&s);
}
if(link!=NULL)
{
link=link->rlink;
push(&s,link1);
}
}
}
//----------------------------------------------------------------------//