Revision Notes
AQA A-Level Computer Science (7517): Reverse Polish – Revision Notes
Condensed AQA 7517 section 4.3.3 recall: infix to Reverse Polish conversion both ways, evaluating with a stack, why RPN is used and a self-test.
- Subject
- Computer Science
- Level
- A LEVELS
- Topic
- Fundamentals of algorithms
- Author
- Marlbridge Academic Team
- Updated
- Reviewed by
- Harris Khan (what this means)
Aligned to AQA A Level Computer Science (7517), 2015-onwards. Official specification .
Syllabus page (what it covers and how it is assessed): AQA A Level Computer Science.
Syllabus points this page covers
7517
- 4.3.3 Reverse Polish
This page covers 1 of the 6 subtopics in topic 3, Fundamentals of algorithms.
Found an error? Report a correction.
Need help with this topic? Request a free trial class for A Level Computer Science (7517).
Condensed recall for 4.3.3 Reverse Polish, AQA A-Level Computer Science (7517): convert simple infix expressions to Reverse Polish notation (RPN) and back, and know why and where RPN is used. Methods and long worked examples are in the Reverse Polish study guide; exam-style questions are in the Reverse Polish practice questions.
The three notations
| Name | Operator position | 3 + 4 written as |
|---|---|---|
| Infix | between operands | 3 + 4 |
| Prefix | before operands | + 3 4 |
| Postfix (RPN) | after operands | 3 4 + |
In RPN the order of the operators in the string is the order of calculation. No brackets and no precedence rules are needed.
Why and where it is used
- Eliminates brackets in sub-expressions.
- Suitable for evaluation using a stack: one left-to-right pass, no backtracking.
- Used in stack-based interpreters, for example PostScript, and in bytecode.
- Easy to produce from an expression tree by post-order traversal.
Evaluate RPN with a stack
- Operand: push.
- Operator: pop two, apply, push the result.
- The single value left at the end is the answer.
Operand order: the value popped first is the right operand. For 9 3 - the sum is 9 − 3 = 6.
Worked check: 7 2 3 * + 4 -
| Read | Stack after |
|---|---|
| 7, 2, 3 | 7, 2, 3 |
* |
7, 6 |
+ |
13 |
| 4 | 13, 4 |
- |
9 |
Answer 9; infix form 7 + 2 × 3 − 4.
Infix to RPN: three methods
Tree method. The operator done last is the root. Build subtrees for each operand. Read the tree in post-order: left, right, operator.
Inside-out method. Rewrite each innermost operation as operand operand operator, treat it as one operand and repeat. a + b × c: first b c *, then a b c * +.
Stack (shunting-yard) method. Operands go straight to the output. For an operator, pop operators of higher or equal precedence to the output, then push it. Push (. At ), pop to the output until (, and discard the pair. At the end pop everything.
Worked conversions
| Infix | RPN |
|---|---|
| (3 + 4) × 5 | 3 4 + 5 * |
| a + b × c | a b c * + |
| (a + b) × (c − d) | a b + c d - * |
| 8 − 3 − 2 | 8 3 - 2 - |
| a × b + c × d | a b * c d * + |
| (a + b) × c − d / e | a b + c * d e / - |
| a − (b − c) | a b c - - |
| a × (b + c) × d | a b c + * d * |
Note that 8 − 3 − 2 groups from the left, but a − (b − c) has brackets that force the right-hand grouping.
Conversion trace using the stack method
Convert (a + b) * c - d. The output list grows from the left and the operator stack has its top on the right.
| Item read | Output | Operator stack |
|---|---|---|
( |
empty | ( |
| a | a | ( |
+ |
a | ( + |
| b | a b | ( + |
) |
a b + | empty |
* |
a b + | * |
| c | a b + c | * |
- |
a b + c * | - (the * has higher precedence, so it leaves first) |
| d | a b + c * d | - |
| end | a b + c * d - | empty |
Check by substituting numbers. With a = 2, b = 3, c = 4 and d = 5 the infix value is (2 + 3) * 4 - 5 = 15, and 2 3 + 4 * 5 - gives 5, then 20, then 15. The two agree.
RPN to infix
Use a stack of strings. Operand: push. Operator: pop two, join as (left operator right), push.
6 2 / 3 + 4 5 * - gives (((6 / 2) + 3) − (4 × 5)), which is 6 − 20 = −14. Remove brackets that are not needed: 6 / 2 + 3 − 4 × 5.
Precedence reminder
- Brackets
- Multiplication and division
- Addition and subtraction
Equal precedence: group from the left.
Exam traps
- Swapped operands for
-and/when evaluating. - Reordered operands when converting. Only the operators move.
- Wrong precedence: a + b × c is not
a b + c *. - Brackets in the answer. RPN has none.
- More than one value left on the stack at the end: something was missed.
- No working shown: use a table with item read and stack after.
Key terms
| Term | Definition |
|---|---|
| Infix | Operator written between its two operands |
| Prefix (Polish) | Operator written before its operands |
| Postfix (Reverse Polish) | Operator written after its operands |
| Operand | A value or variable that an operator acts on |
| Operator | A symbol such as +, −, × or / |
| Precedence | The rule that decides which operator is applied first |
| Stack | Last in, first out data structure used to hold operands |
| Expression tree | A tree whose internal nodes are operators and leaves are operands |
Self-test
- Convert
4 + 5to RPN. - Convert 2 × (3 + 4) to RPN.
- Convert 10 − 4 − 3 to RPN.
- Evaluate
5 1 2 + 4 * + 3 -. - Evaluate
20 4 / 3 -. - State two advantages of RPN.
- Name one place where RPN is used.
- Which tree traversal produces RPN from an expression tree?
- Convert
a b c * +to infix. - When evaluating
8 2 -, which value is popped first and what is it?
Answers
4 5 +2 3 4 + *10 4 - 3 -- 5, then 1 2 + gives 3, 3 × 4 = 12, 5 + 12 = 17, 17 − 3 = 14. The answer is 14.
- 20 / 4 = 5, then 5 − 3 = 2. The answer is 2.
- Any two: no brackets needed; no precedence rules needed; easy to evaluate with a stack in one pass.
- Any one: stack-based interpreters such as PostScript; bytecode.
- Post-order.
- a + (b × c), or a + b × c.
- 2 is popped first (it is the right operand), then 8, so the calculation is 8 − 2 = 6.
More resources on this topic
Related resources
-
Study guides
AQA A-Level Computer Science (7517): Searching algorithms
Deeper guide to AQA 7517 section 4.3.4 Searching algorithms: tracing and analysing linear search, binary search and binary tree search with their time complexity.
Computer Science · AQA · A Level
Read guide -
Practice questions
AQA A-Level Computer Science (7517): Searching algorithms – Practice Questions
Original AQA 7517 section 4.3.4 questions with answers: tracing and analysing linear search, binary search and binary tree search.
Computer Science · AQA · A Level
Try questions -
Revision notes
AQA A-Level Computer Science (7517): Searching algorithms – Revision Notes
Condensed AQA 7517 section 4.3.4 recall: linear, binary and binary tree search, how to trace them, their time complexity and a self-test.
Computer Science · AQA · A Level
Revise topic
Get free revision emails (optional)
Occasional emails with practice questions, worked explanations and links to free resources for the qualification and subjects you choose. No spam, and you can unsubscribe from any email. The free tools on this site never need an email.