Computational thinking: searching, sorting, ADTs and recursion – A Level Computer Science (9618)

22 questions · page 1 of 2 · Clear filters

0 / 0Answer a question to start your score.
Quick quiz (10)Saved 0
Advertisement
1
9618/31/M/J/26 · Question 10May/June 2026Paper 319 marks
Loading question…
Question text

10 (a) An insertion sort algorithm is used to sort the unique ID numbers of club members into
ascending order. These unique ID numbers are kept in a 1D array MemberID. The
corresponding names of the members are stored in a 1D array Names.
The arrays are declared as:
DECLARE MemberID : ARRAY[0:499] OF INTEGER
DECLARE Names : ARRAY[0:499] OF STRING
The following table defines the variables to be used in this algorithm.
identifier
data type
description
Count
integer
main loop counter to iterate through the whole array
Temp1
integer
temporary holding location for current member ID
Temp2
string
temporary holding location for current member name
Insert
integer
counter to show current insertion position
Write the pseudocode for an insertion sort to arrange the unique ID numbers into ascending
order. Make sure that the data in both arrays is swapped as required so that data with the
same index continues to apply to the same person.
Use the given identifiers. Assume all variables have been declared.
…
…
…
…
…
…
…
…
…
…
…
…
…
… [5]
© Cambridge University Press & Assessment 2026
,
,
(b) Describe the factors, other than changing the algorithm, that affect the time efficiency of the
insertion sort algorithm.
…
…
…
…
…

2
9618/32/M/J/26 · Question 10May/June 2026Paper 329 marks
Loading question…
Question text

