Evaluation of Postfix Expression
Definition
Evaluation of postfix expression is the process of computing the result of an expression written in postfix form by scanning the tokens from left to right and using a stack to store operands until an operator is encountered.
In this method:
- operands are pushed onto the stack,
- when an operator appears, the required number of operands is popped,
- the operation is performed,
- the result is pushed back onto the stack.
At the end of the scan, the stack contains exactly one value, which is the final result of the postfix expression.
Main Content
1. Postfix Notation
- In postfix notation, every operator comes after its operands.
- The order of evaluation is clearly defined, so parentheses are not needed.
Examples:
- Infix:
A + B→ Postfix:AB+ - Infix:
(A + B) * C→ Postfix:AB+C* - Infix:
A + B * C→ Postfix:ABC*+
Key properties:
- No ambiguity in evaluation order.
- No need to remember operator precedence separately.
- Very suitable for stack-based processing.
Why postfix is useful:
- Computers can evaluate it more easily than infix.
- It is compact and unambiguous.
- It is commonly used in compilers, calculators, and interpreters.
For example, the infix expression:
(5 + 3) * 2
becomes postfix:
5 3 + 2 *
This means:
- compute
5 + 3, - then multiply the result by
2.
2. Stack-Based Evaluation
- A stack stores operands temporarily while the expression is being scanned.
- The stack follows the LIFO principle, which is ideal for handling nested or sequential operations.
How it works:
- If the current symbol is an operand, push it onto the stack.
- If the current symbol is an operator, pop the top two operands.
- Apply the operator to these operands.
- Push the result back onto the stack.
Important note:
- The first popped value is usually the second operand.
- The second popped value is the first operand.
Example:
For postfix expression 23*54*+
Step meaning:
2 3 *→2 * 3 = 65 4 *→5 * 4 = 206 + 20→26
Final result = 26
Stack behavior is essential because:
- it preserves intermediate results,
- it handles expressions of any length,
- it supports nested operations efficiently.
A small stack trace for 23*54*+:
| Symbol | Action | Stack |
|---|---|---|
| 2 | push | 2 |
| 3 | push | 2, 3 |
| * | pop 3, 2 → 2*3=6 | 6 |
| 5 | push | 6, 5 |
| 4 | push | 6, 5, 4 |
| * | pop 4, 5 → 5*4=20 | 6, 20 |
| + | pop 20, 6 → 6+20=26 | 26 |
3. Operator Handling and Operand Order
- The operator determines how many operands are removed from the stack.
- Most arithmetic operators in basic evaluation are binary operators, so they require two operands.
Binary operator rule:
- Pop the top element as
operand2 - Pop the next top element as
operand1 - Compute
operand1 operator operand2
This order is very important.
Example:
Postfix expression: 82-
- Push
8 - Push
2 - Encounter
- - Pop
2and8 - Compute
8 - 2 = 6 - Push
6
If the order is reversed incorrectly:
2 - 8 = -6, which is wrong
For a more complex example:
Postfix: 953+-
9 5 3 + -5 + 3 = 89 - 8 = 1
Result = 1
This concept is crucial because a postfix evaluator must:
- maintain correct operand sequence,
- support operators like
+,-,*,/,%,^, - avoid errors in calculation order.
Working / Process
1. Read the postfix expression from left to right
- Examine one symbol at a time.
- The symbol may be an operand, operator, or sometimes a multi-digit number depending on notation.
- Tokens are usually separated by spaces in practical implementations to avoid ambiguity.
2. Push operands onto the stack and evaluate when operator appears
- If the token is an operand, push it directly onto the stack.
- If the token is an operator, pop the top two values.
- Apply the operator in correct order.
- Push the result back onto the stack.
3. Continue until the expression ends and obtain the final answer
- After the whole expression is scanned, one value should remain in the stack.
- That value is the final result of the postfix evaluation.
- If more than one item remains, or if the stack underflows, the expression is invalid.
Example 1: Simple Expression
Postfix expression: 53+
Process:
- Push
5 - Push
3 - Encounter
+ - Pop
3and5 - Compute
5 + 3 = 8 - Push
8
Final result: 8
Example 2: Longer Expression
Postfix expression: 82/34*+
Process:
8→ push2→ push/→ pop2,8→8 / 2 = 4→ push43→ push4→ push*→ pop4,3→3 * 4 = 12→ push12+→ pop12,4→4 + 12 = 16→ push16
Final result: 16
Visual representation of stack usage for 53+
Before processing:
Stack: empty
After 5:
Stack: [5]
After 3:
Stack: [5, 3]
After +:
Stack: [8]
Final answer:
8
Algorithmic idea
A standard evaluation algorithm can be summarized as:
- initialize an empty stack,
- scan tokens one by one,
- push operands,
- on operator, pop two operands, compute, push result,
- final stack value is the answer.
Pseudocode
for each token in postfix expression:
if token is an operand:
push token onto stack
else if token is an operator:
operand2 = pop stack
operand1 = pop stack
result = operand1 token operand2
push result onto stack
answer = pop stack
Complexity
Time Complexity
O(n)because each token is processed once.
Space Complexity
O(n)in the worst case when many operands are pushed before operators appear.
Common errors
- Reversing operand order for subtraction or division.
- Forgetting to pop two operands for binary operators.
- Not handling invalid expressions.
- Ignoring token separation in multi-digit numbers.
Advantages / Applications
Simple and efficient evaluation
- Postfix expressions can be evaluated in a single left-to-right pass.
- No need to manage parentheses or precedence rules during evaluation.
Direct use of stacks
- The process naturally fits stack operations.
- This makes it one of the best examples of practical stack usage in programming.
Useful in compilers, calculators, and expression parsers
- Compilers often convert infix expressions to postfix for easier processing.
- Scientific calculators and interpreters may use postfix internally.
- It is also used in expression trees and virtual machine execution.
Additional applications:
- evaluation of arithmetic formulas,
- parsing in compiler design,
- implementation of postfix calculators,
- computer algebra systems,
- intermediate representation in programming languages.
Summary
- Postfix expression places the operator after the operands.
- Evaluation is done using a stack by scanning left to right.
- The correct order of operands is important, especially for
-and/. - Important terms to remember: postfix notation, stack, operand, operator, LIFO, Reverse Polish Notation