Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms

Q17 · 9618/32/O/N/24 Q11

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

Q179618/32/O/N/24original Q11Structured · 12 marks
Question 11 from 9618/32/O/N/24

Worked solution

(a)

25 is the root. 4 and 36 are its children; 1 and 16 are children of 4; 9 is the left child of 16; 64 is the right child of 36 and 49 is the left child of 64. Use -1 for every empty pointer.

NodeLeftPtrRightPtr
25arrow to 4arrow to 36
4arrow to 1arrow to 16
36-1arrow to 64
1-1-1
16arrow to 9-1
64arrow to 49-1
9-1-1
49-1-1

Five additional nodes (16, 36, 64, 9, 49) with correct data [1]

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

Correct arrows from the correct left or right pointer box to each child [1]

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

(b)

Use the index of each child as the pointer. FreePtr points to the first unused row, index 8.

IndexLeftPtrDataRightPtr
01252
1344
2-1365
3-11-1
4616-1
5764-1
6-19-1
7-149-1
8

Write 8 in FreePtr (row 8 stays blank).

All eight filled rows correct [3]

Six or seven correct rows score 2 of those marks; three to five correct rows score 1.

FreePtr = 8 with the last row blank [1]

(c)

Go right if the node's data is smaller than Item, go left if it is larger, otherwise return the index.

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

RETURNS INTEGER [1]

NowPtr ← RootPtr [1]

LinkList[NowPtr].Data > Item [1]

NowPtr ← LinkList[NowPtr].LeftPtr [1]

KEY IDEAA binary tree in an array uses indexes as pointers, with -1 as null and the first unused row held in FreePtr.
TOTAL 12 marks

Official mark scheme

Mark scheme for 9618/32/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.