25-Comp-A4 Program Design and Data Structures · December 2017
Question 8 of 9: Binary Trees — Traversals and Reconstruction
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
Notes on this paper
Paper format. 98-Comp-A4 Program Design and Data
Structures, December 2017 — 3 hours, closed book, no calculator
permitted. Nine questions of equal weight (20 marks each: 1 and 7 split as
(a) 10 + (b) 10, 8 split as (a) 15 + (b) 5); candidates answer any six, so a
complete paper is 120 marks. 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.
All nine questions are answered below, because the whole set
is the more useful revision resource. Answers are given in C or C++ as the
question dictates; each is compilable as written (or corrected where the printed paper itself has a slip), but a clear, correctly reasoned
pseudocode answer would earn the same marks.
Reference texts for this subject.
Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms,
4th ed. — tree traversals and BSTs (ch. 12), recursion and
divide-and-conquer (ch. 2, 4), asymptotic analysis (ch. 3).
Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. —
linked lists and stacks (ch. 3), binary trees (ch. 4).
Deitel & Deitel, C++ How to Program, 10th ed. — class
design and operator overloading (ch. 9–11), file streams (ch. 14).
Kernighan & Ritchie, The C Programming Language, 2nd ed. —
arrays and file I/O (ch. 1, 7), pointers, structures and linked
lists (ch. 5–6).
Stroustrup, The C++ Programming Language, 4th ed. — class
templates and value semantics (ch. 3, 25–27).
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 8: Binary Trees — Traversals and Reconstruction (20 marks: (a) 15, (b) 5)
Given. (a) The pictured 9-node binary tree (root F;
F→D,A; D→C,Q; C→(E,—); Q→(S,—);
A→(—,V); V→(X,—)).
(b) Preorder $1,8,12,25,13,7,9$ and inorder $8,1,25,12,7,13,9$ of a 7-node
tree with all-distinct values.
Find. (a) The preorder, inorder and postorder sequences
of the pictured tree. (b) The unique tree consistent with both given
traversals.
Approach. (a) Apply the three standard recursive
definitions (root position relative to the two subtree traversals) directly
to the pictured structure. (b) Use the standard preorder+inorder
reconstruction algorithm: the first preorder value is always the (sub)tree's
root; its position in the corresponding inorder slice splits that slice into
the left- and right-subtree inorder sequences, whose sizes then split the
remaining preorder values the same way, recursively.
(a) Traversals of the pictured tree (15 marks)
The pictured tree: root F; left subtree rooted at D (children C, Q; C has left-only child E; Q has left-only child S); right subtree rooted at A (right-only child V, which itself has left-only child X).
Preorder (root, left, right). Starting at F: visit F,
then the whole left subtree (D first, then C's subtree C,E, then Q's
subtree Q,S), then the whole right subtree (A, then V, then X).
$$\boxed{\text{preorder}=F,D,C,E,Q,S,A,V,X}$$
Inorder (left, root, right). C's subtree contributes
$E,C$ (E is C's only, left, child); Q's subtree contributes $S,Q$ (S is
Q's only, left, child); D's subtree is therefore $E,C,D,S,Q$; V's subtree
is $X,V$ (X is V's only, left, child) and A has no left child, so A's
subtree is $A,X,V$; the whole tree is D's inorder, then F, then A's
inorder.
$$\boxed{\text{inorder}=E,C,D,S,Q,F,A,X,V}$$
Postorder (left, right, root). C's subtree: $E,C$;
Q's subtree: $S,Q$; D's subtree: $E,C,S,Q,D$; V's subtree: $X,V$; A's
subtree: $X,V,A$; whole tree: D's subtree, then A's subtree, then F.
$$\boxed{\text{postorder}=E,C,S,Q,D,X,V,A,F}$$
(b) Reconstructing the tree (5 marks)
Split on the root at each level. Preorder's first
value, 1, is the root; in the inorder list $8,1,25,12,7,13,9$, value 1 sits
after just $\{8\}$, so the left subtree is the single node 8 and the right
subtree's inorder is $25,12,7,13,9$ (5 nodes). The next preorder value after
the 1 left-subtree node, 12, is therefore the right subtree's root; in
$25,12,7,13,9$ it splits into left $\{25\}$ and right $\{7,13,9\}$. The next
preorder value, 13, is that right-right subtree's root, splitting
$7,13,9$ into left $\{7\}$ and right $\{9\}$.
Draw the resulting tree.
The reconstructed tree T: root 1 (left child 8, a leaf; right child 12); 12's left child is 25 (a leaf) and right child is 13; 13's left child is 7 and right child is 9 (both leaves).
Re-deriving preorder and inorder from this tree reproduces exactly the
two sequences given in the question
($1,8,12,25,13,7,9$ and $8,1,25,12,7,13,9$), confirming the reconstruction.
$$\boxed{T:\ 1(8,\ 12(25,\ 13(7,9)))}$$
Question 8 — results
Item
Result
(a) Preorder
F, D, C, E, Q, S, A, V, X
(a) Inorder
E, C, D, S, Q, F, A, X, V
(a) Postorder
E, C, S, Q, D, X, V, A, F
(b) Reconstructed tree
root 1; left leaf 8; right subtree 12(25, 13(7,9))