9618/32/O/N/24 question 11: worked solution (October/November 2024, paper 32)

Show as plain text

Cambridge A2 Level Computer Science 9618 · 19.1 Algorithms Worked solution from the Computational thinking and Problem-solving Paper 3 Advanced Theory booklet, Part 1 of 2. Q17 9618/32/O/N/24 original Q11 Structured · 12 marks 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. Node LeftPtr RightPtr 25 arrow to 4 arrow to 36 4 arrow to 1 arrow to 16 36 -1 arrow to 64 1 -1 -1 16 arrow to 9 -1 64 arrow 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. Index LeftPtr Data RightPtr 0 1 25 2 1 3 4 4 2 -1 36 5 3 -1 1 -1 4 6 16 -1 5 7 64 -1 6 -1 9 -1 7 -1 49 -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 IDEA A 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 VIDEO ▶ Whole paper Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.