1.4 Conversion of Infix To Postfix
1.4 Conversion of Infix To Postfix
UNIT NO 1
II III
CS8391
DATA STRUCTURES (Common to CSE & IT)
CS8391
DATA STRUCTURES (Common to CSE & IT)
● We can easily distinguish the order of operators, and also can use the parenthesis
to solve that part first during solving mathematical expressions.
● The computer cannot differentiate the operators and parenthesis easily, that’s why
postfix conversion is needed.
● To convert infix expression to postfix expression, we will use the stack data
structure.
● By scanning the infix expression from left to right, when we will get any operand,
simply add them to the postfix form, and for the operator and parenthesis, add
them in the stack maintaining the precedence of them.
CS8391
DATA STRUCTURES (Common to CSE & IT)
2.Convert the input infix string to a list by using the string method split.
4.When the input expression has been completely processed, check the
opstack. Any operators still on the stack can be removed and appended to
the end of the output list.
CS8391
DATA STRUCTURES (Common to CSE & IT)
For example –
Infix notation: (2+4) * (4+6)
Post-fix notation: 2 4 + 4 6 + *
Result: 60
The stack operations for this expression evaluation is shown below:
CS8391
DATA STRUCTURES (Common to CSE & IT)
Example:Convert A * (B + C) * D to postfix notation.
1 A empty A
2 * * A
3 ( (* A
4 B (* AB
5 + +(* AB
6 C +(* ABC
7 ) * ABC+
8 * * ABC+*
9 D * ABC+*D
10 empty
CS8391
DATA STRUCTURES (Common to CSE & IT)
A+B*C/(E-F) A
A+B*C/(E-F) A +
A+B*C/(E-F) AB +
A+B*C/(E-F) AB +*
A+B*C/(E-F) ABC +*
A+B*C/(E-F) ABC* +/
A+B*C/(E-F) ABC*EF- +/
A+B*C/(E-F) ABC*EF-/+