AQA 7517, section 4.3.3.1
Infix and Reverse Polish notation
In everyday maths we write an operator between its operands, as in . This is called infix notation. It needs brackets and precedence rules to avoid confusion: does mean 14 or 11?
In Reverse Polish notation (RPN), also called postfix, the operator comes after its operands:
| Infix | Reverse Polish |
|---|---|
3 4 + |
|
3 4 2 * + |
|
3 4 + 2 * |
Notice that the last two have different RPN forms without any brackets. The order of the operators alone shows the order of the calculation.
Why is RPN used?
- It eliminates the need for brackets in sub-expressions, and the need to remember precedence rules.
- It is in a form that is suitable for evaluation using a stack, which a computer can do in a single left-to-right pass.
- It is used in stack-based interpreters, such as PostScript and bytecode.
Evaluating RPN with a stack
Read the expression from left to right:
- If the item is a number, push it onto the stack.
- If the item is an operator, pop the top two numbers, apply the operator, and push the result back.
When the expression ends, the single value left on the stack is the answer.
Note the order: the first value popped is the right-hand operand. For subtraction and division this matters.
def eval_rpn(tokens):
stack = []
for t in tokens:
if t in "+-*/":
right = stack.pop()
left = stack.pop()
if t == "+": stack.append(left + right)
if t == "-": stack.append(left - right)
if t == "*": stack.append(left * right)
if t == "/": stack.append(left / right)
else:
stack.append(float(t))
return stack.pop()
print(eval_rpn("3 4 2 * +".split())) # 11.0
Trace of 3 4 2 * +
The answer is 11.
Converting infix to RPN
Method 1: using an expression tree
Draw the expression as a tree, with the operators at the branches, then read it off using a post-order traversal (left, right, root; see 4.3.2.1). For the post-order is 3 4 + 2 *.
Method 2: using a stack (the shunting-yard method)
Read the infix expression from left to right, building an output list and using a stack for operators:
- A number goes straight to the output.
- An operator: while the operator on top of the stack has higher or equal precedence, pop it to the output. Then push the new operator.
- A left bracket is pushed onto the stack.
- A right bracket: pop operators to the output until the left bracket is reached, then discard the brackets.
- At the end, pop everything left on the stack to the output.
Multiplication and division have higher precedence than addition and subtraction.
Example:
| Token | Output | Operator stack |
|---|---|---|
| 3 | 3 | |
+ |
3 | + |
| 4 | 3 4 | + |
* |
3 4 | +, * (higher than +, so just push) |
| 2 | 3 4 2 | +, * |
- |
3 4 2 * + | - (pop * and + first) |
| 1 | 3 4 2 * + 1 | - |
| end | 3 4 2 * + 1 - |
Result: 3 4 2 * + 1 -. Check by evaluating: , then , then , which matches the infix answer .
Converting RPN back to infix
Use the same stack idea, but push expressions. When you reach an operator, pop two expressions and combine them, adding brackets where needed.
Convert 5 1 2 + 4 * + 3 -:
| Item | Stack |
|---|---|
| 5 | 5 |
| 1 | 5, 1 |
| 2 | 5, 1, 2 |
+ |
5, (1 + 2) |
| 4 | 5, (1 + 2), 4 |
* |
5, ((1 + 2) * 4) |
+ |
(5 + ((1 + 2) * 4)) |
| 3 | (5 + ((1 + 2) * 4)), 3 |
- |
((5 + ((1 + 2) * 4)) - 3) |
The infix form is , which equals .
Worked example
Task: convert to RPN.
Using the tree method: the root is , its left child is the subtree for and its right child is the subtree for . Post-order visits left, right, root:
- left subtree:
a b + - right subtree:
c d - - root:
*
Result: a b + c d - *.
Exam tip: remember that operators take effect on the two items just before them. When evaluating, write the stack after every step. For subtraction and division, the second item popped is the left operand, so means , not .
Check yourself
Write in Reverse Polish notation
6 2 3 * +. The multiplication happens first, then the addition.
Evaluate 8 2 / 3 +
, then .
Give two reasons why RPN is used
It removes the need for brackets and precedence rules, and it is easy for a computer to evaluate using a stack. It is used in stack-based interpreters such as PostScript and bytecode.
Convert 4 5 + 2 * to infix
, which equals 18.