Past-paper question · 2024 May-June · Paper 42 · Q2(b)(iii)

Question 2(b)(iii) 2024 · May-June · Paper 42 · 4 marks

Original past-paper question for 2(B)(III)
Original past-paper question for 2(B)(III)
Original past-paper question for 2(B)(III)
Original past-paper question for 2(B)(III)
Reveal official mark-scheme answer Official mark-scheme answer for 2(B)(III)

Official answer for Question 2

Part (b)

(iii)

Question 1

mark each

Question 4

✓
Procedure header (and end) with no parameter and if no nodes output ‘No nodes’
✓
(otherwise) Loop from index 0 to NumberNodes (or equivalent) …
✓
… Outputting LeftPointer, Data then RightPointer
✓
… using get methods
✓
e.g.
✓
Java
✓
public void OutputTree(){
✓
if(NumberNodes == 0){
✓
System.out.println("No nodes");
✓
}else{
✓
for(Integer x = 0; x < NumberNodes; x++){
✓
System.out.println(Tree[x].GetLeft() + " " + Tree[x].GetData() + " " +
✓
Tree[x].GetRight());
✓
}
✓
}
✓
}
✓
VB.NET
✓
Sub OutputTree()
✓
If NumberNodes = 0 Then
✓
Console.WriteLine("No nodes")
✓
Else
✓
For x = 0 To NumberNodes - 1
✓
Console.WriteLine(Tree(x).GetLeft() & " " & Tree(x).GetData() & " " &
✓
Tree(x).GetRight())
✓
Next
✓
End If
✓
End Sub
✓
9618/42
✓
Cambridge International AS & A Level – Mark Scheme
✓
May/June 2024
✓
PUBLISHED
✓
Question
✓
Answer
✓
Marks

Mark scheme 9618_s24_ms_42.pdf →

Show text version of question

Question 2

Part (b)

(iii)

Question 2

A binary tree stores data in ascending order. For example:

Question 15

8 19

3 10

A
computer program stores integers in a binary tree in ascending order. The program uses

Object‑Oriented Programming (OOP).

The binary tree is stored as a 1D array of nodes. Each node contains a left pointer, a data value and a right pointer.

The class Node stores the data about a node.

Node LeftPointer : INTEGER stores the index of the node to the left in the binary tree

Data : INTEGER stores the node’s data

RightPointer : INTEGER stores the index of the node to the right in the binary tree Constructor() initialises Data to its parameter value initialises LeftPointer and RightPointer to −1

GetLeft() returns the left pointer

GetRight() returns the right pointer

GetData() returns the data value

SetLeft() assigns the parameter to the left pointer

SetRight() assigns the parameter to the right pointer

SetData() assigns the parameter to the data The class TreeClass stores the data about the binary tree.

TreeClass Tree[0:19] : Node an array of 20 elements of type Node

FirstNode : INTEGER stores the index of the first node in the tree

NumberNodes : INTEGER stores the quantity of nodes in the tree Constructor() initialises FirstNode to −1 and NumberNodes to 0 initialises each element in Tree to a Node object with the data value of −1

InsertNode() takes a Node object as a parameter, inserts it in the array and updates the pointer for its parent node

OutputTree() outputs the left pointer, data and right pointer of each node in Tree

Nodes cannot be deleted from this tree.

(iii)

The method OutputTree()outputs the left pointer, the data and the right pointer for

each node that has been inserted into the tree. The outputs are in the order they are saved in the array.

If there are no nodes in the array, the procedure outputs ‘No nodes’.

Write program code for OutputTree().

Save your program.

Copy and paste the program code into part 2(b)(iii) in the evidence document.

[4]

Question 9

Topic relevance

Why this question appears here

71% assigned-topic match · Programming › Constructs

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_s24_qp_42.pdf

Keep practising this topic