DSA Notes
Master expression notation conversion using stacks — infix to postfix (Shunting Yard algorithm), infix to prefix, postfix evaluation, prefix evaluation, why postfix and prefix are easier to evaluate than infix, operator precedence handling, and complete traced examples.
Three Ways to Write an Expression
Every mathematical expression involving binary operators can be written in three notations based on where the operator is placed relative to its operands.
| Expression | A + B × C |
| Infix | A + B * C (operator between operands — what humans use) |
| Prefix | + A * B C (operator before operands — Polish notation) |
| Postfix | A B C * + (operator after operands — Reverse Polish Notation) |
Why bother? Infix notation requires parentheses and operator precedence rules. Prefix and postfix are unambiguous without parentheses and are trivially evaluated with a stack.
Algorithm 1 — Infix to Postfix (Shunting Yard)
**Step-by-step trace for A + B * C:**
| Token A | output=[A], stack=[] |
| Token + | output=[A], stack=[+] |
| Token B | output=[A,B], stack=[+] |
| Token *: * > + (higher precedence) | push, output=[A,B], stack=[+,*] |
| Token C | output=[A,B,C], stack=[+,*] |
| End: pop all | output=[A,B,C,*,+] |
| Result | "A B C * +" ✓ |
Algorithm 2 — Infix to Prefix
Algorithm 3 — Evaluate Postfix Expression
Why postfix is easy to evaluate:
- No parentheses needed
- No precedence rules needed
- Just a simple left-to-right scan with a stack
- When you see an operator, you already have both operands on the stack
Algorithm 4 — Evaluate Prefix Expression
Comparison of Three Notations
| Feature | Infix | Prefix | Postfix |
|---|---|---|---|
| Operator position | Between operands | Before operands | After operands |
| Human readability | ✅ Natural | ❌ Unusual | ❌ Unusual |
| Parentheses needed | ✅ Sometimes | ❌ Never | ❌ Never |
| Stack evaluation | Complex | Simple (right-to-left) | Simple (left-to-right) |
| Compiler use | Input format | AST representation | Code generation |
| Calculator use | Input | — | RPN calculators (HP) |
Real-World Applications
| Compilers: parse infix source | build AST (prefix-like) → generate postfix bytecode |
| Python bytecode | 3 + 4 * 5 compiles to roughly: |
| HP Scientific Calculators | use RPN (postfix) input |
| Traditional | press "3 + 4 =" |
| RPN | press "3 ENTER 4 +" ← no need for = key |
Summary
- Infix:
A + B— natural for humans, needs precedence/parentheses rules - Prefix:
+ A B— operator first, evaluate right-to-left with stack - Postfix:
A B +— operator last, evaluate left-to-right with stack - Shunting Yard converts infix → postfix in O(n) using an operator stack
- Postfix evaluation is the simplest: push operands, operators trigger pops and compute
*Next: Strings section*
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Infix, Prefix, and Postfix Expressions — Stack-Based Conversion and Evaluation.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, stacks, infix, prefix
Related Data Structures & Algorithms Topics