Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q18 · 9618/33/O/N/24 Q11

Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2.

Q189618/33/O/N/24original Q11Structured · 12 marks
Question 11 from 9618/33/O/N/24

Worked solution

(a)

Each node has a left pointer, data and a right pointer. Add Yellow, Orange, Indigo and Violet and draw an arrow from each parent's correct pointer box to its child. Every node with no child uses -1. RootPtr points to Red.

NodeLeftPtrRightPtr
Redarrow to Greenarrow to Yellow
Greenarrow to Bluearrow to Orange
Yellowarrow to Violet-1
Blue-1-1
Orangearrow to Indigo-1
Indigo-1-1
Violet-1-1

Four additional nodes (Yellow, Orange, Indigo, Violet) with correct data [1]

Correct -1 null pointers in all added nodes and no extra ones where an arrow goes to a node [1]

Correct arrows joining each parent to its child [1]

All nodes in the correct places with nothing extra written in the pointer boxes [1]

(b)

Number the nodes by their index. A pointer holds the index of the child, and -1 means no child. FreePtr points to the first unused row, index 7.

IndexLeftPtrDataRightPtr
01Red2
13Green4
26Yellow-1
3-1Blue-1
45Orange-1
5-1Indigo-1
6-1Violet-1
7

Write 7 in FreePtr (row 7 stays blank).

Red and Green rows correct [1]

Yellow and Blue rows correct [1]

Orange, Indigo and Violet rows correct [1]

FreePtr = 7 with row 7 blank [1]

(c)

Start at the root. If the item is smaller than the node's data, go left; if larger, go right; if equal, return the index.

FUNCTION SearchTree(Item : STRING) RETURNS INTEGER
    NowPtr ← RootPtr
    WHILE NowPtr <> -1
        IF BinTree[NowPtr].Data > Item THEN
            NowPtr ← BinTree[NowPtr].LeftPtr
        ELSE
            IF BinTree[NowPtr].Data < Item THEN
                NowPtr ← BinTree[NowPtr].RightPtr
            ELSE
                RETURN NowPtr
            ENDIF
        ENDIF
    ENDWHILE
    RETURN NowPtr
ENDFUNCTION

RETURNS INTEGER [1]

NowPtr ← RootPtr [1]

BinTree[NowPtr].Data > Item [1]

NowPtr ← BinTree[NowPtr].RightPtr [1]

KEY IDEAIn a tree stored in an array, pointers are array indexes and -1 means no child; a search goes left or right by comparing data.
TOTAL 12 marks

Official mark scheme

Mark scheme for 9618/33/O/N/24 question 11
VIDEO
▶ Whole paper

Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.

Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.