Question bank › A-level › A-level Computer Science › Fundamentals of algorithms

Tree traversal and reverse Polish notation

10 questions39 marksabout 45 min

Answer on paper first, then reveal the mark scheme one mark at a time.

1Standard5 marks

The binary tree below stores integers.

(a) Write the order in which the nodes are visited in a pre-order traversal. [1]

(b) Write the order in which the nodes are visited in an in-order traversal. [1]

(c) Write the order in which the nodes are visited in a post-order traversal. [1]

(d) The tree is a binary search tree. Explain how your answer to part (b) shows this. [2]

40256015325075
2Standard4 marks

(a) Convert the infix expression (3 + 4) * (8 - 2) to reverse Polish notation (RPN). [2]

(b) Evaluate the RPN expression 5 1 2 + 4 * + 3 -. Show the contents of the stack as you work. [2]

3Core3 marks

A program stores data in binary trees. For each task below, state which depth-first traversal (pre-order, in-order or post-order) is most appropriate.

(a) Making an exact duplicate of an existing tree, so that nodes are inserted into the new tree in the same structure. [1]

(b) Deleting every node of a tree so that no node is removed before its children. [1]

(c) Displaying the contents of a binary search tree of surnames in alphabetical order. [1]

Stuck on Tree traversal and reverse Polish notation?

Get it sorted in a free 30-minute session

We work through your weakest questions on this topic on Zoom. I reply the same day.

4Standard4 marks

The expression tree below represents an arithmetic expression.

(a) Write the output of a post-order traversal of the tree. [1]

(b) Evaluate your answer to part (a). [1]

(c) An in-order traversal of the same tree outputs 7 + 5 / 6 - 2 * 3. Explain why this output cannot be used as the expression without changes, and state the correct infix expression. [2]

*/3+−7562
5Standard3 marks

(a) Convert the reverse Polish notation (RPN) expression a b c - d * + into infix notation, using brackets only where they are needed. [2]

(b) Evaluate the expression when a = 10, b = 4, c = 1 and d = 2. [1]

6Hard5 marks

A stack is used to evaluate the RPN expression 9 3 / 4 2 - * 5 +. When an operand is read it is pushed onto the stack. When an operator is read, two values are popped, the operator is applied and the result is pushed.

(a) Show the contents of the stack immediately after each token is processed. Show the top of the stack on the right. [3]

(b) A programmer writes the evaluation as:

X ← POP()
Y ← POP()
PUSH(X operator Y)

Explain why this gives incorrect results for some operators, and state how to correct it. [2]

7Standard4 marks

The binary tree below is processed by the recursive procedure Traverse, which is first called with the root node.

PROCEDURE Traverse(Node)
  IF Node.Left ≠ Null THEN Traverse(Node.Left) ENDIF
  IF Node.Right ≠ Null THEN Traverse(Node.Right) ENDIF
  OUTPUT Node.Data
ENDPROCEDURE

(a) State the type of traversal carried out by Traverse. [1]

(b) Write the output produced when Traverse is called on the root node G. [2]

(c) State how the procedure should be changed so that it outputs the letters in alphabetical order. [1]

GCLAEDPMS
8Stretch4 marks

A binary tree (not a binary search tree) holds nine letters. Its traversals give:

Pre-order: R B Q F Z K N J C
In-order: Q B Z F R K J N C

(a) Draw the binary tree. [3]

(b) Write the output of a post-order traversal of the tree. [1]

9Core3 marks

Some interpreters, such as those for PostScript and many bytecode virtual machines, process expressions in reverse Polish notation (RPN) rather than infix notation.

Explain three reasons why RPN is used in these situations. [3]

10Hard4 marks

(a) Convert the infix expression (8 - 3) * 4 + 6 / 2 into reverse Polish notation. [2]

(b) A user enters the RPN expression 4 7 + 2 * -. Using a stack-based evaluation, explain why this expression is not valid. [2]

Stuck on this topic?

Book a free 30-minute session on WhatsApp and we'll find exactly which step is costing you the marks. Or build a timed paper on it.

Free 30-minute session