PROGRAM CODING:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 30
struct bintree
{
char data;
struct bintree *left,*right;
};
typedef struct bintree * elttype;
struct stack
{
elttype a[MAX];
int top;
}s;
int isempty()
{
if(s.top==-1)
return 1;
else return 0;
}
void initstack()
{
s.top=-1;
}
void push(elttype n)
{
s.top++;
s.a[s.top]=n;
}
elttype pop()
{
return(s.a[s.top--]);
}
void initbintree(struct bintree *root)
{
root=NULL;
}
void onenode(char elt)
{
struct bintree *temp;
temp=(struct bintree *)malloc(sizeof(struct bintree));
temp->data=elt;
temp->left=temp->right=NULL;
push(temp);
}
int exptree(struct bintree **root,char exp)
{
struct bintree *temp;
temp=(struct bintree *)malloc(sizeof(struct bintree));
temp->data=exp;
if(!isempty())
temp->right=pop();
else {printf("Binary tree cannot be constructed using the given expression!**!\n");return 0;}
if(!isempty())
temp->left=pop();
else {printf("Binary tree cannot be constructed using the given expression!**!\n");return 0;}
push(temp);
*root=temp;
return 1;
}
void preorder(struct bintree *root)
{
if(root!=NULL)
{
printf("%c",root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct bintree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%c",root->data);
inorder(root->right);
}
}
void postorder(struct bintree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c",root->data);
}
}
int main()
{
struct bintree *root;
char exp[30];
int i,ch;
root=(struct bintree *)malloc(sizeof(struct bintree));
initbintree(root);
printf("Enter a valid postfix expression: ");
scanf("%s",exp);
for(i=0;i<strlen(exp);i++)
{
if(isalpha(exp[i])||isdigit(exp[i]))
onenode(exp[i]);
else
{
switch(exp[i])
case '+':
case '-':
case '*':
case '/':
if(!exptree(&root,exp[i]))
return 1;
}
}
while(1)
{
printf("TRAVERSAL MENU:\n\t1.Preorder\n\t2.Inorder\n\t3.Postorder\n\t4.Exit\nChoice:");
scanf("%d",&ch);
if(ch==4)
break;
switch(ch)
{
case 1:
preorder(root);printf("\n");
break;
case 2:
inorder(root);printf("\n");
break;
case 3:
postorder(root);printf("\n");
break;
default:printf("Enter correct menu option!**!");
}
}
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#define MAX 30
struct bintree
{
char data;
struct bintree *left,*right;
};
typedef struct bintree * elttype;
struct stack
{
elttype a[MAX];
int top;
}s;
int isempty()
{
if(s.top==-1)
return 1;
else return 0;
}
void initstack()
{
s.top=-1;
}
void push(elttype n)
{
s.top++;
s.a[s.top]=n;
}
elttype pop()
{
return(s.a[s.top--]);
}
void initbintree(struct bintree *root)
{
root=NULL;
}
void onenode(char elt)
{
struct bintree *temp;
temp=(struct bintree *)malloc(sizeof(struct bintree));
temp->data=elt;
temp->left=temp->right=NULL;
push(temp);
}
int exptree(struct bintree **root,char exp)
{
struct bintree *temp;
temp=(struct bintree *)malloc(sizeof(struct bintree));
temp->data=exp;
if(!isempty())
temp->right=pop();
else {printf("Binary tree cannot be constructed using the given expression!**!\n");return 0;}
if(!isempty())
temp->left=pop();
else {printf("Binary tree cannot be constructed using the given expression!**!\n");return 0;}
push(temp);
*root=temp;
return 1;
}
void preorder(struct bintree *root)
{
if(root!=NULL)
{
printf("%c",root->data);
preorder(root->left);
preorder(root->right);
}
}
void inorder(struct bintree *root)
{
if(root!=NULL)
{
inorder(root->left);
printf("%c",root->data);
inorder(root->right);
}
}
void postorder(struct bintree *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf("%c",root->data);
}
}
int main()
{
struct bintree *root;
char exp[30];
int i,ch;
root=(struct bintree *)malloc(sizeof(struct bintree));
initbintree(root);
printf("Enter a valid postfix expression: ");
scanf("%s",exp);
for(i=0;i<strlen(exp);i++)
{
if(isalpha(exp[i])||isdigit(exp[i]))
onenode(exp[i]);
else
{
switch(exp[i])
case '+':
case '-':
case '*':
case '/':
if(!exptree(&root,exp[i]))
return 1;
}
}
while(1)
{
printf("TRAVERSAL MENU:\n\t1.Preorder\n\t2.Inorder\n\t3.Postorder\n\t4.Exit\nChoice:");
scanf("%d",&ch);
if(ch==4)
break;
switch(ch)
{
case 1:
preorder(root);printf("\n");
break;
case 2:
inorder(root);printf("\n");
break;
case 3:
postorder(root);printf("\n");
break;
default:printf("Enter correct menu option!**!");
}
}
return 0;
}
No comments:
Post a Comment