Track progress, take quizzes and save notes on this lesson.

Free forever · no card needed

Start free
Intermediate

Reverse Polish Notation

4.3.3 Reverse Polish Notation (RPN)

Aligned to the AQA 7517 specification

Level
Intermediate
Reading time
6 min
Published
13 June 2026
Updated
1 July 2026
On this page
  1. 1.Infix and Postfix Notation
  2. 2.Evaluating RPN with a Stack
  3. 3.A Longer Evaluation Trace
  4. 4.Converting Infix to RPN: Expression Trees
  5. 5.The Shunting-Yard Algorithm
  6. 6.Why RPN Matters
  7. 7.Common Exam Mistakes

Key takeaways

  • Reverse Polish Notation (RPN), also called postfix, places operators after their operands, so infix `3 + 4` becomes `3 4 +`.
  • RPN requires no brackets because operator precedence and grouping are encoded in the order of operands and operators, so `(3 + 4) * 2` becomes `3 4 + 2 *`.
  • RPN is evaluated with a stack: push operands, and for an operator pop two operands, apply it, and push the result; the final remaining value is the answer.
  • When evaluating `-` or `/`, the first pop is the right operand and the second pop is the left operand, so `5 3 -` means 5 − 3 = 2.
  • Converting infix to RPN can be done by building a binary expression tree and performing a post-order traversal (left, right, node).

Infix and Postfix Notation

Mathematical expressions can be written in three equivalent forms:

NotationFormExampleMeaning
Infixoperator between operands3 + 4the familiar form
Prefix (Polish)operator before operands+ 3 4operator first
Postfix (RPN)operator after operands3 4 +operator last

Reverse Polish Notation (RPN) — also called postfix notation — places operators after their operands.

More complex examples:

InfixRPN
3 + 43 4 +
5 - 35 3 -
3 + 4 * 23 4 2 * +
(3 + 4) * 23 4 + 2 *

Notice that RPN requires no brackets — the order of operations is unambiguous from the position of operators. This is why compilers and calculators use it internally.

Evaluating RPN with a Stack

RPN expressions are evaluated using a stack:

  1. Read the expression left to right
  2. If the token is an operand (number): push it onto the stack
  3. If the token is an operator (+, -, *, /): pop two operands, apply the operator, push the result
  4. When the expression ends, the single remaining stack value is the result

Worked example — evaluate 3 4 2 * +:

TokenActionStack
3Push[3]
4Push[3, 4]
2Push[3, 4, 2]
*Pop 2 and 4; compute 4 × 2 = 8; push 8[3, 8]
+Pop 8 and 3; compute 3 + 8 = 11; push 11[11]

Result: 11 (matches infix 3 + 4 * 2 = 3 + 8 = 11).

Note the pop order: the first pop gives the right operand, the second pop gives the left operand. For - and / this matters: 5 3 - means 5 − 3 = 2, not 3 − 5.

A Longer Evaluation Trace

Evaluate 6 2 / 3 4 * +:

TokenActionStack
6Push[6]
2Push[6, 2]
/Pop 2 and 6; compute 6 ÷ 2 = 3; push 3[3]
3Push[3, 3]
4Push[3, 3, 4]
*Pop 4 and 3; compute 3 × 4 = 12; push 12[3, 12]
+Pop 12 and 3; compute 3 + 12 = 15; push 15[15]

Result: 15 (matches infix 6 / 2 + 3 * 4 = 3 + 12 = 15).

The pseudocode for an RPN evaluator:

FUNCTION evalRPN(tokens)
    stack ← empty stack
    FOR each token IN tokens
        IF token is a number THEN
            stack.push(token)
        ELSE
            right ← stack.pop()
            left  ← stack.pop()
            IF token = "+" THEN stack.push(left + right)
            ELSE IF token = "-" THEN stack.push(left - right)
            ELSE IF token = "*" THEN stack.push(left * right)
            ELSE IF token = "/" THEN stack.push(left / right)
            ENDIF
        ENDIF
    NEXT
    RETURN stack.pop()
ENDFUNCTION

Converting Infix to RPN: Expression Trees

One way to convert infix to RPN is to build a binary expression tree then do a post-order traversal (left, right, node).

Example — convert 3 + 4 * 2 to RPN:

Build the expression tree (respecting operator precedence; * binds tighter than +):

Post-order traversal (left → right → node):

  1. Visit left of +: output 3
  2. Visit right of + (the * subtree):
    • Visit left of *: output 4
    • Visit right of *: output 2
    • Visit node *: output *
  3. Visit node +: output +

Result: 3 4 2 * +

