NivaarExam PrepOfficial exam papers ↗

19-Soft-A1 Algorithms & Data Structures · December 2016

Question 2 of 7: Max Heap

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

Notes on this paper

National Exams — December 2016 — 04-Soft-A1 Algorithms & Data Structures. Three-hour, closed-book exam (Casio or Sharp approved calculator only). Format: seven questions; candidates pick five of their choice, and the first five as they appear in the answer book are marked, each worth 20 marks. All seven questions, and all sub-parts within them, are solved below for completeness. Implementations below use C-style pseudocode, as the exam note permits any of C, C++, Java, Python, or clean pseudocode.

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, hashing; Weiss, Data Structures and Algorithm Analysis in C (2nd ed., Pearson) — array-based binary trees and heaps, ADT design.

Question 2: Max Heap (20 marks: 5 items, 4 marks each)

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.

Given. A 0-indexed array storing a complete binary tree, level by level. Find. the parent/right-child index formulas, the insert and delete-max procedures, the heap states after inserting 4, 6, 3, 7, 1 into an empty heap, and the time complexity of each operation.

1. Index formulas — 4 marks

a. Parent of index $n$: $\left\lfloor \dfrac{n-1}{2} \right\rfloor$ (integer division truncates); node 0 (the root) has no parent. b. Right child of index $n$: $2n+2$, valid only while that index is still within the array's current size.

2. Insertion — 4 marks

The new value is appended in the next open slot at the end of the array, which keeps the tree complete. It is then compared against its parent and swapped upward ("sift-up") for as long as it is larger than its parent, stopping as soon as the parent is at least as large or the new node reaches the root.

3. Deletion (delete-max) — 4 marks

The root (the maximum) is removed and returned; the last element in the array is moved into the now-empty root slot to keep the tree complete, and the array shrinks by one. That relocated element is then compared against its larger child and swapped downward ("sift-down") until both children are smaller or it has no children.

4. Building the heap: insert 4, 6, 3, 7, 1 — 4 marks

Approach. Insert one value at a time into the array's next free slot, then sift it up while it exceeds its parent.

  1. Insert 4. Empty heap → $[4]$, no parent to compare against.
  2. Insert 6. Appended at index 1, parent is index 0 (value 4); $6>4$ so it swaps up: $[6,4]$.
  3. Insert 3. Appended at index 2, parent is index 0 (value 6); $3<6$, no swap: $[6,4,3]$.
  4. Insert 7. Appended at index 3, parent is index 1 (value 4); $7>4$, swap to $[6,7,3,4]$; new position is index 1, parent index 0 (value 6); $7>6$, swap again to $[7,6,3,4]$.
  5. Insert 1. Appended at index 4, parent is index 1 (value 6); $1<6$, no swap. $$\boxed{[7,6,3,4,1]}$$
insert 444insert 66464insert 3643643insert 776347634insert 17634176341
Figure 2 — Array-backed max heap after each insertion, with the array strip shown beneath each tree. Final heap: $[7,6,3,4,1]$.

5. Complexity — 4 marks

Both insertion and delete-max run in $O(\log n)$ time. In each case the element being repositioned travels along a single root-to-leaf path, and because the tree is complete its height is $\lfloor \log_2 n \rfloor$, so the sift-up or sift-down loop performs at most $O(\log n)$ comparisons and swaps.

ItemResult
Parent of index $n$$\lfloor(n-1)/2\rfloor$
Right child of index $n$$2n+2$
Heap array after inserting 4,6,3,7,1$\boxed{[7,6,3,4,1]}$
Insert / delete-max complexity$\boxed{O(\log n)}$ each