What is Postfix expression?
IF THE OPERATOR SYMBOLS ARE PLACED AFTER ITS
OPERANDS , THEN THE EXPRESSION IS IN POSTFIX
Postfix expression is also known as Reverse Polish
Notation(RPN). In RPN the operators follow their operands.
Despite the name , Reverse Polish Notation is not exactly
the reverse of polish notation , for the operands they are
still written in conventional manner.
For eg : “*63”in polish notation and “63*” in reverse polish
In this , expression is different but the answer is same
Precedence of operators:
1. ‘$’ or ‘^’
2. ‘*’ and ‘/’
3. ’+’ and ‘-’
5. ‘(‘ and ‘)’
Fundamental principles followed while
evaluating the postfix expression :
1.Read the expression from left to right.
2.If there comes an operand , push it into the stack.
3.If there comes an operator , pop operand 1 and
operand 2 and then :
A . Push ‘(‘
B . Push ‘operand 2’
C . Push ‘operator’
D . Push ‘operand 1’
E . Push ‘)’
Advantages of Postfix
(A). Postfix notation is easier to work with. In a postfix
expression operators operands appear before the
operators, there is no need of operator precedence and
other rules. In postfix expression the topmost operands
are popped off and operator is applied and the result is
again pushed to the stack and thus finally the stack
contains a single value at the end of the process.
(B). In postfix expression, there are no parentheses
and therefore the order of evaluation will be
determined by the positions of the operators and
related operands in the expression.
Algorithm for Postfix Expression :
1.Read the element.
2.If element is an operand then:
Push the element into the stack.
3.If element is an operator then :
Pop two operands from the stack.
Evaluate expression formed by two operand and the
Push the result of expression in the stack end.
4.If no more elements then:
Pop the result
Goto step 1.
How to evaluate postfix (manually)
Going from left to right, if you see an operator, apply it to
the previous two operands (numbers)
Equivalent infix: A+ B * C / D– (E– F)
(E – F)