Skip to content
Worked Examples · Example 3.2

Q.Figure 3.4 shows the step-by-step process of evaluation of the postfix expression 7 8 2 * 4 / + using Algorithm 3.2.

Tamil Nadu DgeTextbookSubjective· 3mImportance★★★★★
15% · 2/13 Questions
🔒 Locked · start free trial →

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 →
Figure — A step-by-step evaluation table for the postfix expression 7 8 2 * 4 / + using a stack. Columns: Symbol scanned | Action | Stack (contents — Computer Science question
FigureA step-by-step evaluation table for the postfix expression 7 8 2 * 4 / + using a stack. Columns: Symbol scanned | Action | Stack (contents — Computer Science question

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)

  1. Initialize an empty stack.
  2. Scan the postfix expression from left to right, symbol by symbol.
  3. 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.
  4. After scanning all symbols, the stack contains exactly one element: the final answer. Pop and return it.
Watch out

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.

StepSymbolActionStack (bottom → top)
17Push operand7
28Push operand7, 8
32Push operand7, 8, 2
4*Pop 2 and 8; compute 8×2=168 \times 2 = 16; push 167, 16
54Push operand7, 16, 4
6/Pop 4 and 16; compute 16÷4=416 \div 4 = 4; push 47, 4
7+Pop 4 and 7; compute 7+4=117 + 4 = 11; push 1111
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.