📄 Paper 1 · 4.3 Algorithms
⭐ Pro
4.3.3 Reverse Polish Notation (RPN)
AQA 7517 · A-Level Computer Science · ~18 min read

What is RPN?

Reverse Polish Notation (RPN), also called postfix notation, is a way of writing arithmetic expressions where the operator appears after its operands. It is contrasted with standard infix notation where the operator is between operands.

NotationNameExample
InfixStandard notation3 + 4
Prefix (Polish)Operator first+ 3 4
Postfix (RPN)Operator last3 4 +

Why Use RPN?

  • No brackets needed — precedence is unambiguous
  • No precedence rules — evaluated strictly left-to-right
  • Easy to evaluate with a stack — used in compilers and calculators

Converting Infix to RPN

Method 1: Expression Tree (Post-Order)

Build an expression tree from the infix expression, then perform post-order traversal.

// Infix: (3 + 4) × 2
// Expression tree (post-order traversal):
RPN: 3 4 + 2 ×

Method 2: Shunting-Yard Algorithm (Dijkstra)

// Uses: output queue + operator stack
// Rules:
// - Numbers → directly to output
// - Operators → push to stack (pop operators of >= precedence first)
// - '(' → push to stack
// - ')' → pop stack to output until '(' found

// Infix: 3 + 4 × 2
// Step by step:
// 3  → output: [3]
// +  → stack: [+]
// 4  → output: [3,4]
// ×  → × > + so push  stack: [+,×]
// 2  → output: [3,4,2]
// End→ pop stack: [3,4,2,×,+]
// RPN: 3 4 2 × +

Evaluating RPN with a Stack

// Algorithm:
// FOR each token in RPN expression:
//   IF token is a number THEN push to stack
//   IF token is an operator THEN:
//      Pop top two values (b then a)
//      Apply: result = a operator b
//      Push result back

// Example: 3 4 + 2 ×
// Token 3 → push: stack=[3]
// Token 4 → push: stack=[3,4]
// Token + → pop 4 and 3, push 7: stack=[7]
// Token 2 → push: stack=[7,2]
// Token × → pop 2 and 7, push 14: stack=[14]
// Result: 14  ✓  (same as (3+4)×2 = 14)

More RPN Examples

InfixRPN
5 + 35 3 +
5 + 3 × 25 3 2 × +
(5 + 3) × 25 3 + 2 ×
(4 − 2) × (3 + 1)4 2 − 3 1 + ×
8 ÷ 4 − 28 4 ÷ 2 −
Exam tip: AQA often asks you to (1) convert infix to RPN, (2) evaluate an RPN expression using a stack trace, or (3) state advantages of RPN. Key advantages: no brackets needed, no precedence rules, easy stack evaluation (used in compilers). Remember: pop two values — second popped is the left operand (a op b not b op a for non-commutative operations like subtraction).
Click through the slides at your own pace. Use arrow keys or click to advance.
Click slide or press arrow keys to navigate

Worksheet — 4.3.3 Reverse Polish Notation

8 questions · instantly marked · AQA 7517 standard

Q1What is Reverse Polish Notation? How does it differ from standard infix notation?[2]
✅ Mark scheme
Mark scheme
RPN (postfix notation) places the operator after the operands [1]; in standard infix notation the operator is between operands, whereas in RPN it comes after both operands [1].
Q2Give two advantages of RPN over infix notation.[2]
✅ Mark scheme
Mark scheme
No brackets are needed [1]; no operator precedence rules are required (evaluated left-to-right) / easy to evaluate using a stack [1].
Q3Convert the infix expression (8 + 2) × 5 to RPN.[2]
✅ Mark scheme
Mark scheme
8 2 + 5 × [2] (award 1 mark for correct operand order with minor error).
Q4Evaluate the RPN expression: 6 2 ÷ 4 + showing stack traces.[3]
✅ Mark scheme
Mark scheme
Push 6: [6]; Push 2: [6,2]; Pop 2 and 6, compute 6÷2=3, push 3: [3] [1]; Push 4: [3,4]; Pop 4 and 3, compute 3+4=7, push 7: [7] [1]; Final result: 7 [1].
Q5Convert 7 − 2 × 3 to RPN (note: × has higher precedence than −).[2]
✅ Mark scheme
Mark scheme
7 2 3 × − [2] (award 1 mark for partially correct conversion showing × applied before −).
Q6Evaluate the RPN expression: 5 3 + 2 × 4 −[3]
✅ Mark scheme
Mark scheme
Push 5, push 3; pop and compute 5+3=8 push 8 [1]; push 2; pop and compute 8×2=16 push 16 [1]; push 4; pop and compute 16−4=12 push 12; result: 12 [1].
Q7A student evaluates 8 4 − 2 × and gets 8. Explain the stack-based evaluation showing each step.[3]
✅ Mark scheme
Mark scheme
Push 8: [8]; push 4: [8,4]; pop 4 and 8, compute 8−4=4, push 4: [4] [1]; push 2: [4,2]; pop 2 and 4, compute 4×2=8, push 8: [8] [1]; result: 8 [1]. (Note: must pop second value as left operand for subtraction.)
Q8Which tree traversal produces RPN from an expression tree? Explain how this is linked to the stack-based evaluation algorithm.[2]
✅ Mark scheme
Mark scheme
Post-order traversal produces RPN [1]; both process operands before operators — post-order visits children (operands) before the parent (operator), matching the stack algorithm which pushes operands then applies the operator when encountered [1].
Topic Quiz
Question 1 of 15
You scored
out of 15
Card 1 of 8
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — RPN

10 questions · 10 minutes

← 4.3.2 Tree Traversal
21 of 70 · AQA 7517
4.3.4 Searching →