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: [Diagram of a binary tree]. The class Node stores data about the nodes. 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
- (a)(i)Write program code to declare the class Node and its constructor. Do not declare the other methods. Use your programming language appropriate constructor. If you are writing in Python, include attribute declarations using comments.[4]
- (a)(ii)Write program code for the three get methods.[3]
- (a)(iii)The method SetLeft() takes an object of type Node as a parameter. The method 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()[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[2]
- (c)(i)Write program code to declare the class Tree and its constructor. Do not declare the other methods. Use your programming language appropriate constructor. If you are writing in Python, include attribute declarations using comments.[2]
- (c)(ii)Write program code for GetRootNode()[1]
- (c)(iii)The method Insert() takes a node as a parameter and then searches the tree to find 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()[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()[5]
- (e)(i)Write program code to extend the main program to: • create a new object of type Tree with the node containing the data 10 as the first node • insert the nodes with the values 20, 5, 15 and 7 into the tree in the order given • call OutputInOrder() with the tree's root node as a parameter.[3]
- (e)(ii)Test your program. Take a screenshot of the output(s). Copy and paste the screenshot into part 3(e)(ii) in the evidence document.[1]
This question uses a diagram from the original paper, which isn't shown here. Open the full paper from the past papers archive to see it.
Mark scheme
Marking points from the official mark scheme (May/June 2025). The mark or mark type is shown in brackets.
- [4] 1 mark each
- [4] • Class header (and end where appropriate)
- [4] • Constructor header (and end where appropriate) with (min) one parameter (integer) within class
- [4] • 3 attributes with correct data types
- [4] • NodeData has parameter assigned, LeftNode and RightNode are assigned null within constructor
- [3] 1 mark each
- [3] • 1 get method with no parameter ...
- [3] ... returning correct value
- [3] • 2nd and 3rd correct get methods
- [3] 1 mark each
- [3] • 1 set method taking parameter of type Node ...
- [3] ... assigning to correct attribute
- [3] • 2nd correct set method
- [2] 1 mark each
- [2] • Creating 1 instance of Node with a correct value and storing the node ...
- [2] ... remaining 4 correct
- [2] 1 mark each
- [2] • Class Tree header (and end) no inheritance and constructor header (and end) taking 1 node parameter within class
- [2] • storing parameter in FirstNode declared as a Node data type
- [1] 1 mark for
- [1] • Get method header (and end) with no parameter, returning FirstNode
- [6] 1 mark each to max 6
- [6] • Insert method header (and end) taking 1 node parameter
- [6] • If parameter < first node, checking if there is a left node ...
- [6] ... storing node in left node if it is null
- [6] • If parameter >= first node, checking if there is a right node ...
- [6] ... storing node in right node if it is null
- [6] • Looping until correct position is found // recursive calls
- [5] 1 mark each
- [5] • Procedure header (and end) taking node as parameter, that is recursive
- [5] • Checking if left is null and recursive call if not null
- [5] • Outputting node's data
- [5] • Checking if right is null and recursive call if not null
- [5] • Correct order
- [3] 1 mark each
- [3] • Creation of Tree object with the Node with value 10 as parameter
- [3] • Calling method Insert() for tree with the nodes for 20, 5, 15 and 7 in order
- [3] • Calling OutputInOrder() with tree's root node as parameter
- [1] Output of
- [1] 5
- [1] 7
- [1] 10
- [1] 15
- [1] 20
Write your answer and get it marked
On Oracle Prep you can write or photograph your own answer and have it marked by AI against this mark scheme, with feedback on what you missed.
Sign up free →