IBDP Computer Science B2.3 Programming constructs HL Paper 2 - New Syllabus

Question 

(a) (i) State two advantages of using a binary tree rather than a static array. [2]
(a) (ii) Outline one disadvantage of using a binary tree. [2]

Consider the following recursive algorithm:

public static int aMethod(int [][]arr, int k, int n)
{
    if (n < 3)
        return arr[k][n] + aMethod(arr,k,n+1);
    else
        return arr[k][3];
}
(b) Determine the value returned by aMethod(casesWon,4,0). [3]

casesWon is the array shown in Figure 11.

All recursive calls must be shown in your working.

(c) State two reasons why the algorithm in part (b) is considered a recursive algorithm. [2]
(d) Define the term object reference. [1]

Most-appropriate topic code

B2.2: Data structures — parts (a) and (b)
B2.3: Programming constructs — part (c)
B3.2: Fundamentals of OOP for multiple classes — part (d)
▶️ Answer/Explanation

(a) (i)
Award [2 max]

  • A binary tree can grow and shrink dynamically, whereas a static array has a fixed size.
  • A binary tree can be more memory efficient than an array because it does not require a contiguous block of memory.
  • A binary tree allows faster searching than an array when the tree is appropriately structured.
  • A binary tree supports ordered data storage and efficient retrieval.
  • A binary tree avoids shifting elements when inserting or deleting elements.

(a) (ii)
Award [2 max]

  • Memory usage: a binary tree can use more memory because each node stores additional pointers to its left and right children.
  • Accessing: direct access is not possible. Searching must start from the root and move through the left or right pointers, which can make retrieval slower.
  • Operations: deleting an element can be complex because the tree may need to be restructured by reassessing its child pointers.
  • Maintenance overhead: tree operations require careful pointer management, increasing algorithmic complexity.

(b)
Award [3 max]

Using the recursive calls:

aMethod(casesWon,4,0)
= 8 + aMethod(casesWon,4,1)

= 8 + 6 + aMethod(casesWon,4,2)

= 8 + 6 + 5 + aMethod(casesWon,4,3)

= 8 + 6 + 5 + 1

= 20

Therefore, the value returned is 20.

(c)
Award [2 max]

  • The method calls itself from within the method.
  • The method has a base case, n >= 3, which stops the recursion.
  • Each recursive call passes an updated value of n, moving the algorithm towards the base case.

(d)
Award [1 max]

An object reference is a pointer, address or variable that refers to the memory location of an object rather than containing the actual object itself.

Scroll to Top