25-Comp-A4 Program Design and Data Structures · December 2018
Question 8 of 9: Pointer-based Data Structures — Inorder Traversal
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
Notes on this paper
Pseudocode or any
high-level language (e.g. C or C++) 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.
Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms,
4th ed. — tree walks (ch. 12), stacks and linear-time scans (ch. 10, 2),
asymptotic analysis (ch. 3).
Weiss, Data Structures and Algorithm Analysis in C, 2nd ed. —
arrays and dynamic 2-D allocation (ch. 1), linked lists (ch. 3), stacks
(ch. 3.3), binary trees (ch. 4).
Deitel & Deitel, C++ How to Program, 10th ed. — class
design, templates and the Rule of Three (ch. 9–12), file streams
(ch. 14).
Kernighan & Ritchie, The C Programming Language, 2nd ed.
— character/file I/O idioms (ch. 1, 7), pointers and structures
(ch. 5–6).
Stroustrup, The C++ Programming Language, 4th ed. — value
semantics, const-correctness and templates (ch. 3, 16–18).
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: Pointer-based Data Structures — Inorder Traversal (20 marks)
Check: the printed TreeNode
struct does not compile as given. Its left/right
fields are declared struct element *, but no type named
element is in scope here (and even if there were, it would be
the wrong type for a self-referential tree node). This is likely a slip in the printed paper. It is fixed
below to the obvious intended declaration, struct treenode *left,
*right, which is what makes the type genuinely self-referential, as
its own name requires.
Given. A binary tree node holding an int and
two child pointers, with no ordering assumption stated (unlike a BST, nothing
here requires left < node < right) — the function must work for
an arbitrary binary tree shape.
Find. A function that prints every node's data in
inorder (left, node, right) order, and does nothing on an empty tree.
Approach. This is a direct recursive tree walk: the base
case is an empty subtree (root == NULL), and the recursive case
visits the left subtree, then the node itself, then the right subtree.
State the recursive definition and implement it directly.
Traversing an empty subtree does nothing, which is exactly the required
empty-tree behaviour — no special-case branch is needed beyond the base
case itself.
Trace it on a worked example tree (8 at the root; left
subtree rooted at 3 with children 1 and 6, 6 itself having children 4 and 7;
right subtree rooted at 10 with a right child 14, which itself has a left
child 13):
the recursion visits 1, 3, 4, 6, 7, 8, 10, 13, 14, in that order — every
node's left subtree is fully printed before the node itself, which prints
before its right subtree.
$$\boxed{\text{inorder} = 1,\,3,\,4,\,6,\,7,\,8,\,10,\,13,\,14}$$