Master the fundamental concepts of b-trees through this focused micro-challenge.
B-tree nodes pack sorted keys and child pointers in a fixed fanout so each node fits one disk page (commonly 4KB/8KB). PostgreSQL B-Tree pages, SQLite table b-trees, and InnoDB indexes share this on-disk shape even when in-memory prototypes use smaller degrees for teaching.
Leaf flag, key count, parallel arrays of keys and values or child pointers, plus parent link if mutable.
cLoading…
t means keys per node between t-1 and 2t-1 (except root)Keep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will define a B-tree node struct and print its layout fields. This exercise requires showing how many keys fit given MAX_KEYS and page size constants.
Define a B-tree node with room for MAX_KEYS = 3 key/value pairs and MAX_KEYS + 1 child pointers, plus num_keys and is_leaf. Build this two-level tree:
Print the node or nodes selected on stdin. A node prints as LABEL: leaf|internal keys=N [k:v k:v] capacity=3.
One word: leaf, keys, root or demo. Anything else behaves like demo.
Print exactly these lines for each mode:
leaf: the left leaf, labelled Node
cLoading…
keys: the left leaf, then its first key
cLoading…
root: the root, labelled Root
cLoading…
demo: the root, the left leaf, the right leaf, then the capacity
cLoading…
Input:
cLoading…
Output:
cLoading…
print_node walks keys[0..num_keys).calloc and free them all before exiting.Hidden tests cover the first key, the root node and the whole tree.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it works