Вы находитесь на странице: 1из 17

10

A binary tree is implemented with three 1-dimensional arrays.

Identifier
Data

Data Type
ARRAY OF STRING

Stores the data values

LeftP

ARRAY OF INTEGER

Stores the left index pointer

RightP

ARRAY OF INTEGER

Root

INTEGER

For
Examiner's
Use

Description

(a) An array is a static data structure.

(i) Explain the difference between a static and a dynamic data structure.


(ii) What benefit would be gained from using a dynamic data structure to implement a
binary tree?



UCLES 2013

9691/31/M/J/13

11
The initially empty tree has the following items added in this order:

For
Examiner's
Use

MELON, PEAR, BANANA, ORANGE

(b) Draw the binary tree after these four items have been added.



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.


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

Fill in the missing lines of code.

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

(iii) What do the arrows in the diagram represent?



UCLES 2013

9691/31/M/J/13

[Turn over

8
4

A binary tree is maintained for a list of animals.

The tree currently has seven animals as shown below:
ELEPHANT

BEAR

LLAMA

CATERPILLAR

LION

SNAKE

TIGER



(ii) State the number of leaf nodes in the current tree.


The binary tree is implemented in a high-level language with the following
data structures:
Variable

Data type

Description

RootPtr

Data

RightPtr

LeftPtr

(b) Complete the two table entries above.

UCLES 2014

9691/31/M/J/14



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

(d) (i) How many comparisons will be made to locate LION in the current tree?

(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

Flag set to TRUE when SearchAnimal is found

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


ENDIF

UCLES 2014

9691/31/M/J/14

9
4

A binary tree is used to store a list of sports.

The tree currently has seven sports as shown below:
JUDO

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



(b) Two new sports, CYCLING and TAI-CHI are to be added.

Add these to the diagram above.

UCLES 2014



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


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

//binary tree search

INPUT SearchSport
IsFound FALSE
Current RootPtr
REPEAT
IF SearchSport = Data[Current]
THEN
OUTPUT 'found'
IsFound TRUE
ELSE
IF SearchSport > Data[Current]
THEN
OUTPUT 'Moving right'
Current RightPtr[Current]
ELSE
OUTPUT 'Moving left'
Current LeftPtr[Current]
ENDIF
ENDIF
UNTIL IsFound = TRUE OR Current = 0
IF Current = 0
THEN
ENDIF

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



UCLES 2014

9691/33/M/J/14

(a) (i) Dynamic data structure changes size ...



At execution time




[MAX 2]



Takes memory from heap as required //

returns memory as required (following node deletion)



There is no wasted memory space / makes efficient use of memory


[MAX 1]

(b)

Root is MELON1
Correct left subtree
Correct right subtree





(c)

(i) InOrder(LeftP[Root]) // InOrder(RightP[Root])



(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

(iii) The procedure has to backtrack/unwind from the current call



[MAX 1]
[Total: 12]

Cambridge International Examinations 2013

(a) (i)
(ii)
(b)

BEAR - CATERPILLAR



3 leaf nodes



INTEGER

(1)

ARRAY[1 : 2000]
(OF) STRING / TEXT

(1)
(1)



(1)
(1)
(1)
(1)



(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)



(ii)

//binary tree search

INPUT SearchAnimal
IsFound FALSE
Current RootPtr
REPEAT
IF SearchAnimal = Data[Current]
THEN
//found
OUTPUT 'Found'
IsFound TRUE
ELSE
IF SearchAnimal > Data[Current]
THEN
// move right

Current RightPtr[Current] (A. =)

ELSE
Current LeftPtr[Current]
ENDIF
ENDIF
UNTIL IsFound( = TRUE)

// SearchAnimal = Data[Current]
OR Current = 0
IF

Current = 0
// IsFound = FALSE // NOT IsFound
// IsFound = FALSE AND Current = 0

THEN
OUTPUT SearchAnimal
ENDIF

Cambridge International Examinations 2014



(a) INTEGER
ARRAY[1 : 2000] OF (datatype>
OF STRING

[MAX 2]

(b)


(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)

Cambridge International Examinations 2014



(d)
SearchSport

IsFound

Current

LACROSSE

FALSE

OUTPUT

Moving right
3
Moving left
6
Found
TRUE
[MAX 5]
One mark per entry (MAX 5)