TheAlgorithms/Go · error
node has too many keys
Error message
node has too many keys
What it means
Verify panics when a node holds more keys than the tree's maxKeys, exceeding the node's allocated capacity. Normally splits in Insert keep nodes within capacity, so this signals an out-of-band mutation or a node shared between trees with different maxKeys.
Source
Thrown at structure/tree/btree.go:50
}
}
func NewBTree[T constraints.Ordered](maxKeys int) *BTree[T] {
if maxKeys <= 2 {
panic("Must be >= 3 keys")
}
return &BTree[T]{
root: nil,
maxKeys: maxKeys,
}
}
func (node *BTreeNode[T]) Verify(tree *BTree[T]) {
minKeys := minKeys(tree.maxKeys)
if node != tree.root && node.numKeys < minKeys {
panic("node has too few keys")
} else if node.numKeys > tree.maxKeys {
panic("node has too many keys")
}
}
func (node *BTreeNode[T]) IsFull(maxKeys int) bool {
return node.numKeys == maxKeys
}
func (node *BTreeNode[T]) Search(key T) bool {
i := 0
for ; i < node.numKeys; i++ {
if key == node.keys[i] {
return true
}
if key < node.keys[i] {
break
}
}
if node.isLeaf {View on GitHub (pinned to 5ba447ec5f)
Solutions
- Only insert keys through tree.Insert, which triggers Split when nodes fill.
- Keep node ownership 1:1 with a single BTree and its maxKeys.
- Rebuild the tree from scratch (re-insert all keys) if nodes were transferred between trees.
- Run root.Verify after manual manipulations to catch corruption early.
Example fix
// before leaf.Append(k, nil) // can exceed maxKeys // after tree.Insert(k) // splits full nodes automatically
Defensive patterns
Strategy: validation
Validate before calling
if tree.root != nil {
tree.root.Verify(tree) // panics if any node overflows
}
tree.Insert(key) Try / catch
func safeInsert[T constraints.Ordered](t *BTree[T], k T) {
defer func() { _ = recover() }()
t.Insert(k)
} Prevention
- Insert only via tree.Insert so splits run
- Do not move nodes between trees
- Use Append/Concat only inside library-internal code
When it happens
Trigger: Inserting into nodes without going through the tree's split logic (calling node-level helpers directly); moving a node from a tree with larger maxKeys into a tree with smaller maxKeys and calling Verify; writing past numKeys accounting with direct key appends.
Common situations: Copying nodes between BTree instances in tests or serialization code; custom batch-insert code that bypasses InsertNonFull's split path.
Related errors
- node has too few keys
- nodes should not have less than the minimum number of keys
- cannot delete key from node with minimum number of keys
- BTree maxKeys cannot be zero
- Must be >= 3 keys
AI-assisted analysis of TheAlgorithms/Go@5ba447ec5f (2026-09-02).
Data as JSON: /api/errors/4d3ce4398a39405b.
Report an issue: GitHub.