Question bank › A-level › A-level Computer Science › Fundamentals of data structures

Trees and binary trees

10 questions43 marksabout 50 min

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

1Hard5 marks

A binary search tree is stored in an array of records. Each record has a Left pointer, a Data field and a Right pointer. A pointer value of −1 means there is no child. Items are inserted using alphabetical order: an item that comes before a node's data goes to the left of that node and an item that comes after it goes to the right. The items are inserted in the following order, with Mango as the root at index 0 and each later item stored at the next index:

Mango, Cherry, Peach, Apple, Fig, Plum, Banana, Kiwi

(a) Complete the Left and Right pointers in the table. [3]

IndexLeftDataRight
0Mango
1Cherry
2Peach
3Apple
4Fig
5Plum
6Banana
7Kiwi

(b) Describe how the tree would be searched to determine whether Grape is stored in it, stating the items compared. [2]

2Core5 marks

Trees are a fundamental data structure.

(a) Define what is meant by a tree in terms of graphs. [2]

(b) Explain the difference between a tree and a rooted tree. [1]

(c) State what makes a rooted tree a binary tree. [1]

(d) Give one typical use of a binary tree. [1]

3Standard3 marks

Figure 1 shows three graphs, P, Q and R.

For each graph, state whether or not it is a tree and give a reason for your answer. [3]

1234512345123456Graph PGraph QGraph R
Stuck on Trees and binary trees?

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.

4Core4 marks

An operating system stores its files and folders in a hierarchical directory structure, shown in Figure 1. The structure can be represented as a rooted tree.

(a) State the label of the root node. [1]

(b) List all of the leaf nodes. [1]

(c) Explain why this structure is not a binary tree. [1]

(d) The tree has 9 nodes. State the number of edges in the tree and explain why. [1]

/homeetcvaralicebobdocspicslog
5Standard4 marks

Figure 1 shows a binary search tree. Each letter was inserted so that letters earlier in the alphabet are placed to the left of a node and later letters to the right.

(a) Write down the order in which the nodes are visited in:

(i) a pre-order traversal [1]

(ii) an in-order traversal [1]

(iii) a post-order traversal. [1]

(b) State which of these traversals would be used to produce a copy of the tree, with the same shape, by inserting the nodes into an empty tree in the order visited. [1]

MFTBHGRW
6Standard4 marks

Figure 1 shows a binary tree used to represent an arithmetic expression.

(a) Write the expression represented by the tree in standard infix form, using brackets where needed. [1]

(b) State the type of traversal that will output this expression in Reverse Polish Notation (RPN) and write down the RPN expression. [1]

(c) Given that A = 3, B = 5, C = 10, D = 12 and E = 4, evaluate the expression. Show the contents of the stack after each operator in the RPN expression is applied. [2]

*+−ABC/DE
7Hard5 marks

The integers 15, 22, 31, 47, 58, 63 and 79 are to be stored in a binary search tree. Smaller values go to the left of a node and larger values to the right.

(a) The values are inserted in ascending order, starting with 15. Describe the shape of the resulting tree. [1]

(b) State how many comparisons are needed to find 79 in the tree from part (a). [1]

(c) State an order in which the same seven values could be inserted so that any value can be found using no more than 3 comparisons. [2]

(d) Using your answers, explain how the order of insertion affects the time complexity of searching a binary search tree containing n items. [1]

8Hard5 marks

A binary tree of integers is stored using three arrays, Left, Data and Right. A pointer value of −1 means there is no child. The root is at index 0.

IndexLeftDataRight
01502
13304
2−1705
3−120−1
4640−1
5−185−1
6−135−1
SUBROUTINE Show(p)
  IF p ≠ -1 THEN
    Show(Right[p])
    OUTPUT Data[p]
    Show(Left[p])
  ENDIF
ENDSUBROUTINE

SUBROUTINE Leaves(p)
  IF p = -1 THEN
    RETURN 0
  ENDIF
  IF Left[p] = -1 AND Right[p] = -1 THEN
    RETURN 1
  ENDIF
  RETURN Leaves(Left[p]) + Leaves(Right[p])
ENDSUBROUTINE

(a) State the output when Show(0) is called. [2]

(b) Describe the purpose of the subroutine Show when it is used on a binary search tree. [1]

(c) State the value returned by Leaves(0). [1]

(d) Explain why recursion is well suited to processing a tree. [1]

9Hard4 marks

A binary tree is said to have h levels if the longest path from the root to a leaf contains h nodes.

(a) Calculate the maximum number of nodes that a binary tree with 5 levels can hold. [1]

(b) A binary search tree is to store 1000 different product codes. Calculate the minimum number of levels the tree must have. Show your working. [2]

(c) State the maximum number of comparisons needed to search for a product code if the 1000 codes were inserted into the tree in sorted order. [1]

10Stretch4 marks

A binary tree contains 11 nodes, each with a different letter. It is not a binary search tree. Two traversals of the tree give:

Pre-order: K, D, B, A, F, E, H, R, P, M, S

In-order: A, B, D, E, F, H, K, P, M, R, S

(a) Draw the binary tree. [3]

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

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