Data Structures
Data Structures
Figure 1
TempCard ← Cards[RNo1]
Cards[RNo1] ← Cards[RNo2]
Cards[RNo2] ← TempCard
Figure 2
Cards[RNo1] ← Cards[RNo2]
Cards[RNo2] ← Cards[RNo1]
Explain why the Shuffle subroutine would not work if it used the method shown in
Figure 2 instead of the method shown in Figure 1.
___________________________________________________________________
___________________________________________________________________
(1)
(b) State one reason why a set could not have been used instead of a list.
___________________________________________________________________
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
Page 1 of 64
Explain why a hash table is a suitable choice.
___________________________________________________________________
___________________________________________________________________
(1)
(Total 6 marks)
Q2.
For the expression 3+x the binary tree stores + at the root, 3 at the left hand node and x at
the right hand node. If the nodes of this tree are printed as the tree is traversed, what will
be printed when the traversal is
Q3.
A binary search tree is used by software to store and then search for user names on a
college network.
The following are the first seven user names to join the tree:
(2)
(i) The task is to search for the user name CollinsK. List in order the nodes
visited.
______________________________________________________________
(1)
(ii) A second search is done to find the user name RuddleA. How many
Page 2 of 64
comparisons does this require?
______________________________________________________________
(1)
(Total 4 marks)
Q4.
A tree can be used to represent a mathematical expression. This is known as an
expression tree. Figure 1 is an expression tree for the infix expression 4 + 9 * 6.
Figure 1
(b) The expression tree in Figure 1 could be represented using three one-dimensional
arrays named A, B and C. Figure 2 shows a representation of Figure 1 together
with the array indices.
Figure 2
Arrays
Index A B C
[1] + 2 3
[2] 4 0 0
[3] * 4 5
[4] 9 0 0
[5] 6 0 0
Page 3 of 64
Describe the role of each of the arrays A, B and C.
A:
_________________________________________________________________
B:
_________________________________________________________________
C:
_________________________________________________________________
(3)
___________________________________________________________________
___________________________________________________________________
(1)
(d) The procedure in Figure 3 describes a type of tree traversal that can be carried out
on the representation of the tree shown in Figure 2.
Figure 3
Procedure Traverse(Pos:Integer)
Output A[Pos]
End Procedure
Using the table below, trace the execution of the procedure when it is called using
Traverse(1). You may not need to use all of the lines provided in the table.
Pos Output
Page 4 of 64
(4)
(e) Which type of tree traversal does the procedure Traverse carry out?
___________________________________________________________________
(1)
___________________________________________________________________
(1)
(Total 12 marks)
Q5.
A binary tree is a type of data structure.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(b) Figure 1 shows a binary tree and its representation using an array of records called
Tree. Each record consists of three fields, Data, Left and Right.
Figure 1
Figure 2 shows a subroutine that implements a binary tree search algorithm using
the array Tree. The subroutine parameter, k, is the data item being searched for.
The subroutine returns a Boolean value indicating if the data item being searched
for is in the binary tree or not.
Figure 2
SUBROUTINE BTS(k)
Current ← 1
WHILE Current > 2
IF Tree[Current].Data = k THEN
RETURN 3
Page 5 of 64
ELSEIF Tree[Current].Data < k THEN
4
ELSE
5
ENDIF
ENDWHILE
RETURN 6
ENDSUBROUTINE
Complete each row in the table below, to show what the labels indicating missing
parts of the algorithm in Figure 2 should be replaced by.
6
(4)
(c) There are similarities in how the binary tree search and the binary search algorithms
work.
___________________________________________________________________
___________________________________________________________________
(1)
(d) Explain why the binary search algorithm has the time complexity stated in your
answer for part (c).
___________________________________________________________________
___________________________________________________________________
(1)
(e) Explain why searching for an item in a list or tree is a tractable problem.
___________________________________________________________________
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Page 6 of 64
___________________________________________________________________
(2)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 13 marks)
Q6.
The table below shows an adjacency matrix representation of a directed graph (digraph).
(2)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
Page 7 of 64
What properties must a graph have for it to be a tree?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
Show how the following data may be stored as a binary tree for subsequent
processing in alphabetic order by drawing the tree. Assume that the first item is the
root of the tree and the rest of the data items are inserted into the tree in the order
given.
(3)
(e) A binary tree such as the one created in part (d) could be represented using one
array of records or, alternatively, using three one-dimensional arrays.
Describe how the data stored in the array(s) could be structured for one of these
two possible methods of representation.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
(Total 12 marks)
Q7.
Page 8 of 64
Figure 1 is a graph that shows the time it takes to travel between six locations in a
warehouse. The six locations have been labelled with the numbers 1 - 6. When there is no
edge between two nodes in the graph this means that it is not possible to travel directly
between those two locations. When there is an edge between two nodes in the graph the
edge is labelled with the time (in minutes) it takes to travel between the two locations
represented by the nodes.
(a) The graph is represented using an adjacency matrix, with the value 0 being used to
indicate that there is no edge between two nodes in the graph.
Complete the unshaded cells in Table 1 so that it shows the adjacency matrix for
Figure 1.
Table 1
1 2 3 4 5 6
4
5
6
(2)
(b) Instead of using an adjacency matrix, an adjacency list could be used to represent
the graph. Explain the circumstances in which it would be more appropriate to use
an adjacency list instead of an adjacency matrix.
___________________________________________________________________
Page 9 of 64
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(c) State one reason why the graph shown in Figure 1 is not a tree.
___________________________________________________________________
___________________________________________________________________
(1)
(d) The graph in Figure 1 is a weighted graph. Explain what is meant by a weighted
graph.
___________________________________________________________________
___________________________________________________________________
(1)
Figure 2 contains pseudo-code for a version of Djikstra’s algorithm used with the graph in
Figure 1.
Q is a priority queue which stores nodes from the graph, maintained in an order based on
the values in array D. The reordering of Q is performed automatically when a value in D is
changed.
AM is the name given to the adjacency matrix for the graph represented in Figure 1.
Figure 2
Q ← empty queue
FOR C1 ← 1 TO 6
D[C1] ← 20
P[C1] ← −1
ADD C1 TO Q
ENDFOR
D[1] ←
0
WHILE Q NOT EMPTY
U ←get next node from Q
remove U from Q
FOR EACH V IN Q WHERE AM[U, V] > 0
A ←D[U] + AM[U, V]
IF A < D[V] THEN
D[V] ← A
P[V] ← U
ENDIF
ENDFOR
ENDWHILE
OUTPUT D[6]
Page 10 of 64
(e) Complete the unshaded cells of Table 2 to show the result of tracing the algorithm
shown in Figure 2. Some of the trace, including the maintenance of Q, has already
been completed for you.
(7)
(f) What does the output from the algorithm in Figure 2 represent?
___________________________________________________________________
___________________________________________________________________
(1)
(g) The contents of the array P were changed by the algorithm. What is the purpose of
the array P?
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 16 marks)
Q8.
Figure 1 shows a simpler example of a type of logic puzzle with fewer cells. In this simpler
puzzle only the letters A-D are used.
Page 11 of 64
It is possible to represent this type of puzzle as a graph. To do this a unique number is
given to each cell and a node containing this unique number is added to the graph. An
edge between two nodes denotes a link between those two cells, meaning they cannot
contain the same letter as each other.
Figure 2 shows how unique numbers have been allocated to each cell in the puzzle in
Figure 1 and Figure 3 shows an adjacency matrix that represents this puzzle.
Page 12 of 64
The graph in Figure 3 can be considered to be both a representational abstraction and an
abstraction by generalisation of the puzzle from Figure 1.
Only the top half of the matrix in Figure 3 needed to be used to present the puzzle. For
which type of graph would the bottom half of the matrix also need to be used?
_______________________________________________________________________
_______________________________________________________________________
(Total 1 mark)
Q9.
Figure 1 shows a simpler example of a type of logic puzzle with fewer cells. In this simpler
puzzle only the letters A-D are used.
Page 13 of 64
It is possible to represent this type of puzzle as a graph. To do this a unique number is
given to each cell and a node containing this unique number is added to the graph. An
edge between two nodes denotes a link between those two cells, meaning they cannot
contain the same letter as each other.
Figure 2 shows how unique numbers have been allocated to each cell in the puzzle in
Figure 1 and Figure 3 shows an adjacency matrix that represents this puzzle.
Page 14 of 64
The graph in Figure 3 can be considered to be both a representational abstraction and an
abstraction by generalisation of the puzzle from Figure 1.
Explain the circumstances when it would be more appropriate to use an adjacency matrix
instead of an adjacency list.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 2 marks)
Q10.
Explain how the elements in a non-empty queue may be reversed with the aid of a stack.
Page 15 of 64
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 4 marks)
Q11.
Describe how the elements in a non-empty queue are reversed with the aid of a stack.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 4 marks)
Q12.
The list Ports contains the following names:
The table below shows some functions which take a list as their single argument and
return a result which is either an element of a list or a boolean value.
Head(list) – If the list is non-empty, it returns the element at the head of the list
(e.g. Head (Ports) → Southampton) otherwise it reports an error
Tail(list) – If the list is non-empty it returns a new list containing all but the first
element of the original list, otherwise it reports an error
Empty(list) – if the list is the empty list it returns True otherwise it returns False.
The empty list is donated by [ ]
(a) What result is returned when the following function calls are made?
______________________________________________________________
(1)
Page 16 of 64
(ii) Head(Tail(Tail(Ports))) ____________________________________________
______________________________________________________________
(2)
______________________________________________________________
(2)
A recursively defined procedure P, which takes a list as its single parameter, is defined
below.
Define Procedure P(list)
If Not Empty(list)
Then
P(Tail(list))
Print Head(list)
EndIf
EndDefine
___________________________________________________________________
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(d) For the procedure call P(Ports) give the PRINTed output in the order in which it is
produced.
___________________________________________________________________
___________________________________________________________________
(4)
(e) Complete the table to show the list Ports as a linked list so that the ports can be
accessed in alphabetical order.
Page 17 of 64
(2)
(Total 14 marks)
Q13.
A stack is a type of abstract data type (ADT) that is often known as a LIFO data type. A
stack with a single element 27 may be drawn as follows:
___________________________________________________________________
(1)
(b) A stack has two operations, Push and Pop. Push n adds item n to stack. Pop
removes one item from the stack. A number of operations are performed, in
sequence, on the stack drawn above. Using the stack diagrams below show the
effect of this sequence of operations.
(i) Push 5
(1)
(ii) Push 9
Page 18 of 64
(1)
(iii) Pop
(1)
(iv) Push 6
(1)
___________________________________________________________________
(1)
(Total 6 marks)
Q14.
(b) Queue and stack are examples of data structures. Tick in the following table to
indicate whether they are FIFO or LIFO data structures.
FIFO LIFO
Queue
Stack
(2)
Page 19 of 64
(c) Describe one example of the use of a stack.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 8 marks)
Q15.
Reverse Polish Notation is an alternative to standard infix notation for writing arithmetic
expressions.
(a) Convert the following Reverse Polish Notation expressions to their equivalent infix
expressions.
45 6 +
12 19 + 8 *
(2)
(b) State one advantage of Reverse Polish Notation over infix notation.
___________________________________________________________________
___________________________________________________________________
(1)
(c) The pseudo-code algorithm below can be used to calculate the result of evaluating a
Reverse Polish Notation expression that is stored in a string. The algorithm is
designed to work only with the single digit denary numbers 0 to 9. It uses
procedures and functions listed in the table below, two of which operate on a stack
data structure.
StringPos ← 0
Repeat
StringPos ← StringPos + 1
Token ← GetCharFromString(InputString, StringPos)
If Token = ‘+’ Or Token = ‘-’ Or Token = ‘/’ Or Token = ‘*’
Then
Op2 ←Pop()
Op1 ← Pop()
Case Token Of
‘+’: Result ← Op1 + Op2
‘-’: Result ← Op1 - Op2
Page 20 of 64
‘/’: Result ← Op1 / Op2
‘*’: Result ←Op1 * Op2
EndCase
Push(Result)
Else
IntegerVal ←ConvertToInteger(Token)
Push(IntegerVal)
EndIf
Until StringPos = Length(InputString)
Output Result
(d) Complete the table below to trace the execution of the algorithm when InputString
is the string: 64+32+*
In the Stack column, show the contents of the stack once for each iteration of the
Repeat..Until loop, as it would be at the end of the iteration.
The first row and the leftmost column of the table have been completed for you.
0 - - - - -
Page 21 of 64
1
(5)
Write a pseudo-code algorithm for the Push operation to push a value stored in the
Page 22 of 64
variable ANumber onto the stack.
Your algorithm should cope appropriately with any potential errors that might occur.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(4)
(Total 13 marks)
Q16.
(a) State the principle of operation of a set of data values which behave as a stack.
___________________________________________________________________
___________________________________________________________________
(1)
(b) Memory locations 600 to 605 are to be used as a stack area to store character data,
and the first value added to the stack is to be stored at address 600.
Figure 1
600
601
602
603
604
605
(i) Show on Figure 2 the state of the stack after the characters ‘A’, ‘V’, ‘E’, ‘R’
and ‘Y’ join the stack.
Figure 2
600
Page 23 of 64
601
602
603
604
605
(1)
(ii) Two items are removed from the stack. Show on Figure 3 the state of the
stack.
Figure 3
600
601
602
603
604
605
(1)
(iii) Two new characters ‘S’ and ‘P’ join the stack. Show on Figure 4 the final state
of the stack.
Figure 4
600
601
602
603
604
605
(1)
(c) The original items in this stack are to be reversed. This can be done using a second
data structure which uses locations 700 to 705 respectively. The first item added to
the stack was character ‘A’.
Figure 5
Page 24 of 64
600 ‘A’ 700 600
Stack Stack
(before the operation) (i) ____________ (after the operation)
______________________________________________________________
(1)
______________________________________________________________
______________________________________________________________
(1)
______________________________________________________________
______________________________________________________________
(1)
(iv) Show on Figure 5 the final contents of all the memory locations.
(2)
(Total 9 marks)
Q17.
A queue data structure can be implemented as a static data structure using an array.
(a) Describe the method that would need to be followed to attempt to remove an item
from a circular queue implemented as a static data structure using an array.
Your method should deal appropriately with any issues which could arise.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Page 25 of 64
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(4)
(b) Describe three differences between dynamic and static data structures.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(3)
The figure below shows data that has been stored in a stack implemented using an array
S.
[7]
[6]
[5]
[4]
[3]
[2] Jib Top = 2
[1] Skye
[0] Harry
___________________________________________________________________
___________________________________________________________________
(1)
___________________________________________________________________
___________________________________________________________________
(1)
Page 26 of 64
(e) Explain how a single stack can be used to reverse the order of the items in a queue.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 11 marks)
Q18.
Describe the process that should be followed to add an item to a circular queue
implemented as a static data structure using an array.
Your method should deal appropriately with any issues which could arise.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 5 marks)
Q19.
Priority queues and linear queues are examples of data structures.
Describe the steps involved in adding an item to a linear queue that has been
implemented as a static data structure using an array. Your answer should include a
description of how any pointers are used and changed.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Page 27 of 64
_______________________________________________________________________
(Total 3 marks)
Q20.
A computer program is being developed to play a card game on a smartphone. The game
uses a standard deck of 52 playing cards, placed in a pile on top of each other.
The cards will be dealt (ie given out) to players from the top of the deck.
(a) Explain why a queue is a suitable data structure to represent the deck of cards in
this game.
___________________________________________________________________
___________________________________________________________________
(1)
(b) The queue representing the deck of cards will be implemented as a circular queue
in a fixed-size array named DeckQueue. The array DeckQueue has indices running
from 1 to 52.
The figure below shows the contents of the DeckQueue array and its associated
pointers at the start of a game. The variable QueueSize indicates how many cards
are currently represented in the queue.
(i) Twelve cards are dealt from the top of the deck.
What values are now stored in the FrontPointer and RearPointer pointers
and the QueueSize variable?
QueueSize = ___________
(1)
(ii) Next, a player gives up three cards and these are returned to the deck.
What values are now stored in the FrontPointer and RearPointer pointers
Page 28 of 64
and the QueueSize variable?
QueueSize = ___________
(1)
Your algorithm should output the value of the card that is to be dealt and make any
required modifications to the pointers and to the QueueSize variable.
It should also cope appropriately with any situation that might arise in the
DeckQueue array whilst a game is being played.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(6)
(Total 9 marks)
Q21.
Describe how a single stack could be used to evaluate an RPN expression.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Page 29 of 64
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
(Total 4 marks)
Q22.
A graph can be drawn to represent a maze. In such a graph, each graph vertex
represents one of the following:
• a dead end.
Diagram 1 shows a maze and Diagram 2 shows one possible representation of this
maze.
Position 1 in Diagram 1 corresponds to vertex 1 in Diagram 2 and is the entrance to the
maze. Position 7 in Diagram 1 is the exit to the maze and corresponds to vertex 7.
Dead ends have been represented by the symbol in Diagram 2.
Diagram 3 shows a simplified undirected graph of this maze with dead ends omitted.
Diagram 2 Diagram 3
Page 30 of 64
Representation of maze Graph representing maze
including dead ends with dead ends omitted
___________________________________________________________________
___________________________________________________________________
(1)
Describe a feature of a maze that would result in its graph not being a tree.
___________________________________________________________________
___________________________________________________________________
(1)
(c) Complete the table below to show how the graph in Diagram 3 would be stored
using an adjacency matrix.
Page 31 of 64
(2)
______________________________________________________________
______________________________________________________________
(1)
(ii) To enable the use of recursion a programming language must provide a stack.
Explain what this stack will be used for and why a stack is appropriate.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
(2)
Diagram 3 is repeated here so that you can answer Question (e) without having to turn
pages.
Page 32 of 64
(e) A recursive routine can be used to perform a depth-first search of the graph that
represents the maze to test if there is a route from the entrance (vertex 1) to the exit
(vertex 7).
The recursive routine in the diagram below is to be used to explore the graph in
Diagram 3. It has two parameters, V (the current vertex) and EndV (the exit vertex).
Complete the trace table below to show how the Discovered and
CompletelyExplored flag arrays and the variable Found are updated by the
algorithm when it is called using DFS(1,7).
The details of each call and the values of the variables V, U and EndV have already
been entered into the table for you. The letter F has been used as an abbreviation
for False. You should use T as an abbreviation for True.
Page 33 of 64
(5)
(Total 12 marks)
Q23.
Describe the steps involved in adding a record to a hash table.
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
_______________________________________________________________________
Page 34 of 64
(Total 5 marks)
Q24.
A dictionary is an abstract data type that allows pairs of values to be associated with each
other. For example, an English-French dictionary might associate English words with their
translations into French. In such a dictionary, "Apple" would be associated with "Pomme"
because "Pomme" is the French word for "Apple".
In each implementation, a record containing the English word and the equivalent French
word are stored at each index in the array that is in use.
The figure below shows how an English-French dictionary containing five words could be
implemented using these two methods.
[6] [6]
[9] [9]
(a) Explain why, when the French translation of an English word needs to be looked up,
Implementation Two is more time efficient than Implementation One.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Page 35 of 64
___________________________________________________________________
(2)
(b) In Implementation Two, it is possible that the hash function could compute the
same value for two different English words.
Explain what the effect of this would be, and how it could be dealt with.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(c) In Implementation Two, both the English and French words are stored at each
index in the Array. In this implementation, explain why it would not be possible to
perform reliable English to French translation if only the French words were stored.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(1)
(Total 5 marks)
Q25.
The graph below shows two vectors a and b and the angle between them, c.
Page 36 of 64
a = [4, 3]
b = [4, 0]
The magnitude of a vector, represented as an arrow, is the length of the arrow. The
magnitude of vector a is 5 because
___________________________________________________________________
___________________________________________________________________
(1)
(b) Calculate the dot product of vectors a and b. You should show your working.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
The angle between two vectors cannot be larger than 180 o. If the angle is measured as
being greater than 180o then this angle is subtracted from 360o to find the actual angle
between the two vectors.
(c) Describe what will happen to the angle c and the magnitude of vector a when vector
a is multiplied by the scalar 2
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(d) Describe what will happen to the angle c and the magnitude of vector a when vector
a is multiplied by the scalar -1
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
(2)
(Total 7 marks)
Page 37 of 64
Mark schemes
Q1.
(a) Mark is for AO2 (analyse)
There will end up being two copies of the second card (A. a card);
The first card (A. a card) will be overwritten;
Max 1
1
Q2.
(a) +3x;
1
(b) 3+x;
1
(c) 3x+;
1
[3]
Q3.
(a)
Page 38 of 64
Correct root + left subtree;
Correct root + right subtree;
Q4.
(a) +;
4, 9, 6; (in any order)
2
(d) One mark for each area outlined with a dark rectangle. Lines that are not
outlined can be missed out.
Alternative 1Alternative 2
Page 39 of 64
Mark against whichever alternative gives the highest mark.
(e) Post-order;
A Depth-first
A Depth-first search as BOD
TO Depth-first pre / in-order
1
Q5.
(a) All marks for AO1 (understanding)
Each node has no more than two child nodes; R. has two child nodes
2
1 0
2 −1
3 True
4 Current ← Tree[Current].Right
5 Current ← Tree[Current].Left
6 False
Page 40 of 64
Note for examiners: answers are in pseudo-code so accept any reasonable
representation (including use of string or integer values for rows 3 and 6).
Mark as follows:
O(log2n);
I. missing brackets
I. missing O
I. missing 2
1
Every comparison halves the size of the binary tree to look at; (A. every
comparison halves the size of the tree)
1
Max 1
1
Max 2
2
As the size of the input/problem increases; the amount of time taken remains
the same;
2
[13]
Q6.
Page 41 of 64
(a)
(b) Adjacency matrix appropriate when there are many edges between vertices //
when edges may be frequently changed // when presence/absence of specific
edges needs to be tested (frequently)
Adjacency list appropriate when there are few edges between vertices // when
graph is sparse // when edges rarely changed //when presence/absence of
specific edges does not need to be tested (frequently)
A alternative words which describe edge e.g. connection, line
2
(d)
Page 42 of 64
1 mark for Jack as root
1 mark for Bramble and Snowy as children of Jack
1 mark for four correct children of Bramble and Snowy
Q7.
(a) All marks AO2 (analyse)
1 2 3 4 5 6
1 0 2 5 3 0 8
2 2 0 1 0 0 0
Page 43 of 64
3 5 1 0 0 0 4
4 3 0 0 0 1 0
5 0 0 0 1 0 5
6 8 0 4 0 5 0
Alternative answer
1 2 3 4 5 6
1 0 2 5 3 0 8
2 0 1 0 0 0
3 0 0 0 4
4 0 1 0
5 0 5
6 0
Alternative answer
1 2 3 4 5 6
1 0
2 2 0
3 5 1 0
4 3 0 0 0
5 0 0 0 1 0
6 8 0 4 0 5 0
Mark as follows:
I. non-zero symbols used to denote no edge but only for showing no edge
going from a node to itself
2
Adjacency list appropriate when there are few edges between vertices // when
graph/matrix is sparse; NE. few edges
Page 44 of 64
A. Alternative words which describe edge, eg connection, line, arc
Max 2
2
Mark as follows:
I. output column
1 mark first value of A is 2
1 mark second value of A is 5 and third value is 3
1 mark fourth and subsequent values of A are 8, 3, 7, 4, 9 with no more values
after this
1 mark D[2] is set to 2 and then does not change
Page 45 of 64
1 mark D[3] is set to 5 and then changes to 3 and does not change again
1 mark correct final values for each position of array P
Page 46 of 64
1 mark correct final values for D[1], D[4], D[5], D[6]
Page 47 of 64
Max 6 marks if any errors
7
Used to store the previous node/location in the path (to this node);
Max 1 if not clear that the values represent the shortest path
Page 48 of 64
Alternative answer
Max 1 if not clear that the values represent the shortest path
2
[16]
Q8.
Mark is for AO1 (understanding)
Q9.
All marks for AO1 (understanding)
Adjacency matrix appropriate when there are many edges between vertices // when
graph/matrix is not sparse; when edges frequently changed; when presence/absence of
specific edges needs to be tested frequently;
Max 2 marks
Q10.
Repeat
S→Q
Until Q empty
Queue emptied to a stack
Elements taken from front of queue and placed / pushed on stack
2 marks
Repeat
Q→S
Until S empty
Stack emptied to a queue
Elements popped/taken from top of stack placed in queue
2 marks
Or suitable diagram
1 mark
[4]
Q11.
Queue is FIFO ; (1)
Stack is LIFO; (1)
Given that:
Page 49 of 64
Process of taking elements from queue to stack(1)
Process of popping stack(1)
[4]
Q12.
[Athens] (1)
2
True [ ] (1)
[True] (0)
2
(e)
Page 50 of 64
2
[14]
Q13.
(b) (i)
(ii)
(iii)
(iv)
Page 51 of 64
(c) To reverse elements/ pass parameters/ store volatile environment;
A store return address
1
[6]
Q14.
(b)
FIFO LIFO
Queue
Stack
2
Q15.
(a)
Reverse Polish Notation Equivalent Infix Expression
45 6 + 45 + 6
R 6 + 45
12 19 + 8 * (12 + 19) * 8
R 12+19*8, (19+12)*8
A x for *
2
Page 52 of 64
Operators appear in the order required for computation;
No need for order of precedence of operators;
No need to backtrack when evaluating;
A RPN expressions cannot be ambiguous as BOD
1
(c)
Output : 50
1 mark for appropriate If structure including condition (does not need both
Page 53 of 64
Then and Else) – Do not award this mark if ANumber is put into StackArray
outside the If.
1 mark for reporting error in correct place
1 mark* for incrementing TopOfStackPointer
1 mark* for storing value in ANumber into correct position in array
* = if the store instruction is given before the increment instruction OR
the If structure then award Max 1 of these two marks UNLESS the item is
inserted at position TopOfStackPointer+1 so the code would work.
I initialisation of TopOfStackPointer to 0
A TopOfStackPointer=20/>=20 for Stack is full
A Logic of if structure reversed i.e. If stack is not full /
TopOfStackPointer<20 / <>20/!=20 and Then, Else swapped
A Any type of brackets or reasonable notation for the array index
DPT If candidate has used a different name any variable then do not award
first mark but award subsequent marks as if correct name used.
Refer answers where candidate has used a loop to find position to insert item
into stack to team leaders.
4
[13]
Q16.
(a) Last (item) in, is the first (item) out / first (item) in is the last (item) out ;
R LIFO / FILO
1
(b) (i)
600 ‘A’
601 ‘V’
602 ‘E’
603 ‘R’
604 ‘Y’ ;
605
(ii)
599
600 ‘A’
601 ‘V’
602 ‘E’ ;
603
604
Page 54 of 64
605
Correct three items // ft from an incorrect (i) including 605 as the first
location used ;
A ‘R’ and ‘Y’ entries indicated in some way as ‘deleted’
1
(iii)
600 ‘A’
601 ‘V’
602 ‘E’
603 ‘S’
604 ‘P’ ;
605
(ii) Items are removed/popped from the stack (one at a time) (and items are
then added to the queue);
1
(iii) Items leave the queue on a ‘first in-first out’ basis ; A from the front of
the queue
1
Q17.
(a) All marks for AO1 (understanding)
Alternative answer 1
Page 55 of 64
array minus one;
3. If equal then front pointer becomes zero; A. index of the first position in
the array instead of zero
4. Otherwise, add one to the front pointer;
Alternative answer 2
Alternative answer 3
Alternative answer 4
Static data structures can waste storage space / memory if the number of data
items stored is small relative to the size of the structure
//
dynamic data structures only take up the amount of storage space required for
the actual data;
Dynamic data structures require (memory to store) pointers to the next item(s)
// static data structures (typically) do not need (memory to store) pointers;
Max 3
3
Page 56 of 64
(c) Mark is for AO2 (apply)
Jib;
1
Jib;
1
(Until the queue is empty) repeatedly remove / delete (the front item) from the
queue and push it on to the stack;
(Until the stack is empty) repeatedly pop items from the stack and add them to
the (rear of the) queue;
2
[11]
Q18.
All marks AO1 (understanding)
Alternative answer 1
Alternative answer 2
Alternative answer 3
Page 57 of 64
Alternative answer 4
Q19.
All marks for AO1 (understanding)
then add the new item to the position indicated by the rear pointer;
Alternative answer
Check that the queue is not already full;
(if it isn’t) then add the new item to the position indicated by the rear pointer;
Q20.
(a) Values/cards need to be taken out of the data structure from the opposite end
that they are put in // cards removed from top/front and added at
end/bottom/rear;
Values/cards need to be removed in the same order that they are added;
A. It is First In First Out // It is FIFO;
A. It is Last In Last Out // It is LILO;
Max 1
(ii) FrontPointer = 13
RearPointer = 3
QueueSize = 43
Page 58 of 64
(c) If DeckQueue is empty THEN
Report error
ELSE
Output DeckQueue[FrontPointer]
Decrement QueueSize
Increment FrontPointer
IF FrontPointer > 52 THEN
FrontPointer ← 1
ENDIF
ENDIF
Q21.
All marks AO1 (understanding)
Each time operator reached pop top two values off stack (and apply operator to them) //
Each time operator reached pop required number of values off stack (and apply operator
to them);
When end of expression is reached the top item of the stack is the result // when end of
expression is reached pop one value off the stack;
Note for examiners: Award 0 marks if description is not about a stack / LIFO structure
even if the word “stack” has been used.
Page 59 of 64
[4]
Q22.
(c)
or
(with the shaded portion in either half – some indication must be made that
half of the matrix is not being used. This could just be leaving it blank, unless
the candidate has also represented absence of an edge by leaving cells blank)
1 mark for drawing a 7x7 matrix, labelled with indices on both axis and filled
only with 0s and 1s, or some other symbol to indicate presence/absence of
edge. e.g. T/F. Absence can be represented by an empty cell.
1 mark for correct values entered into matrix, as shown above;
2
(d) (i) Routine defined in terms of itself // Routine that calls itself;
Page 60 of 64
A alternative names for routine e.g. procedure, algorithm
NE repeats itself
1
Max 1
Max 1
2
(e)
Page 61 of 64
1 mark for having the correct values changes in each region highlighted by a
rectangle and no incorrect changes in the region. Ignore the contents of any
cells that are not changed.
Q23.
All marks AO1 (understanding)
Page 62 of 64
Q24.
(a) Implementation One would need to use a linear search //
would need to look at every word in the array (before the one
that is being searched for) // lookup time is proportional to
number of words in list // lookup is O(N); N.E. “search”
without further clarification that this would be linear
Implementation Two would use the hash function/hashing
to directly calculate where the word would be stored // could
jump directly to the correct position/location/index for the
word in the array // lookup time is constant regardless of how
many words in list // lookup is O(1); A. No need to go
through words in list
2
(c) The hash function could compute the same value/location for
more than one/two English word(s), so need to verify if the
English word stored at the location is the one that is being
looked up;
To avoid returning a French translation that is for a different
English word, which is stored at the same location as the
word that is being looked up // if a collision occurred (when
storing the words) it will not be possible to tell if the
translation is correct;
Q25.
(a) Mark is for AO2 (apply)
4
//
2 marks: 16
Page 63 of 64
If final answer is incorrect then award a maximum of 1 mark for working:
The angle will still be the same; A. the direction will not change
The angle will be 180 – c // the angle will be 360 – 180 – c // the angle will be
143.13;
Page 64 of 64