You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Reverse Polish Notation (RPN), also known as postfix notation, is a way of writing mathematical expressions without the need for parentheses or operator precedence rules. It is closely connected to how compilers evaluate expressions, and it relies on a stack for evaluation. Understanding RPN is essential for A-Level Theory of Computation.
| Notation | Name | Example (3 + 4 × 2) | Operator Position |
|---|---|---|---|
| Infix | Standard | 3 + 4 × 2 | Between operands |
| Prefix (Polish) | Prefix | + 3 × 4 2 | Before operands |
| Postfix (RPN) | Reverse Polish | 3 4 2 × + | After operands |
Infix is what humans use, but it requires:
RPN eliminates both problems. The order of operators and operands alone determines the evaluation.
Example: 3 + 4 × 2
Parse tree:
+
/ \
3 ×
/ \
4 2
Post-order traversal: 3, 4, 2, ×, + → 3 4 2 × +
This algorithm converts infix to postfix using a stack for operators:
| Token | Action | Output | Stack |
|---|---|---|---|
| ( | Push to stack | ( | |
| 3 | Output | 3 | ( |
| + | Push to stack | 3 | ( + |
| 4 | Output | 3 4 | ( + |
| ) | Pop until ( | 3 4 + | |
| × | Push to stack | 3 4 + | × |
| 2 | Output | 3 4 + 2 | × |
| End | Pop all | 3 4 + 2 × |
Result: 3 4 + 2 × (which evaluates to 14).
RPN expressions are evaluated left to right using a stack:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.