Q.Figure 3.4 shows the step-by-step process of evaluation of the postfix expression 7 8 2 * 4 / + using Algorithm 3.2.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →The postfix expression 7 8 2 * 4 / + evaluates to 11 by scanning left-to-right, pushing operands onto a stack and applying operators to the top two stack elements.
Why Postfix (and a Stack)?
Postfix notation—also called Reverse Polish Notation—places every operator after its operands. The beauty of postfix is that it needs no parentheses and no precedence rules: you simply scan left to right, and whenever you see an operator you know exactly which two values it acts on (the two most recent operands). A stack is the natural data structure because it gives you LIFO access: the last two numbers pushed are the first two popped when an operator arrives.
Infix (7 + ((8 * 2) / 4)) becomes postfix 7 8 2 * 4 / +. The postfix form encodes the same tree of operations but in an order a machine can execute in one linear pass.
Algorithm 3.2 (Postfix Evaluation)
- Initialize an empty stack.
- Scan the postfix expression from left to right, symbol by symbol.
- For each symbol:
- If it is an operand (a number), push it onto the stack.
- If it is an operator (
+,-,*,/, etc.):- Pop the top element → call it
B(the second operand). - Pop the next top element → call it
A(the first operand). - Compute
A operator B. - Push the result back onto the stack.
- Pop the top element → call it
- After scanning all symbols, the stack contains exactly one element: the final answer. Pop and return it.
The order of operands matters for non-commutative operators. When you pop twice, the first pop is the right operand and the second pop is the left operand. For division A / B, if you reverse them you get B / A, which is wrong.
Step-by-Step Trace of 7 8 2 * 4 / +
We process each symbol in turn. The table below shows the action taken and the stack contents after that action.
| Step | Symbol | Action | Stack (bottom → top) |
|---|---|---|---|
| 1 | 7 | Push operand | 7 |
| 2 | 8 | Push operand | 7, 8 |
| 3 | 2 | Push operand | 7, 8, 2 |
| 4 | * | Pop 2 and 8; compute ; push 16 | 7, 16 |
| 5 | 4 | Push operand | 7, 16, 4 |
| 6 | / | Pop 4 and 16; compute ; push 4 | 7, 4 |
| 7 | + | Pop 4 and 7; compute ; push 11 | 11 |
| 8 | (end) | Pop final result | — |
The single remaining element is 11.
Python Implementation
Below is a complete function that implements Algorithm 3.2. It assumes the postfix expression is given as a string of space-separated tokens.
def evaluate_postfix(expression):
"""
Evaluate a postfix expression given as a space-separated string.
Returns the numeric result.
"""
stack = []
tokens = expression.split()
for token in tokens:
if token in {'+', '-', '*', '/'}:
# Pop two operands (order matters!)
b = stack.pop() # right operand
a = stack.pop() # left operand
# Apply the operator
if token == '+':
result = a + b
elif token == '-':
result = a - b
elif token == '*':
result = a * b
elif token == '/':
result = a / b # use // for integer division if needed
stack.append(result)
else:
# It's an operand; convert to number and push
stack.append(float(token)) …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.