NivaarExam PrepOfficial exam papers ↗

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)

Notes on this paper

Paper format. 17-Comp-A4 Program Design and Data Structures, December 2018 — 3 hours, closed book, no calculator permitted. Nine questions, each of equal weight (Question 1 is split 10+10; Questions 2–9 are 20 marks each); candidates answer any six, and only the first six as they appear in the answer book are marked, so the paper is marked out of 120. 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. 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, 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 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)

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 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.

  1. 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.
    #include <stdio.h>
    
    typedef struct treenode {
        int data;
        struct treenode *left;
        struct treenode *right;
    } TreeNode;
    
    void inorder(TreeNode *root)
    {
        if (root == NULL)
            return;                 /* empty (sub)tree: nothing to print */
    
        inorder(root->left);
        printf("%d ", root->data);
        inorder(root->right);
    }
  2. 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}$$
Question 8 — results
QuantityValue
Example tree inorder sequence1, 3, 4, 6, 7, 8, 10, 13, 14
inorder(NULL)prints nothing