Date | November 2020 | Marks available | 3 | Reference code | 20N.2.HL.TZ0.16 |
Level | HL | Paper | 2 | Time zone | no time zone |
Command term | Outline | Question number | 16 | Adapted from | N/A |
Question
A single parking space that holds one car can be used to hold two motorbikes. The programmer has decided to use a two-dimensional array to create a map of the parking area.
Vehicle parkingSpaces[][] = new Vehicle[2][200]
;
Note: m
and c
have been used to show where motorbikes and cars are parked.
Police have requested a way of searching the parking area for vehicles with a specified registration plate, and a binary tree has been suggested.
Identify one advantage of using a two-dimensional array for this purpose.
Identify one disadvantage of using a two-dimensional array for this purpose.
Outline why searching for the registration plate in a binary tree would be faster than looking for it in the two-dimensional array.
Identify the steps that would be involved in taking the data from this two-dimensional array and creating a binary search tree.
Markscheme
Award [1 max].
Faster to access (i.e. O1 instead of On);
Arrays take less memory than lists (as they do not store pointers);
Mirrors real life situation;
Award [1 max].
Static, therefore memory wasted when not being fully utilised;
Inability to grow, should further spaces be added, i.e. If Parking area grows;
Award [3 max].
The array would need to be searched using a linear search which is inefficient O(n) whereas a binary search is much more efficient O(log n);
A linear search for a car in this parking area could require a maximum of 200 comparisons (i.e. it would look at the first row of every column) whereas a binary search would only require 8 (i.e. log 200 base 2);
A linear search for a motorbike in this parking area would require a maximum of 400 comparisons (i.e. it would need to look at both rows of every column) whereas a binary search would only require 7 (log 100 base 2);
In a binary search, each comparison would eliminate half of the remaining possible matches, whereas in a linear search it only eliminates 1;
A binary search has the registration plate as its key and therefore it doesn’t require accessing the field of the vehicle object for each comparison, whereas in a linear search, each comparison would be slower because it would require getting the vehicle object’s registration field using an accessor method;
A binary search is always faster than a linear search on a large data set. As the police may be searching for many cars and in many parking areas, their searches will be on large set of data;
Award [5 max].
Create a binary tree to store vehicle objects;
Loop around all of the elements in the 2 dimensional array (e.g. using inner and outer loop);
Skip empty locations;
Take the first vehicle found and add as the head of the binary tree;
Take all subsequent vehicles found and do the following;
Get the node’s vehicle registration;
Find the appropriate insertion point in the binary tree by navigating from the root and comparing the registration to the key of the current node;
When a leaf node is reached compare the registration with the vehicle to be inserted and insert it as the left or right child node, using its registration as the key;
Continue until the end of the vehicle array has been reached;
Examiners report
Both parts were well-answered.
Both parts were well-answered.
Most students identified the tree property that effectively eliminates half of the tree when searching but answers were not always detailed enough to gain full marks.
A large number of students were unclear as to how a binary search tree is created. Full marks were only reached by those students who clearly identified the different stages of the process.