Flat Word-RAM Arena Representation and Split/Insert Operations for Classic B-Trees #
Pure memory-layout geometry, deterministic BTree ℕ ℕ decoding (readTree, readNodeSet), node
validity and key-order predicates (ValidNode, SortedNode), and index-level correctness of
proactive top-down node splitting (memSplitRoot, memSplitChild) and leaf/internal updates
(memOverwriteVal, memInsertLeaf) on a flat Word-RAM word array mem : ℕ → ℕ, following
[CLRS09], Section 18.2.
Arena layout at base and minimum degree t ≥ 2 #
- Header (
[base, base + 2)):mem base: root node indexroot(0-based whenmem (base + 1) > 0).mem (base + 1): allocated node countcnt(0denotes the empty B-tree.leaf).
- Node
u(0 ≤ u < cnt) occupiesnodeStride t = 6 * twords atnodeAddr base t u = base + 2 + u * (6 * t):+ 0:isLeaf(1for leaf,0for internal node).+ 1:numKeys(m, with1 ≤ m ≤ 2 * t - 1).+ 2 + i(0 ≤ i < 2 * t - 1):key[i].+ (2 * t + 1) + i(0 ≤ i < 2 * t - 1):val[i].+ 4 * t + j(0 ≤ j < 2 * t):child[j](0-based child node index whenisLeaf = 0).
Main definitions #
BTree.WordRAM.nodeStride,BTree.WordRAM.nodeAddr,BTree.WordRAM.arenaWords: arena address arithmetic.BTree.WordRAM.leafFlag,BTree.WordRAM.numKeys,BTree.WordRAM.keyAt,BTree.WordRAM.valAt,BTree.WordRAM.childAt: node field accessors onmem : ℕ → ℕ.BTree.WordRAM.readTree,BTree.WordRAM.readNodeSet: deterministic decoding of aBTree ℕ ℕand its allocated node-index set from(levelsBelow, base, t, mem, u).BTree.WordRAM.ValidNode,BTree.WordRAM.SortedNode: structural and strict key-order predicates on an allocated subtree.BTree.WordRAM.slotCount: within-node strict lower-key count computed by a counting loop.BTree.WordRAM.shiftArrRight: backward-shift memory transformer.BTree.WordRAM.memInitSingle,BTree.WordRAM.memOverwriteVal,BTree.WordRAM.memInsertLeaf,BTree.WordRAM.memCopyUpper,BTree.WordRAM.memSplitRoot,BTree.WordRAM.memSplitChild: pure memory transformers mirrored by the Word-RAM machine code.
Main results #
BTree.WordRAM.ValidNode.congr_mem,BTree.WordRAM.SortedNode.congr_mem: disjoint-node memory framing forValidNode,SortedNode,readTree, andreadNodeSet.BTree.WordRAM.SortedNode.isSortedAssoc,BTree.WordRAM.ValidNode.isWF: any valid sorted node at2 ≤ tdecodes to a well-formedBTree.IsWF ttree of heightl + 1.BTree.WordRAM.slotCount_spec,BTree.WordRAM.lookup_readTree_eq_some,BTree.WordRAM.lookup_readTree_leaf_none,BTree.WordRAM.lookup_readTree_succ_eq_child: within-node search bounds and single-level reduction ofBTree.lookup.BTree.WordRAM.memSplitRoot_spec,BTree.WordRAM.memSplitChild_spec: splitting a full (2 * t - 1-key) root or child preservesValidNode,SortedNode, and exacttoListwhile leaving(t - 1)-key halves.BTree.WordRAM.memOverwriteVal_spec,BTree.WordRAM.memInsertLeaf_spec,BTree.WordRAM.childInsert_spec: key overwrite, leaf shift-insertion, and recursive child insertion realizeCslib.SortedAssoc.insert.
References #
1. Address geometry and field accessors #
Words per allocated B-tree node at minimum degree t.
Starting word address of 0-based node u in an arena at base of minimum degree t.
Total word footprint of an arena with capacity capNodes at minimum degree t.
2. Deterministic tree & node-set decoding #
4. Membership in toList and strict key-order predicate (SortedNode) #
Disjoint-node framing: if mem' agrees with mem on every node in
readNodeSet l base t mem u, then ValidNode, readTree, and readNodeSet are preserved.
SortedNode implies List.IsSortedAssoc on (readTree l base t mem u).toList.
When slot < numKeys and keyAt u slot = k, BTree.lookup k (readTree ...) returns
some (valAt u slot).
On a sorted leaf node u (l = 0), if keyAt u i ≠ k for all i < m, lookup returns
none.
On a sorted internal node u (l + 1), when slot ≤ m satisfies keyAt u i < k for
i < slot and k < keyAt u i for slot ≤ i < m, BTree.lookup reduces to child slot.
6. Generic forward-copy and backward-shift array helpers #
7. Pure memory operations: init, overwrite, leaf insert, and split #
8. Upper-half copy, root/child splitting, and child-insertion lifting #
Split full child c = childAt u slot (2 * t - 1 keys) of non-full internal parent u
(m ≤ 2 * t - 2), allocating right half cnt, shifting u's keys/values/children, inserting the
median at slot, and incrementing mem (base + 1) to cnt + 1.
Lift a recursive subtree insertion at c = childAt u slot back up to internal node u when
mem' modifies no node in readNodeSet (l + 1) base t mem u \ readNodeSet l base t mem c.