The binary tree below stores integers.
(a) Write the order in which the nodes are visited in a pre-order traversal. [1]
(b) Write the order in which the nodes are visited in an in-order traversal. [1]
(c) Write the order in which the nodes are visited in a post-order traversal. [1]
(d) The tree is a binary search tree. Explain how your answer to part (b) shows this. [2]
- (a) 40, 25, 15, 32, 60, 50, 75(1)
- (b) 15, 25, 32, 40, 50, 60, 75(1)
- (c) 15, 32, 25, 50, 75, 60, 40(1)
- (d) In-order traversal gives the values in ascending (sorted) order(1)
- (d) Because every value in a left subtree is smaller than its parent and every value in the right subtree is larger, visiting left, root, right produces ascending order(1)
- Answer: (a) 40, 25, 15, 32, 60, 50, 75 (b) 15, 25, 32, 40, 50, 60, 75 (c) 15, 32, 25, 50, 75, 60, 40 (d) In-order gives sorted order.
- Examiner tip: Pre-order is root-left-right, in-order is left-root-right, post-order is left-right-root – say the rule to yourself before you trace.