25-Comp-B3 Data Bases and File Systems · December 2015
Nivaar worked solution (AI-drafted; not reviewed by a licensed engineer) · Last updated
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. An order-3 B+-tree (at most 2 keys per node, so at most 3 child pointers), initially empty. The 7 keys Edith, Carol, Betty, Debbie, Alice, Zelda, Wilma are inserted one at a time, in that order, and are compared alphabetically (all 7 first letters are distinct, so alphabetical order on the whole name and on the first letter agree).
Find. (a) equality vs. range search; (b) whether insertion order affects the final tree shape; (c) the tree after EACH of the 7 insertions.
Part (a) — equality search vs. range search. An equality search looks up ONE specific key value (e.g. "find the record for Debbie"): the search descends the tree once, comparing the target key against each node's separators to pick a single child at every level, until it reaches the one leaf that can hold that key — cost is a single root-to-leaf path, O(log N). A range search asks for every record whose key falls between two bounds (e.g. "every name from Carol to Edith inclusive"): it descends once to find the leaf holding the LOWER bound, exactly like an equality search, but then walks SIDEWAYS along the leaf-level sibling-pointer chain — visible as the double-headed arrows between leaves in every diagram below — reading entries in sorted order until a key exceeds the upper bound, with no further root-to-leaf descents needed. This sideways chain is exactly why a B+-tree (unlike a plain B-tree, which also stores data in internal nodes) keeps every data key in the leaves, linked in order: it turns a range query into one descent plus a linear scan, instead of repeated whole-tree lookups.
Part (b) — does insertion order change the final tree? Yes. A B+-tree's shape is a side effect of exactly when each leaf happens to overflow and split, and that timing depends on which keys have already accumulated in a leaf at the moment a new key arrives — which is a property of the ARRIVAL order, not just of the final key set. Part (c) below builds one tree by inserting Edith, Carol, Betty, Debbie, Alice, Zelda, Wilma in exactly that order; inserting the same 7 names in a different order (say, alphabetically: Alice, Betty, Carol, Debbie, Edith, Wilma, Zelda) would trigger splits at different points and can leave keys grouped into different leaves and a different-shaped internal structure, even though both trees necessarily index the same 7 names in the same sorted leaf-chain order — the one invariant insertion order can never change, since every B+-tree keeps its leaves in sorted order regardless of how they filled up.
Part (c) — approach. Standard B+-tree insertion (Ramakrishnan & Gehrke / Silberschatz algorithm, order 3: at most 2 keys/leaf, at most 2 keys & 3 pointers/internal node): descend to the correct leaf and insert the key in sorted position; if the leaf now holds 3 keys it OVERFLOWS and SPLITS into a 2-key left leaf and a 1-key right leaf, and the smallest key of the new right leaf is COPIED up into the parent (data always stays in the leaves); if an internal node is pushed to 3 keys it splits too, but its middle key MOVES up (it is not duplicated, since internal keys are pure routing separators, not data). Traced below and cross-checked against a Python implementation of this exact algorithm.
| Stage | Leaf chain (left→right) |
|---|---|
| After Edith | [Edith] |
| After Carol | [Carol,Edith] |
| After Betty (1st split) | [Betty,Carol] ↔ [Edith] |
| After Debbie | [Betty,Carol] ↔ [Debbie] ↔ [Edith] |
| After Alice (root splits) | [Alice,Betty] ↔ [Carol] ↔ [Debbie] ↔ [Edith] |
| After Zelda | [Alice,Betty] ↔ [Carol] ↔ [Debbie] ↔ [Edith,Zelda] |
| After Wilma (final) | [Alice,Betty] ↔ [Carol] ↔ [Debbie] ↔ [Edith,Wilma] ↔ [Zelda] |