For (3 + 4) * 2, the tree changes because + is now the lower node:

Post-order: 3 4 + 2 * — the brackets are encoded in the tree structure, not in the RPN.

Studying this for an exam?

Generate a personalised learning path for this subject. Free to get started.

Create a learning path

The Shunting-Yard Algorithm

The Shunting-Yard algorithm (Dijkstra, 1961) converts infix to RPN directly using a stack and an output queue, without building a full expression tree.

AQA requires awareness of the algorithm — you need to know it exists and what it does, not implement it step by step.

Key rules (simplified):

  • Numbers go directly to output
  • Operators are pushed onto a stack, popped to output when a lower-precedence operator arrives or when ) is reached
  • Brackets control the order: ( is pushed; ) triggers popping until ( is found

This is used internally in compilers to generate code for arithmetic expressions — the compiler converts infix source code to RPN (or equivalent bytecode) for efficient stack-based evaluation.

Why RPN Matters

RPN is not just an academic curiosity — it is used in real systems:

  • Compilers: convert infix arithmetic to stack-based bytecode (e.g. Java's JVM uses a stack-based instruction set)
  • Calculators: Hewlett-Packard's RPN calculators (HP-12C still in use by financial professionals)
  • PostScript: the page-description language (used by printers) is an RPN-based stack language

The connection to computer science concepts:

  • Evaluation uses a stack (see stacks and queues)
  • Conversion uses tree traversal (post-order on a binary expression tree)
  • Operator precedence is implicit in tree structure, not in bracket syntax

Common Exam Mistakes

1. Wrong pop order for subtraction and division

When evaluating - or /, the first pop gives the right operand and the second pop gives the left operand. 5 3 - means 5 − 3 = 2, not 3 − 5 = −2. Getting this backwards loses the mark.

2. Leaving multiple values on the stack

A valid RPN expression always evaluates to exactly one remaining value. If your trace ends with more than one value on the stack, the expression is malformed or you missed an operator.

3. Forgetting that brackets disappear in RPN

RPN contains no brackets. Operator precedence and grouping are encoded in the order of operands and operators. (3 + 4) * 2 becomes 3 4 + 2 *, not 3 + 4 * 2.

4. Confusing pre-order and post-order traversal

Converting infix to RPN requires post-order (left, right, node). Pre-order (node, left, right) gives prefix/Polish notation. Using the wrong traversal produces the wrong output.

Key terms

Infix notation
Notation that places the operator between its operands, such as `3 + 4`, the familiar everyday form.
Prefix (Polish) notation
Notation that places the operator before its operands, such as `+ 3 4`.
Postfix (Reverse Polish) notation
Notation that places the operator after its operands, such as `3 4 +`, requiring no brackets.
Stack
The data structure used to evaluate RPN: operands are pushed, and operators pop two operands and push the result.
Expression tree
A binary tree representing an expression, where a post-order traversal yields the RPN form and precedence is encoded in the structure.
Post-order traversal
A tree traversal visiting left subtree, then right subtree, then the node, used to convert an expression tree to RPN.
Shunting-Yard algorithm
Dijkstra's 1961 algorithm that converts infix to RPN directly using a stack and an output queue without building a full expression tree.

Frequently asked questions

Read the expression left to right. Push each operand (number) onto the stack. For each operator, pop two operands, apply the operator, and push the result. When the expression ends, the single remaining stack value is the result.

Because the first value popped is the right operand and the second is the left operand. So `5 3 -` means 5 − 3 = 2, not 3 − 5. Getting this backwards loses the mark for `-` and `/`.

Build a binary expression tree respecting operator precedence, then perform a post-order traversal (left, right, node). For example `3 + 4 * 2` gives the tree post-order output `3 4 2 * +`. Pre-order traversal would instead give prefix (Polish) notation.

Generate revision on any topic you study

Type any topic you're studying and Aicademy generates a complete lesson, quiz, and flashcard set, personalised to your level.

Lessons on anything

Structured, level-matched lessons on any topic you study

Practice quizzes

Find out what you actually know before the exam does

Flashcard sets

Lock in key concepts with instant revision cards

Ask Aica

Stuck on something? Get a clear explanation, any time

Prev

Graph Traversal: BFS and DFS

Next

Searching Algorithms

Related lessons

9 min

Lesson

Stacks and Queues

A-Level Computer Science · AQA 7517

2 months ago

8 min

Lesson

Binary Search Trees

A-Level Computer Science · AQA 7517

1 month ago

Top students don’t revise more. They revise what counts.

Start revising free

Free to start. No card needed.