25-Comp-A4 Program Design and Data Structures · December 2016
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Pseudocode or any high-level language is accepted, and the examiner's note states explicitly that marking emphasises the operation of the program, not syntactic details.
Reference texts for this subject.
The Computer Engineering citation list is built around architecture and networking texts (Patterson & Hennessy, Tanenbaum, Mano); this subject is programming and data structures, so the works above are cited instead.
Question text not reproduced: the examination questions are © Engineers and Geoscientists BC. Open the official past paper (linked at the top of this page) to read the question, then follow the worked solution below.
Check: the question's own stated BST rule ("smaller than its right child... greater than its left child") describes only the immediate parent–child relationship, not the standard whole-subtree BST invariant (every node in a left subtree less than the ancestor, every node in a right subtree greater). The given example tree happens to satisfy the standard, stronger definition as well — every key in 16's subtree (16, 17, 20) is <22 and every key in 28's subtree (24, 28, 33, 36) is >22, and likewise recursively within each subtree — so the answer below uses the standard whole-subtree definition, since that is what makes binary search on the tree valid at all, and is almost certainly the intended meaning.
Given. (a) The 7-node general binary tree pictured. (b) The 8-node BST pictured (root 22).
Find. (a) Its inorder, preorder and postorder sequences. (b) The tree after insert(34), then delete(33), then insert(33), each applied by the standard BST algorithms.
Approach. (a) Apply the three standard recursive traversal orders directly to the pictured shape. (b) Insert always walks down comparing keys until an empty child pointer is found; delete on a node with one child splices that child into the deleted node's place (no successor search needed, since neither 33 nor its replacement in this problem ever has two children).
[Figure not reproduced: Question 9(a) — the given general binary tree (root a ; b has only a right child d ; f has only a left child g ). See the official exam paper.]
a:
visit a, recurse into b's subtree entirely (visit
b, then d's subtree: d, e,
then f's subtree: f, g), then finally
visit c.
$$\boxed{\text{preorder} = a,b,d,e,f,g,c}$$b has no left
child, so b is visited immediately, then its right subtree
rooted at d is traversed inorder (e, then
d, then f's subtree inorder: g, then
f), and finally a and its right child c.
$$\boxed{\text{inorder} = b,e,d,g,f,a,c}$$e then g
then f then d (closing d's subtree),
then b (closing b's subtree), then c,
then finally the root a.
$$\boxed{\text{postorder} = e,g,f,d,b,c,a}$$[Figure not reproduced: Given BST (root 22). See the official exam paper.]
| Item | Result |
|---|---|
| (a) Preorder | a, b, d, e, f, g, c |
| (a) Inorder | b, e, d, g, f, a, c |
| (a) Postorder | e, g, f, d, b, c, a |
| (b) After insert(34) | 34 is right child of 33 |
| (b) After delete(33) | 34 replaces 33 as left child of 36 |
| (b) After insert(33) | 33 is left child of 34 |