You are on page 1of 5

RCPIT, Shirpur Department of Electronics and Telecommunication

Name of Student:

Expt No: 06 Roll No: Batch:

Title of Experiment: Wap to implement construction of expression tree using postfix

Date of Performance: Date of Submission:

Marks: Sign:

AIM: Implement construction of expression tree using postfix expression

THEORY:

An expression tree in data structure used to represent an expression in the form of a tree. After
generating the expression tree of an expression, we can perform inorder traversal to generate infix
expressions. Similarly, doing a postorder traversal of the expression tree will generate postfix
expressions.
In this, we are going to discuss the expression tree in data structure and how we can generate an
expression tree from a given expression using stacks.
Expression trees are used to express a mathematical expression in the form of a binary tree.
Expression trees are binary trees in which each internal (non-leaf) node is an operator and each
leaf node is an operand.
1. Let exprTree be the input expression tree.
2. Create a recursive function dfsTree(Node* node) to perform the dfs of the expression tree.
Here Node represents individual nodes of the tree.
3. If node is null or node represents an operand, return node’s value.
4. Otherwise, perform dfsTree(node->left) and dfsTree(node->right) and store them in L1 and
L2.
5. Finally, perform the operation represented by node->value between L1 and L2.
Construction of Expression Tree:
Now for constructing an expression tree we use a stack. We loop through input expression and
do the following for every character.

1. If a character is an operand push that into the stack

Data Structure & Algorithms Laboratory Page No.


RCPIT, Shirpur Department of Electronics and Telecommunication
2. If a character is an operator pop two values from the stack make them its child and push the
current node again.
In the end, the only element of the stack will be the root of an expression tree.

First, let us discuss how to evaluate a given expression tree in data structure. For example, if we
have an expression A * B + C / D. Then, the expression tree would be like the figure shown below:

Data Structure & Algorithms Laboratory Page No.


RCPIT, Shirpur Department of Electronics and Telecommunication
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>

struct node
{
struct node *right,*left,*prev;
char data;
}*cur,*par,*root=NULL;

void preorder(struct node *);


void postorder(struct node *);
void inorder(struct node *);
void main()
{
char a[100];
int len,i;
struct node *new_node;
clrscr();
printf("Enter Postfix Expression\n");
gets(a);
len = strlen(a);
for(i = len -1 ; i >= 0 ; i--)
{
new_node = (struct node *)malloc(sizeof(struct node));
new_node -> data = a[i];
new_node -> left = new_node -> right = new_node -> prev = NULL;

if(root == NULL)
{
root = new_node;
cur = par = root;
}
else
{
if(a[i] == '+' || a[i] == '-' || a[i] == '*' || a[i] == '/')
{
if(par -> right == NULL)
{
cur = new_node;
par -> right = cur;

cur -> prev = par;


par = cur;

Data Structure & Algorithms Laboratory Page No.


RCPIT, Shirpur Department of Electronics and Telecommunication

}
else if(par -> left == NULL)
{
cur = new_node;
par -> left = cur;
cur -> prev = par;
par = cur;
}
else
{
while(par -> left != NULL)
{
par = par -> prev;
}
cur = new_node;
par -> left = cur;
cur -> prev = par;
par = cur;
}
}
else
{
if(par -> right == NULL)
{
cur = new_node;
par -> right = cur;
cur -> prev = par;
}
else if(par -> left == NULL)
{
cur = new_node;
par -> left = cur;
cur -> prev = par;
}
else
{
while(par -> left != NULL)
{
par = par -> prev;
}
cur = new_node;
par -> left = cur;
cur -> prev = par;
}
}
}
}

Data Structure & Algorithms Laboratory Page No.


RCPIT, Shirpur Department of Electronics and Telecommunication
printf("\nInorder Traversal: \n");
inorder(root);
printf("\n\nPreorder Traversal: \n");
preorder(root);
printf("\n\nPostorder Traversal: \n");
postorder(root);
getch();
}

void inorder(struct node *root)


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

void preorder(struct node *root)


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

void postorder(struct node *root)


{
if(root != NULL)
{
postorder(root -> left);
postorder(root -> right);
printf("%c\t",root -> data);
}
}
CONCLUSION:

Data Structure & Algorithms Laboratory Page No.

You might also like