Exercise 1: A Tree With Fewer Keys Than One Leaf's Capacity — Possible Solution ==================================================================== THE TEST ------------------------------ tiny_pairs = [(1, 'a'), (2, 'b'), (3, 'c')] tiny_tree = bulk_load_two_level(tiny_pairs, leaf_capacity=8) tiny_tree.leaf [btree_search(tiny_tree, k) for k, v in tiny_pairs] RESULT ------------------------------ tiny_tree.leaf -> True btree_search(tiny_tree, 1) -> 'a' btree_search(tiny_tree, 2) -> 'b' btree_search(tiny_tree, 3) -> 'c' The returned tree is a single leaf node -- no root, no internal node, no children list at all -- and every key is found correctly. WHY NO INTERNAL NODE GETS BUILT ------------------------------ bulk_load_two_level's own first step is: chunks = [sorted_pairs[i:i + leaf_capacity] for i in range(0, len(sorted_pairs), leaf_capacity)] if len(chunks) == 1: leaf = BTreeNode(leaf=True) leaf.keys = [k for k, v in chunks[0]] leaf.values = [v for k, v in chunks[0]] return leaf With only 3 pairs and leaf_capacity=8, the single slice sorted_pairs[0:8] captures all 3 pairs in one chunk -- the list comprehension produces exactly ONE chunk, so `len(chunks) == 1` is True. The function returns immediately with a single, plain leaf node, never reaching the code further down that builds a root and splits data across multiple children. WHY btree_search STILL WORKS CORRECTLY, WITH NO SPECIAL CASE ------------------------------ btree_search's own logic already has a leaf-node code path baked in from the start -- it was never written assuming a root/internal node always exists: if i < len(node.keys) and key == node.keys[i]: return node.values[i] if node.leaf: return None Called directly on a leaf node, the function finds the matching key by linear position within that leaf's own keys list (via the same while loop every node uses), and returns its value. There's no code anywhere that assumes the argument passed to btree_search must be an internal node, or that a tree must have at least one level of internal structure before search can begin. WHY THIS WORKS AS AN ANSWER ------------------------------ This confirms btree_search treats "the whole tree is just one leaf" as a completely ordinary, unremarkable case rather than something requiring special handling -- a real, practical property, since a genuinely small table (or a young, freshly-created index before much data has been inserted) is exactly this shape in a real database too.