10 (a) Two examples of searching algorithms are a linear search and a binary search.
Describe how increasing the number of data items in the file to be searched affects the time
efficiency of these search algorithms. Refer to Big O notation in your answer.
…
…
…
…
…
… [3]
© Cambridge University Press & Assessment 2026
,
,
(b) A temperature in degrees Celsius (°C) is recorded every minute for one day in a random
file, Temperatures.dat.
A function is required to find the temperature for a specific time in the day to check if it
was freezing. The position of each temperature in the file corresponds to the number
of minutes after midnight, for example, the temperature at 10:00 am would be stored at
position 600 (10 × 60).
Write the pseudocode to complete the algorithm to check a temperature stored in
Temperatures.dat, by passing its position in the file as a parameter and using file
handling commands to find the relevant data item. Once the temperature is found, it is
checked to see if it is above or below freezing, and an appropriate message is returned.
Some of the blank lines may not be used.
FUNCTION TempCheck( …
DECLARE Temperature : REAL
DECLARE Response : STRING
OPENFILE …
…
…
…
…
…
…
…
…
…
…
…
ENDFUNCTION
[6]
© Cambridge University Press & Assessment 2026

3
9618/33/M/J/26 · Question 10May/June 2026Paper 339 marks
Loading question…
Question text

10 (a) A program declares a 1D array globally as PartName[1:1000000] OF STRING, and sorts
its contents in ascending alphabetical order. The function PartSearch performs a binary
search on the array. The item to be found is passed as a parameter. If it is found, its location
in the array is returned, otherwise, –1 is returned.
(i)
Complete the following table to define the variables used in the function.
identifier
data type
description
Left
Right
Mid
Target
[2]
© Cambridge University Press & Assessment 2026
,
,
(ii)
Complete the pseudocode for the function to perform a binary search on PartName
using all the variables from the table in 10(a)(i).
Some lines may be left blank.
FUNCTION PartSearch( …
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
…
ENDFUNCTION
[5]
(b) Describe how the time efficiency of a binary search changes as the number of items in the
search list increases. Refer to Big O notation in your answer.
…
…
…
… [2]
© Cambridge Unive

Advertisement
4
9618/42/M/J/26 · Question 1May/June 2026Paper 4218 marks
Loading question…
Question text

A program stores nine lower‑case strings in a 1D array TheData. The following strings are
1
stored in the order shown:
laptop
tablet
printer
computer
processor
monitor
memory
cable
touchscreen
(a) Write program code to declare the array TheData in the main program. Store the given
strings in the array.
Save your program as Question1_J26.
Copy and paste the program code into 1(a) in the evidence document.
[2]
The procedure FindLongestShortest():
(b) (i)
•
takes an array of strings as a parameter
•
finds and outputs the longest string in the array of strings
•
finds and outputs the shortest string in the array of strings.
All outputs must have appropriate messages. Do not use in‑built functions to find the
longest and shortest string in the array. You may use an in‑built length function to find
the length of a string.
You can assume that there will only be one string that has the shortest length.
You can assume that there will only be one string that has the longest length.
Write program code for FindLongestShortest()
Save your program.
Copy and paste the program code into 1(b)(i) in the evidence document.
[5]
© Cambridge University Press & Assessment 2026
Write program code to extend the main program to call FindLongestShortest()
(ii)
with TheData
Save your program.
Copy and paste the program code into 1(b)(ii) in the evidence document.
[1]
(iii)
Test your program
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(b)(iii) in the evidence document.
[1]
The function InsertionSort():
(c) (i)
•
takes an array of strings as a parameter
•
sorts the strings in the array into ascending alphabetical order using an insertion sort
•
returns the sorted array.
The function must work for an array of any length. Do not use an in‑built function to sort
the array.
Write program code for InsertionSort()
Save your program.
Copy and paste the program code into 1(c)(i) in the evidence document.
[5]
(ii)
Write program code to extend the main program to perform the following in the
order shown:
•
output the string "Before sorting"
•
output the contents of the array TheData
•
call InsertionSort() with TheData as a parameter
•
output the string "After sorting"
•
output the contents of the returned sorted array.
Save your program.
Copy and paste the program code into 1(c)(ii) in the evidence document.
[3]
(iii)
Test your program
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 1(c)(iii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026

5
9618/43/M/J/26 · Question 3May/June 2026Paper 4325 marks
Loading question…
Question text

3
A binary tree is designed to store up to 100 positive integers. The binary tree stores data in
ascending numerical order.
The data in the binary tree is created as a global 2D array with the identifier BinaryTree
Each node has three values:
•
a pointer to the array index with the left node
•
the integer data
•
a pointer to the array index with the right node.
A null pointer is represented by the integer –1. All nodes are initialised with the null data –1
The table has example data for a binary tree:
index
left pointer
data
right pointer
0
1
20
2
1
4
10
–1
2
3
26
–1
3
–1
22
–1
4
–1
8
–1
The binary tree has two global pointers:
•
TreeRootPointer, initialised to –1, stores the index of the root node of the binary tree
•
FirstFreeNodePointer, initialised to 0, stores the index of the first node that does not
store any data.
Data cannot be removed or replaced from the tree. FirstFreeNodePointer is incremented
each time a node is added to the binary tree.
(a) The main program initialises all 100 nodes, TreeRootPointer
and FirstFreeNodePointer
Write program code for the main program.
Save your program as Question3_J26.
Copy and paste the program code into 3(a) in the evidence document.
[3]
© Cambridge University Press & Assessment 2026
(b) The procedure AddNode():
•
takes an integer parameter to be stored as the data in a new node
•
stores the data in the first element and updates the root pointer when the tree is empty
•
outputs "The tree is full" if the tree is full
•
stores the data in the next free node if the tree is not full, then:
◦
checks if the data is less than or greater than the root node
◦
follows the left or right pointer(s) repeatedly until it reaches the appropriate
leaf node
◦
updates the appropriate parent node pointer to the new node.
Write program code for AddNode()
Save your program.
Copy and paste the program code into 3(b) in the evidence document.
[7]
(c) The procedure OutputArray() outputs the content of the first 10 elements in the binary
tree. Each node is output on one line in the format:
<left pointer> <data> <right pointer>
For example:
1
20
4
Write program code for OutputArray()
Save your program.
Copy and paste the program code into 3(c) in the evidence document.
[2]
© Cambridge University Press & Assessment 2026
(d) (i)
Write program code to extend the main program to:
•
take 10 integers as input
•
store each input in the binary tree in the order they are entered using the
appropriate procedure
•
output the content of the binary tree using OutputArray()
Save your program.
Copy and paste the program code into 3(d)(i) in the evidence document.
[3]
(ii)
Test your program. Enter the following numbers in the order shown:
50
26
120
236
2
16
67
49
165
15
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into 3(d)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(e) An in‑order tree traversal outputs the contents of the binary tree in ascending numerical
order by:
•
visiting the left node
•
output the data
•
visiting the right node.
For example, an in‑order traversal on the following binary tree will output: 5 12 18 20 26
20
12
26
5
18
The recursive procedure InOrder():
(i)
•
takes the root pointer as a parameter
•
makes a recursive call with the left pointer as the parameter if the left pointer is
not null
•
outputs the data at the parameter
•
makes a recursive call with the right pointer as the parameter if the right pointer is
not null.
Write program code for InOrder()
Save your program.
Copy and paste the program code into 3(e)(i) in the evidence document.
[7]
Write program code to extend the main program to call InOrder()
(ii)
Save your program.
Copy and paste the program code into 3(e)(ii) in the evidence document.
[1]
© Cambridge University Press & Assessment 2026
(iii)
Test your program. Enter the following numbers in the order given:
50
26
120
236
2
16

6
9618/31/M/J/25 · Question 4May/June 2025Paper 317 marks
Loading question…
Question text

4
(a) A linked list of nodes is used to store an ordered list of integers. Each node consists of the
data, a left pointer and a right pointer, for example:
Left pointer Data
Right pointer
20
The linked list will be organised as a binary tree.
−1 is used to represent a null pointer.
Complete the binary tree, including null pointers, to show how the data will be organised after
the following integers have been added:
6, 15, 41, 66
Root pointer
36
12
40
– 1
3
[4]
(b) Describe what is meant by recursion.
…
…
…
… [2]
ĬÏĊ®Ġ´íÈõÏĪÅĊÞû·Ā×
,
,
(c) A binary tree is a suitable Abstract Data Type (ADT) that a designer can implement using
recursive algorithms.
Identify one other ADT that a designer can implement using recursive algorithms.
… [1]

7
9618/32/M/J/25 · Question 11May/June 2025Paper 326 marks
Loading question…
Question text

11
(a) A linked list of nodes is used to store an ordered list of strings. Each node consists of the
data, a left pointer and a right pointer.
Left pointer Data Right pointer
Pp
The linked list will be organised as a binary tree.
0 is used to represent a null pointer.
Complete the binary tree, including null pointers, to show how the data will be organised after
the following strings have been added:
Aa, Mm, Ss, Xx
Root pointer
Pp
Gg
Rr
0
Kk
[4]
ĬÑĊ®Ġ´íÈõÏĪÅĊÞù¸þ×
,
,
(b) A binary tree can be used to implement recursion.
Identify one feature of an algorithm that makes it beneficial to use recursion.
Give one example of an application that could use a recursive algorithm.
Feature …
…
Example …
[2]
ĬÓĊ®Ġ´íÈõÏĪÅĊÞû¸þ×

8
9618/33/M/J/25 · Question 12May/June 2025Paper 337 marks
Loading question…
Question text

12 (a) An array is an Abstract Data Type (ADT).
Identify two other ADTs.
1 …
2 …
[1]
(b) A 1D array DataArray holds up to 1000 elements of type integer and needs to be sorted in
ascending order.
Write the pseudocode for an insertion sort to sort the array into ascending order.
Use the identifiers from the table in your algorithm.
You do not need to declare any arrays or variables for this algorithm. You may assume this
has already been done.
Identifier
Data type
Description
Index
INTEGER
counter for outer loop
Position
INTEGER
counter for inner loop – insertion position
DataArray
INTEGER
1D array to store up to 1000 integers
Value
INTEGER
value to insert
The first line has been written for you.
← 2 to 1000
FOR Index
…
…
…
…
…
…
…
…
…
… [4]
ĬÍĊ®Ġ´íÈõÏĪÅĊàùµĂ×
,
,
(c) Describe two ways in which the performance of a sort routine is affected by the data to be
sorted.
1 …
…
2 …
…
[2]
ĬÏĊ®Ġ´íÈõÏĪÅĊàûµĂ×

Advertisement
9
9618/33/M/J/25 · Question 13May/June 2025Paper 33114 marks
Loading question…
Question text

13 The recursive procedure Delete() is defined as follows:
PROCEDURE Delete(Index, Target)
IF Numbers[Index] > 0 THEN
IF Numbers[Index] >= Target THEN
←
Numbers[Index + 1]
Numbers[Index]
ENDIF
←
Index + 1
Index
CALL Delete(Index, Target)
ENDIF
ENDPROCEDURE
An array Numbers is used to store a sorted data set of non-zero positive integers.
Unused cells contain zero.
The contents of the array at the start of the algorithm are:
Numbers
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
2
3
7
11
15
17
19
23
0
0
Complete the trace table for the algorithm for the procedure call:
CALL Delete(1, 15)
Numbers
Index
Target
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
2
3
7
11
15
17
19
23
0
0
[4]
Permission to reproduce items where third-party owned material protected by copyright is included has been sought and cleared where possible. Every
publisher will be pleased to make amends at the earliest possible opportunity.
To avoid the issue of disclosure of answer-related information to candidates, all copyright acknowledgements are reproduced online in the Cambridge
Assessment International Education Copyright Acknowledgements Booklet. This is produced for each series of examinations and is freely available to download
at www.cambridgeinternational.org after the live examination series.
Cambridge Assessment International Education is part of Cambridge Assessment. Cambridge Assessment is the brand name of the University of Cambridge
ĬÍĊ®Ġ´íÈõÏĪÅĊÞùµĄ×

10
9618/41/M/J/25 · Question 3May/June 2025Paper 4130 marks
Loading question…
Question text

3
A program stores data in a binary tree that is designed using Object‑Oriented Programming
(OOP).
The tree stores data in ascending numerical order, for example:
30
20
45
1
21
33
63
The class Node stores data about the nodes.
Node
NodeData : Integer
stores the node’s integer data
LeftNode : Node
stores the node that is stored to the left of the current
node, or a null value if there is no node to the left
RightNode : Node
stores the node that is stored to the right of the current
node, or a null value if there is no node to the right
Constructor()
initialises NodeData to its parameter value; initialises
LeftNode and RightNode to a null value
GetLeft()
returns LeftNode
GetRight()
returns RightNode
GetData()
returns NodeData
SetLeft()
takes an object of type Node as a parameter and stores
it in LeftNode
SetRight()
takes an object of type Node as a parameter and stores
it in RightNode
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question3_J25.
Copy and paste the program code into part 3(a)(i) in the evidence document.
[4]
(ii)
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 3(a)(ii) in the evidence document.
[3]
The method SetLeft() takes an object of type Node as a parameter. The method
(iii)
stores the parameter in the attribute LeftNode
The method SetRight() takes an object of type Node as a parameter. The method
stores the parameter in the attribute RightNode
Write program code for SetLeft() and SetRight()
Save your program.
Copy and paste the program code into part 3(a)(iii) in the evidence document.
[3]
(b) Write the main program to declare five objects of type Node :
•
Node 1 with the data 10
•
Node 2 with the data 20
•
Node 3 with the data 5
•
Node 4 with the data 15
•
Node 5 with the data 7
Save your program.
Copy and paste the program code into part 3(b) in the evidence document.
[2]
(c) The class Tree stores the tree.
Tree
FirstNode : Node
stores the root node in the tree
Constructor()
initialises FirstNode to its parameter value
GetRootNode()
returns the node stored in FirstNode
Insert()
stores its parameter node in the correct position in the
tree
Write program code to declare the class Tree and its constructor.
(i)
Do not declare the other methods.
Use your programming language appropriate constructor.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 3(c)(i) in the evidence document.
[2]
Write program code for GetRootNode()
(ii)
Save your program.
Copy and paste the program code into part 3(c)(ii) in the evidence document.
[1]
The method Insert() takes a node as a parameter and then searches the tree to find
(iii)
the position to insert the new node by:
•
checking whether the node’s data is less than or greater than the root node’s data
•
moving to the left node if the data is less than the root node’s data
•
moving to the right node if the data is greater than or equal to the root node’s data
•
repeating until the final position of the node is found and stores the node in that
position.
Write program code for Insert()
Save your program.
Copy and paste the program code into part 3(c)(iii) in the evidence document.
[6]
(d) The recursive procedure OutputInOrder() outputs the data stored in the binary tree in
ascending numerical order.
The procedure takes a Node object as a parameter and then:
•
checks if the left node is null. If there is a left node, the function calls itself with the left
node
•
outputs the data of the current node
•
checks if the right node is null. If there is a right node, the function calls itself with the
right node.
Write program code for OutputInOrder()
Save your program.
Copy an

11
9618/43/M/J/25 · Question 2May/June 2025Paper 4320 marks
Loading question…
Question text

A program sorts the data in the 1D array DataArray and searches DataArray for specific
2
values.
DataArray stores 14 integer values and is declared local to the main program.
(a) Write program code to create DataArray and initialise it with the following data values in the
order they are written:
0
3
4
56
67
44
43
32
31
345
45
6
54
1
Save your program as Question2_J25.
Copy and paste the program code into part 2(a) in the evidence document.
[1]
(b) The function InsertionSort() takes an array of integers as a parameter. The function
sorts the data in the array into ascending numerical order using an insertion sort. The function
returns the sorted array.
Write program code for InsertionSort()
You must not use any inbuilt sorting functions for your programming language.
Save your program.
Copy and paste the program code into part 2(b) in the evidence document.
[5]
(c) The procedure OutputArray() takes an array of integers as a parameter. The procedure
outputs each element in the array from the first element to the last element. The output is on
one line with a space between each number.
An example output is:
"0 3 4 56 67 44 43 32 31 345 45 6 54 1"
Write program code for OutputArray()
Save your program.
Copy and paste the program code into part 2(c) in the evidence document.
[2]
(d) (i)
The main program needs extending to:
•
output the content of the unsorted array using OutputArray()
•
sort the array using InsertionSort()
•
output the content of the sorted array using OutputArray()
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(d)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(d)(ii) in the evidence document.
[1]
(e) The function Search() performs a binary search to find ItemToFind in DataArray
The function takes two parameters:
•
DataArray, an array of integers
•
ItemToFind, an integer to find in DataArray
The function returns:
•
the index of ItemToFind if it is in DataArray
•
–1 if ItemToFind is not in DataArray
Write program code for Search()
You must not use any inbuilt searching functions for your programming language.
Save your program.
Copy and paste the program code into part 2(e) in the evidence document.
[6]
The main program needs extending to call Search() with the sorted array four times:
(f)
(i)
•
the first time to find the index of the integer 0
•
the second time to find the index of the integer 345
•
the third time to find the index of the integer 67
•
the fourth time to find the index of the integer 2
If the integer is found in the array, output an appropriate message that includes the index.
If the integer is not found, output that it was not found.
Write program code to extend the main program.
Save your program.
Copy and paste the program code into part 2(f)(i) in the evidence document.
[2]
(ii)
Test your program.
Take a screenshot of the output(s).
Save your program.
Copy and paste the screenshot into part 2(f)(ii) in the evidence document.
[1]

12
9618/31/M/J/24 · Question 10May/June 2024Paper 318 marks
Loading question…
Question text

10 (a) State a condition that must be true for an array to be searchable for a binary search.
…
… [1]
(b) Complete the given pseudocode to find an item in a 1D array Names of type STRING using a
binary search.
DECLARE Names : ARRAY[1:100000] OF STRING
DECLARE TopOfList : INTEGER
DECLARE EndOfList : INTEGER
DECLARE CurrentItem : INTEGER
DECLARE ToFind : STRING
DECLARE Found : BOOLEAN
DECLARE NotInList : BOOLEAN
←
1
TopOfList
←
100000
EndOfList
OUTPUT "Which name do you wish to find? "
INPUT ToFind
…
← FALSE
NotInList
WHILE … AND …
←
(TopOfList + EndOfList) DIV 2
CurrentItem
IF … THEN
←
TRUE
Found
ELSE
IF TopOfList >= EndOfList THEN
…
ELSE
IF ToFind > Names[CurrentItem] THEN
…
ELSE
←
CurrentItem – 1
EndOfList
ENDIF
ENDIF
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Item found at position ", CurrentItem, " in array"
ELSE
OUTPUT "Item not in array"
ENDIF
[5]
(c) Describe the performance of a binary search in relation to the number of data items in the
array being searched. Refer to Big O notation in your answer.
…
…
…
… [2]

13
9618/32/M/J/24 · Question 8May/June 2024Paper 328 marks
Loading question…
Question text

(a) Complete the pseudocode to find an item in a 1D array Widgets of type STRING, using a
8
linear search.
DECLARE Widgets : ARRAY[1:50000] OF STRING
DECLARE TopOfList : INTEGER
DECLARE EndOfList : INTEGER
DECLARE Count : INTEGER
DECLARE ToFind : STRING
DECLARE Found : BOOLEAN
DECLARE NotInList : BOOLEAN
1
TopOfList
50000
EndOfList
OUTPUT "Enter the name of the item you wish to find "
INPUT ToFind
…
FALSE
NotInList
TopOfList
Count
WHILE … AND …
IF … THEN
TRUE
Found
ENDIF
Count + 1
Count
IF … THEN
TRUE
NotInList
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Item found at position ", Count - 1, " in array"
ELSE
OUTPUT "Item not in array"
ENDIF
[4]
(b) Compare the methods used by the linear and binary search algorithms to find an item in an
array. Refer to Big O notation in your answer.
…
…
…
…
…
…
…
… [4]

Advertisement
14
9618/33/M/J/24 · Question 10May/June 2024Paper 338 marks
Loading question…
Question text

10 (a) State a condition that must be true for an array to be searchable for a binary search.
…
… [1]
(b) Complete the given pseudocode to find an item in a 1D array Names of type STRING using a
binary search.
DECLARE Names : ARRAY[1:100000] OF STRING
DECLARE TopOfList : INTEGER
DECLARE EndOfList : INTEGER
DECLARE CurrentItem : INTEGER
DECLARE ToFind : STRING
DECLARE Found : BOOLEAN
DECLARE NotInList : BOOLEAN
←
TopOfList
1
←
EndOfList
100000
OUTPUT "Which name do you wish to find? "
INPUT ToFind
…
← FALSE
NotInList
WHILE … AND …
←
CurrentItem
(TopOfList + EndOfList) DIV 2
IF … THEN
←
Found
TRUE
ELSE
IF TopOfList >= EndOfList THEN
…
ELSE
IF ToFind > Names[CurrentItem] THEN
…
ELSE
←
EndOfList
CurrentItem – 1
ENDIF
ENDIF
ENDIF
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Item found at position ", CurrentItem, " in array"
ELSE
OUTPUT "Item not in array"
ENDIF
[5]
,
,
(c) Describe the performance of a binary search in relation to the number of data items in the
array being searched. Refer to Big O notation in your answer.
…
…
…
… [2]

15
9618/42/M/J/24 · Question 2May/June 2024Paper 4230 marks
Loading question…
Question text

2
A binary tree stores data in ascending order. For example:
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
Write program code to declare the class Node and its constructor.
(a) (i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program as Question2_J24.
Copy and paste the program code into part 2(a)(i) in the evidence document.
[4]
The get methods GetLeft(), GetRight()and GetData()each return the relevant
(ii)
attribute.
Write program code for the three get methods.
Save your program.
Copy and paste the program code into part 2(a)(ii) in the evidence document.
[3]
The set methods SetLeft(), SetRight()and SetData()each take a parameter and
(iii)
then store this in the relevant attribute.
Write program code for the three set methods.
Save your program.
Copy and paste the program code into part 2(a)(iii) in the evidence document.
[3]
(b) 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.
Write program code to declare the class TreeClass and its constructor.
(i)
Do not declare the other methods.
Use the appropriate constructor for your programming language.
If you are writing in Python, include attribute declarations using comments.
Save your program.
Copy and paste the program code into part 2(b)(i) in the evidence document.
[4]
The method InsertNode()takes a Node object, NewNode, as a parameter and inserts
(ii)
it into the array Tree.
InsertNode()first checks if the tree is empty.
If the tree is empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
increments NumberNodes
•
stores 0 in FirstNode.
If the tree is not empty, InsertNode():
•
stores NewNode in the array Tree at index NumberNodes
•
accesses the data in the array Tree at index FirstNode and compares it to the
data in NewNode
•
repeatedly follows the pointers until the correct position for NewNode is found
•
once the position is found, InsertNode()sets the left or right pointer of its parent
node
•
increments NumberNodes.
Write program code for InsertNode().
Save your program.
Copy and paste the program code into part 2(b)(ii) in the evidence document.
[6]
The method OutputTree()outputs the left pointer, the data and the right pointer for
(iii)
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 progra

Advertisement