{"record":{"id":"9083257767f06227","repo":"TheAlgorithms/Go","slug":"must-be-3-keys","errorCode":null,"errorMessage":"Must be >= 3 keys","messagePattern":"Must be >= 3 keys","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"structure/tree/btree.go","lineNumber":37,"sourceCode":"\nfunc minKeys(maxKeys int) int {\n\treturn (maxKeys - 1) / 2\n}\n\nfunc NewBTreeNode[T constraints.Ordered](maxKeys int, isLeaf bool) *BTreeNode[T] {\n\tif maxKeys <= 0 {\n\t\tpanic(\"BTree maxKeys cannot be zero\")\n\t}\n\treturn &BTreeNode[T]{\n\t\tkeys:     make([]T, maxKeys),\n\t\tchildren: make([]*BTreeNode[T], maxKeys+1),\n\t\tisLeaf:   isLeaf,\n\t}\n}\n\nfunc NewBTree[T constraints.Ordered](maxKeys int) *BTree[T] {\n\tif maxKeys <= 2 {\n\t\tpanic(\"Must be >= 3 keys\")\n\t}\n\treturn &BTree[T]{\n\t\troot:    nil,\n\t\tmaxKeys: maxKeys,\n\t}\n}\n\nfunc (node *BTreeNode[T]) Verify(tree *BTree[T]) {\n\tminKeys := minKeys(tree.maxKeys)\n\tif node != tree.root && node.numKeys < minKeys {\n\t\tpanic(\"node has too few keys\")\n\t} else if node.numKeys > tree.maxKeys {\n\t\tpanic(\"node has too many keys\")\n\t}\n}\n\nfunc (node *BTreeNode[T]) IsFull(maxKeys int) bool {\n\treturn node.numKeys == maxKeys","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/tree/btree.go#L19-L55","documentation":"NewBTree panics when maxKeys <= 2 because a B-tree with fewer than 3 max keys cannot satisfy the minimum-key invariants of internal nodes (minKeys = (maxKeys-1)/2 leaves no room to split or borrow). The library rejects structurally impossible tree parameters at construction time.","triggerScenarios":"Calling NewBTree with 0, 1, or 2, e.g. NewBTree[int](2), often from a misconfigured degree setting or a default zero value of an int variable.","commonSituations":"Zero-valued struct field used as maxKeys; misunderstanding whether the parameter is 'order' vs 'max keys'; hardcoded small values in tests.","solutions":["Call NewBTree with maxKeys >= 3 (4 is a common even choice).","Clamp or validate configuration values before constructing the tree.","If you need a smaller branching factor, B-trees are not the right structure; use a BST/2-3 tree implementation."],"exampleFix":"// before\ntree := NewBTree[int](2)\n// after\ntree := NewBTree[int](4)","handlingStrategy":"validation","validationCode":"func NewBTreeSafe[T constraints.Ordered](maxKeys int) *BTree[T] {\n    if maxKeys < 3 { maxKeys = 4 }\n    return NewBTree[T](maxKeys)\n}","typeGuard":null,"tryCatchPattern":"defer func() {\n    if r := recover(); r != nil {\n        log.Fatalf(\"invalid BTree maxKeys: %v\", r)\n    }\n}()","preventionTips":["Always pass a literal >= 3 or clamp config input","Default to 4 if the setting is missing","Remember the parameter is maxKeys, not tree order"],"tags":["panic","btree","invalid-argument"],"backgroundTag":"invalid-constructor-argument","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}