25-Comp-A4 Program Design and Data Structures · December 2018
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)
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.
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 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.
#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);
}| Quantity | Value |
|---|---|
| Example tree inorder sequence | 1, 3, 4, 6, 7, 8, 10, 13, 14 |
| inorder(NULL) | prints nothing |