Académique Documents
Professionnel Documents
Culture Documents
Data Type
ARRAY[100] OF STRING
LeftP
ARRAY[100] OF INTEGER
RightP
ARRAY[100] OF INTEGER
Root
INTEGER
For
Examiner's
Use
Description
[2]
(ii) What benefit would be gained from using a dynamic data structure to implement a
binary tree?
[1]
UCLES 2013
9691/31/M/J/13
11
The initially empty tree has the following items added in this order:
For
Examiner's
Use
(b) Draw the binary tree after these four items have been added.
[3]
UCLES 2013
9691/31/M/J/13
[Turn over
12
(c) The following algorithm traverses the tree shown and outputs the nodes in order i.e. an
in-order traversal.
PROCEDURE InOrder(Root)
IF LeftP[Root] <> 0
THEN
// move left
InOrder(LeftP[Root])
ENDIF
OUTPUT Data[Root]
Root
PEPPER
PUMPKIN
BEAN
IF RightP[Root] <> 0
THEN
// move right
InOrder(RightP[Root])
ENDIF
ENDPROCEDURE
PEA
(i) Copy a line from procedure InOrder that makes the procedure recursive.
[1]
UCLES 2013
9691/31/M/J/13
For
Examiner's
Use
13
(ii) The diagram shows a trace of the execution of this algorithm for the given tree
data.
For
Examiner's
Use
InOrder(PEPPER)
LeftP[PEPPER] <> 0 is TRUE
InOrder(BEAN)
LeftP[BEAN] <> 0 is FALSE
OUTPUT BEAN
RightP[BEAN] <> 0 is TRUE
InOrder(PEA)
LeftP[PEA] <> 0 is FALSE
OUTPUT PEA
...................................................
ENDPROCEDURE
ENDPROCEDURE
....................................................
RightP[PEPPER] <> 0 is TRUE
InOrder(PUMPKIN)
LeftP[PUMPKIN] <> 0 is FALSE
...................................................
RightP[PUMPKIN] <> 0 is FALSE
..........................
ENDPROCEDURE
[4]
(iii) What do the arrows in the diagram represent?
[1]
UCLES 2013
9691/31/M/J/13
[Turn over
8
4
BEAR
LLAMA
CATERPILLAR
LION
SNAKE
TIGER
[1]
Data type
Description
RootPtr
Data
RightPtr
LeftPtr
UCLES 2014
9691/31/M/J/14
[3]
9
(c) The animals joined the tree in the order:
ELEPHANT, LLAMA, SNAKE, BEAR, LION, CATERPILLAR, TIGER.
The diagram below is to show the contents of the arrays and the root pointer variable.
Complete the diagram.
RootPtr
LeftPtr
1
Data
RightPtr
ELEPHANT
2
3
4
5
6
7
2000
[4]
(d) (i) How many comparisons will be made to locate LION in the current tree?
[1]
(ii) An algorithm is designed in pseudocode. It will search the binary tree for a particular
animal.
The algorithm uses the variables below:
Variable
Data type
Description
SearchAnimal
STRING
Current
INTEGER
IsFound
BOOLEAN
UCLES 2014
9691/31/M/J/14
[Turn over
10
Complete the algorithm below:
//binary tree search
INPUT
IsFound FALSE
Current RootPtr
REPEAT
IF
THEN
//found
OUTPUT 'Found'
IsFound TRUE
ELSE
IF SearchAnimal > Data[Current]
THEN
// move right
ELSE
Current LeftPtr[Current]
ENDIF
ENDIF
UNTIL
OR Current = 0
IF
THEN
OUTPUT SearchAnimal ' not found'
[5]
ENDIF
UCLES 2014
9691/31/M/J/14
9
4
ATHLETICS
SOCCER
HOCKEY
SAILING
SWIMMING
WRESTLING
The binary tree is implemented in a high-level language with the following data structures:
Variable
Data type
Description
RootPtr
Data
RightPtr
LeftPtr
[2]
UCLES 2014
[2]
9691/33/M/J/14
[Turn over
10
(c) The first seven sports joined the tree in the order:
JUDO, SOCCER, SAILING, SWIMMING, ATHLETICS, WRESTLING, HOCKEY.
Complete the diagram below showing the contents of the arrays and the RootPtr variable.
RootPtr
LeftPtr
1
Data
RightPtr
JUDO
2
3
4
5
6
7
2000
[4]
UCLES 2014
9691/33/M/J/14
11
(d) An algorithm is designed in pseudocode to search the binary tree for a particular sport.
The algorithm uses the variables below:
Variable
Data type
Description
SearchSport STRING
Current
INTEGER
IsFound
BOOLEAN
UCLES 2014
9691/33/M/J/14
[Turn over
12
The algorithm is tested using this new dataset.
1
RootPtr
LeftPtr
Data
RightPtr
KARATE
ARCHERY
ROWING
HANDBALL
RUGBY
LACROSSE
SHOOTING
2000
Complete the trace table below to search for LACROSSE.
SearchSport
IsFound
Current
LACROSSE
FALSE
OUTPUT
[5]
UCLES 2014
9691/33/M/J/14
[1]
At execution time
[1]
[1]
[MAX 2]
[1]
[1]
[1]
[MAX 1]
(b)
Root is MELON1
Correct left subtree
Correct right subtree
[1]
[1]
[1]
(c)
[1]
(ii) InOrder(PEPPER)
LeftP[PEPPER] <> 0 is TRUE
InOrder(BEAN)
LeftP[BEAN] <> 0 is FALSE
OUTPUT BEAN
RightP[BEAN] <> 0 is TRUE
InOrder(PEA)
LeftP[PEA] <> 0 is FALSE
OUTPUT PEA
RightP[PEA] <> 0 IS FALSE
ENDPROCEDURE
ENDPROCEDURE
OUTPUT PEPPER
RightP[PEPPER] <> 0 is TRUE
InOrder(PUMPKIN)
LeftP[PUMPKIN] <> 0 is FALSE
OUTPUT PUMPKIN
RightP[PUMPKIN] <> 0 is FALSE
ENDPROCEDURE
ENDPROCEDURE
[4]
(iii) The procedure has to backtrack/unwind from the current call
To return to the calling procedure // return to the addresses from which called
[1]
[1]
[MAX 1]
[Total: 12]
(a) (i)
(ii)
(b)
BEAR - CATERPILLAR
[1]
3 leaf nodes
[1]
INTEGER
(1)
ARRAY[1 : 2000]
(OF) STRING / TEXT
(1)
(1)
[3]
(1)
(1)
(1)
(1)
[4]
(c)
RootPtr 1
1
2
3
4
5
6
7
LeftPtr
4
5
(0)
(0)
(0)
(0)
(0)
Data
ELEPHANT
LLAMA
SNAKE
BEAR
LION
CATERPILLAR
TIGER
RightPtr
2
3
7
6
(0)
(0)
0
Mark as follows:
Root = 1
Elephant pointers 4 and 2
Six names entered
Other pointers correct
(d) (i)
[1]
(ii)
// SearchAnimal = Data[Current]
OR Current = 0
IF
Current = 0
// IsFound = FALSE // NOT IsFound
// IsFound = FALSE AND Current = 0
THEN
OUTPUT SearchAnimal
ENDIF
[5]
(a) INTEGER
ARRAY[1 : 2000] OF (datatype>
OF STRING
[MAX 2]
(b)
[2]
(c)
1
RootPtr
LeftPtr
Data
RightPtr
r
JUDO
SOCCER
SAILING
SWIMMING
ATHLETICS
WRESTLING
HOCKEY
Root = 1
JUDO pointers 5 and 2
Six names entered
Other pointers correct
(1)
(1)
(1)
(1)
[4]
(d)
SearchSport
IsFound
Current
LACROSSE
FALSE
OUTPUT
Moving right
3
Moving left
6
Found
TRUE
[MAX 5]
One mark per entry (MAX 5)