{"record":{"id":"5fbb8bcbf74c984b","repo":"TheAlgorithms/Go","slug":"btree-maxkeys-cannot-be-zero","errorCode":null,"errorMessage":"BTree maxKeys cannot be zero","messagePattern":"BTree maxKeys cannot be zero","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"structure/tree/btree.go","lineNumber":26,"sourceCode":"type BTreeNode[T constraints.Ordered] struct {\n\tkeys     []T\n\tchildren []*BTreeNode[T]\n\tnumKeys  int\n\tisLeaf   bool\n}\n\ntype BTree[T constraints.Ordered] struct {\n\troot    *BTreeNode[T]\n\tmaxKeys int\n}\n\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","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/tree/btree.go#L8-L44","documentation":"NewBTreeNode panics when maxKeys is <= 0 because a B-tree node needs at least one key slot; the arrays keys and children would be meaningless otherwise. The library treats invalid construction parameters as a programming error, so it panics immediately rather than returning an error.","triggerScenarios":"Calling NewBTreeNode with maxKeys = 0 or a negative value, e.g. NewBTreeNode[int](0, true), or NewBTree (which calls it via Insert/Split paths) indirectly with a bad size derived from config or user input.","commonSituations":"Reading maxKeys from a config file or environment variable that is unset (0) or negative; off-by-one or integer division producing 0; copy-pasting a minimal example and passing 0 as a placeholder.","solutions":["Pass a positive maxKeys >= 3 (a B-tree degree parameter) when constructing nodes.","Validate any external/config-sourced value before calling NewBTreeNode.","Use NewBTree(maxKeys) which enforces maxKeys >= 3 for the top-level entry point."],"exampleFix":"// before\nnode := NewBTreeNode[int](cfg.MaxKeys, true)\n// after\nif cfg.MaxKeys < 3 { cfg.MaxKeys = 3 }\nnode := NewBTreeNode[int](cfg.MaxKeys, true)","handlingStrategy":"validation","validationCode":"func validMaxKeys(n int) bool { return n >= 3 }\nif !validMaxKeys(cfg.MaxKeys) { cfg.MaxKeys = 4 }\nnode := NewBTreeNode[int](cfg.MaxKeys, true)","typeGuard":null,"tryCatchPattern":"func safeNewNode[T constraints.Ordered](maxKeys int, leaf bool) (n *BTreeNode[T]) {\n    defer func() {\n        if r := recover(); r != nil { n = nil }\n    }()\n    return NewBTreeNode[T](maxKeys, leaf)\n}","preventionTips":["Never pass unvalidated config/env values as maxKeys","Use NewBTree as the sole construction entry point","Document that maxKeys is max keys per node, 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"}