BIT201 ยท 2082 ยท 5 marks
Traverse the following tree in pre-order and in-order.
Traverse the following tree in pre-order and in-order. [5]
Given
Task: Traverse a given tree in pre-order and in-order.
Tree structure: The tree diagram/image is NOT provided in the question. Only the instruction and the mark allocation ([5]) are given.
Missing data: The actual tree (nodes and their parent/child relationships) is missing. Without the specific tree, an exact, verifiable set of traversal sequences cannot be produced. The correct output depends entirely on the omitted diagram.
Since the tree cannot be recovered, I will state the required methods precisely and demonstrate them on a clearly labelled assumed sample tree (so the traversal logic can still be checked), while noting this is illustrative only.
Solution
Assumed Sample Tree
A
/ \
B C
/ \ / \
D E F G
1. Pre-order Traversal (Root โ Left โ Right)
Rule: visit Root, then Left subtree, then Right subtree.
- Visit A (root)
- Left subtree of A (rooted at B): visit B, then D, then E
- Right subtree of A (rooted at C): visit C, then F, then G
$$\text{Pre-order: } A \to B \to D \to E \to C \to F \to G$$
2. In-order Traversal (Left โ Root โ Right)
Rule: traverse Left subtree, visit Root, traverse Right subtree.
- Left subtree of A: D, B, E
- Visit A
- Right subtree of A: F, C, G
$$\text{In-order: } D \to B \to E \to A \to F \to C \to G$$
Summary
| Traversal | Order | Result |
|---|---|---|
| Pre-order | Root, Left, Right | A, B, D, E, C, F, G |
| In-order | Left, Root, Right | D, B, E, A, F, C, G |
Important note: These results are valid only for the assumed sample tree shown above. Because the actual tree from the exam paper was not supplied, the definitive answer cannot be produced. If the real tree differs, apply the same rules:
- Pre-order = Root, Left, Right
- In-order = Left, Root, Right
Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.