NivaarExam PrepOfficial exam papers ↗

19-Soft-A1 Algorithms & Data Structures · May 2013

Question 2 of 8: Tree Implementation

Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer)

Notes on this paper

National Exams — May 2013 — 04-Soft-A1 Algorithms & Data Structures. Three-hour, closed-book exam (Casio or Sharp approved calculator only). Format: eight questions in two parts — candidates choose 4 of the first 5 (10 marks each) and must answer Q6, Q7 and Q8 (20 marks each), with Q7 itself asking for 5 of 6 sub-concepts. All eight questions, and all sub-parts within them, are solved below for completeness.

Reference texts: Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms (3rd ed., MIT Press) — asymptotic analysis, heaps, graph algorithms, divide-and-conquer, NP-completeness; Sedgewick & Wayne, Algorithms (4th ed., Addison-Wesley) — linked-list and array data structures, sorting; Weiss, Data Structures and Algorithm Analysis in C (2nd ed., Pearson) — array-based binary trees and heaps.

Question 2: Tree Implementation (10 marks: 4, 4, 2)

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 stated signatures parent(int* data, int* out) and right_child(int* data, int* out) omit the index whose relative is being asked for and the array's current size — both are required to answer the question at all, so they are added explicitly below as n (the element's index) and size (number of nodes currently stored). The tree is assumed 0-indexed (root at data[0]), the standard C-array convention and the one under which the classic heap formulas parent(n) = (n-1)/2, left(n) = 2n+1, right(n) = 2n+2 hold; this is confirmed against the 1-indexed alternative in the Concept box below.

Approach. The array stores the tree level-by-level, left-to-right (a "complete tree" layout), so a node's relatives are pure arithmetic on its index — no pointers are stored or followed.

1. Parent index — 4 marks

For 0-indexed storage, node n's parent is at index $\left\lfloor \dfrac{n-1}{2} \right\rfloor$, and the root (n = 0) has no parent.

int parent(int* data, int n, int* out) {
    if (n <= 0) return 0;              /* root has no parent */
    *out = data[(n - 1) / 2];          /* integer division truncates */
    return 1;
}

2. Right-child index — 4 marks

Node n's right child is at index $2n + 2$; it exists only if that index is still within the stored range [0, size).

int right_child(int* data, int n, int size, int* out) {
    int idx = 2 * n + 2;
    if (idx >= size) return 0;         /* no right child stored */
    *out = data[idx];
    return 1;
}

3. Internal (non-leaf) node count — 2 marks

A node is internal exactly when its left child index, $2i+1$, still lies inside the array — i.e. indices $0, 1, \dots, \lfloor n/2 \rfloor - 1$ are internal and the rest are leaves: $$\text{internal nodes} = \left\lfloor \frac{n}{2} \right\rfloor, \qquad \text{leaves} = n - \left\lfloor \frac{n}{2} \right\rfloor = \left\lceil \frac{n}{2} \right\rceil.$$

Quantity (0-indexed array of n nodes)Result
Parent of index k$\lfloor (k-1)/2 \rfloor$, none if $k=0$
Right child of index k$2k+2$ if $< n$, else none
Internal (non-leaf) nodes, tree of size n$\boxed{\lfloor n/2 \rfloor}$