0% found this document useful (0 votes)
90 views4 pages

Implementation of Expresssion Tree: Program Coding

The document describes a C program that implements an expression tree for postfix expressions. The program takes a postfix expression as input, converts it to a tree data structure, and then traverses the tree to output the expression in infix, prefix, and postfix notations. Key functions include conversion() to build the tree from postfix, and infix(), prefix(), and postfix() functions to traverse the tree and output the expression in the different notations. The output shows the program taking a sample postfix expression "ab+cde+**" as input and correctly generating the equivalent infix, prefix, and postfix forms.

Uploaded by

suse2037
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
90 views4 pages

Implementation of Expresssion Tree: Program Coding

The document describes a C program that implements an expression tree for postfix expressions. The program takes a postfix expression as input, converts it to a tree data structure, and then traverses the tree to output the expression in infix, prefix, and postfix notations. Key functions include conversion() to build the tree from postfix, and infix(), prefix(), and postfix() functions to traverse the tree and output the expression in the different notations. The output shows the program taking a sample postfix expression "ab+cde+**" as input and correctly generating the equivalent infix, prefix, and postfix forms.

Uploaded by

suse2037
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

NAME:R.

DIVYA
[Link].
DATE:

Implementation Of Expresssion Tree

Program Coding:

#include<stdio.h>

#define operand(x)(x>='a' && x<='z')

typedef struct node *tree;

struct node
{
char data;
tree left,right;
}*t=NULL;

tree stack[20];
char postf[20];
int top=-1;

void conversion();
void infix(tree);
void prefix(tree);
void postfix(tree);
void push(tree);
tree pop();

main()
{

printf("\nEnter the postfix expression:\t");


scanf("%s",postf);
conversion();

printf("\nThe tree traversal:");


printf("\n Infix Traversal:\n");
infix(t);

printf("\n Prefix Traversal:\n");


prefix(t);

printf("\n Postfix Traversal:\n");


postfix(t);

void conversion()
{

int i;
NAME:[Link].
[Link].
char x;
tree a,b,c;
for(i=0;postf[i]!='#';i++)
{
x=postf[i];
if(operand(x))
{
a=(tree)malloc(sizeof(struct node));
a->data=x;
a->left=NULL;
a->right=NULL;
push(a);
}
else
{
a=pop();
b=pop();
c=(tree)malloc(sizeof(struct node));
c->data=x;
c->left=b;
c->right=a;
push(c);
}
}
t=stack[top];

void push(tree a)
{

top++;
stack[top]=a;

tree pop()
{

tree a;
if(top==-1)
printf("\nThe Stack is empty");
else
{
a=stack[top];
top--;
}
return(a);

void infix(tree t)
{
NAME:[Link].
[Link].
if(t!=NULL)
{
infix(t->left);
printf("%c",t->data);
infix(t->right);
}

void prefix(tree t)
{

if(t!=NULL)
{
printf("%c",t->data);
prefix(t->left);
prefix(t->right);
}

void postfix(tree t)
{

if(t!=NULL)
{
postfix(t->left);
postfix(t->right);
printf("%c",t->data);
}

}
NAME:[Link].
[Link]

Output:

Enter the postfix expression: ab+cde+**#

The tree traversal:

Infix Traversal:

a+b*c*d+e

Prefix Traversal:

*+ab*c+de

Postfix Traversal:

ab+cde+**

You might also like