Chapter 12 – Graph and tree traversal

AQA A level Computer Science
Teaching and Learning Resources
Chapter 12 – Graph and tree traversal
TASK QUESTIONS (WITH ANSWERS)
1 Draw a binary tree for the following data: Rose, Jasmine, George, Naomi, Trevor and
Stanley.
Rose
Jasmine
George
Trevor
Naomi
Stanley
a) List the nodes that will be visited in order to find the node that stores George.
Rose and Jasmine
b) Traverse the tree in pre-order and write down the value at each node when you
visit it.
Rose, Jasmine, George, Naomi, Trevor, Stanley
c) Repeat this process for a post-order traversal.
George, Naomi, Jasmine, Rose, Stanley, Trevor
d) Repeat this process for an in-order traversal.
George, Jasmine, Naomi. Rose, Stanley, Trevor
2 Explain the term ‘recursion’ and give an example of where it might be used.
Recursion is the process of calling a function from within itself. It is useful for traversing graphs and
trees, as it enables the programmer to write one block of code to traverse the nodes. Conditional
statements can then be used to decide what to do at each node.
3 Write pseudo code to show how the following graph could be traversed: depth first and
breadth first.
B
A
C
Refer students to the tables 12.2 and 12.3 in Chapter 12.
AQA A level Computer Science
© Hodder & Stoughton Limited 2015