Past-paper question · 2022 October-November · Paper 43 · Q3(c)

Question 3(c) 2022 · October-November · Paper 43 · 5 marks

Question 3

Part (c)

Question 3

A binary tree consists of nodes. Each node has 3 integer values: a left pointer, data and a right

pointer.

The binary tree is stored using a global 2D array.

The pseudocode declaration for the array is:

DECLARE ArrayNodes : ARRAY[0:19, 0:2] OF INTEGER

For example:

  • ArrayNodes[0, 0] stores the left pointer for the first node.
  • ArrayNodes[0, 1] stores the data for the first node.
  • ArrayNodes[0, 2] stores the right pointer for the first node.
  • 1 indicates a null pointer, or null data.

Part (c)

The following recursive pseudocode function searches the binary tree for a given value. If the

value is found, the function must return the index of the value. If the value is not found, the function must return –1.

The function is incomplete. There are four incomplete statements.

FUNCTION SearchValue(Root : INTEGER, ValueToFind : INTEGER) RETURNS INTEGER

IF Root = –1 THEN

RETURN –1

ELSE

IF ArrayNodes[Root, 1] = ValueToFind THEN

RETURN .......................................

ELSE

IF ArrayNodes[Root, 1] = –1 THEN

RETURN –1

ENDIF

ENDIF

ENDIF

IF ArrayNodes[Root, 1] ....................................... ValueToFind THEN

RETURN SearchValue(ArrayNodes[............, 0], ValueToFind)

ENDIF

IF ArrayNodes[Root, ............] < ValueToFind THEN

RETURN SearchValue(ArrayNodes[Root, 2], ValueToFind)

ENDIF

ENDFUNCTION

Write program code for the function SearchValue().

Save your program.

Copy and paste the program code into part 3(c) in the evidence document.

[5]

Question 10

Reveal official mark-scheme answer Official mark-scheme answer for 3(C)Official mark-scheme answer for 3(C)

Official answer for Question 3

Part (c)

Question 1

mark for each completed statement (4)

Question 5

✓
1 mark for remainder of function correct
✓
Pseudocode:
✓
FUNCTION SearchValue(BYVAL Root : INTEGER, ValueToFind :
✓
INTEGER)
✓
IF Root = -1 THEN
✓
RETURN -1
✓
ELSE
✓
IF ArrayNodes[Root,1] = ValueToFind THEN
✓
RETURN Root
✓
ELSE
✓
IF ArrayNodes[Root, 1] = -1 THEN
✓
RETURN -1
✓
ENDIF
✓
ENDIF
✓
ENDIF
✓
IF ArrayNodes[Root,1] > ValueToFind THEN
✓
RETURN SearchValue(ArrayNodes[Root,0], ValueToFind)
✓
ENDIF
✓
IF ArrayNodes[Root,1] < ValueToFind THEN
✓
RETURN SearchValue(ArrayNodes[Root,2], ValueToFind)
✓
ENDIF
✓
ENDFUNCTION
✓
Example program code:
✓
Python
✓
def SearchValue(Root, ValueToFind):
✓
global ArrayNodes
✓
if Root == -1:
✓
return -1
✓
elif ArrayNodes[Root][1] == ValueToFind:
✓
return Root
✓
elif ArrayNodes[Root][1] == -1:
✓
return -1
✓
if(ArrayNodes[Root][1] > ValueToFind):
✓
return SearchValue(ArrayNodes[Root][0], ValueToFind)
✓
if(ArrayNodes[Root][1] < ValueToFind):
✓
return SearchValue(ArrayNodes[Root][2], ValueToFind)
✓
Cambridge International AS & A Level – Mark Scheme
✓
9618/43
✓
October/November
✓
PUBLISHED
✓
2022
✓
Question
✓
Answer
✓
Marks

Mark scheme 9618_w22_ms_43.pdf →

Topic relevance

Why this question appears here

98% assigned-topic match · Computational thinking and problem-solving › Recursion

Topic-match percentages estimate how closely each question fits the selected syllabus topic. They are a guide for choosing stricter or broader practice.

Permanent source page: Educate A Change · From 9618_w22_qp_43.pdf

Keep practising this topic