{"record":{"id":"96e1c50250bceef2","repo":"TheAlgorithms/Go","slug":"cannot-merge-when-leaf-node-is-parent","errorCode":null,"errorMessage":"cannot merge when leaf node is parent","messagePattern":"cannot merge when leaf node is parent","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/tree/btree.go","lineNumber":221,"sourceCode":"\t}\n\tnode.numKeys--\n}\n\n// Transform:\n//\n//\tA B C\n//\t / \\\n//\ta   b\n//\n// Into:\n//\n//\tA C\n//\t |\n//\n// a B c\nfunc (node *BTreeNode[T]) Merge(idx int) {\n\tif node.isLeaf {\n\t\tpanic(\"cannot merge when leaf node is parent\")\n\t}\n\tleft := node.children[idx]\n\tright := node.children[idx+1]\n\tleft.Append(node.keys[idx], right.children[0])\n\tleft.Concat(right, 0)\n\tnode.DeleteIthKey(idx)\n}\n\nfunc (node *BTreeNode[T]) Min() T {\n\tif node.isLeaf {\n\t\treturn node.keys[0]\n\t}\n\treturn node.children[0].Min()\n}\n\nfunc (node *BTreeNode[T]) Max() T {\n\tif node.isLeaf {\n\t\treturn node.keys[node.numKeys-1]","sourceCodeStart":203,"sourceCodeEnd":239,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/tree/btree.go#L203-L239","documentation":"Merge combines children[idx] and children[idx+1] under the parent node by moving the parent's separator key down; a leaf has no children to merge, so the operation is meaningless and panics. Merge is only valid on an internal (non-leaf) node.","triggerScenarios":"Calling node.Merge(idx) directly on a leaf node; a custom Delete implementation that reaches a leaf before attempting rebalancing; calling Merge with a node from a tree where all nodes happen to be leaves (single-level tree).","commonSituations":"Implementing a custom delete/rebalance routine on top of the node API; tests invoking Merge on the root of a tree with no children.","solutions":["Only call Merge on internal nodes — check !node.isLeaf first.","Use tree.Delete which only merges during internal-node rebalancing.","For underflowing leaves, borrow from a sibling or let the parent-level Delete logic handle it."],"exampleFix":"// before\nnode.Merge(i) // node may be a leaf\n// after\nif !node.isLeaf {\n    node.Merge(i)\n}","handlingStrategy":"validation","validationCode":"if node.isLeaf {\n    return errors.New(\"cannot merge a leaf node\")\n}\nnode.Merge(idx)","typeGuard":null,"tryCatchPattern":"func safeMerge(n *BTreeNode[int], idx int) (err error) {\n    defer func() {\n        if r := recover(); r != nil { err = fmt.Errorf(\"merge failed: %v\", r) }\n    }()\n    n.Merge(idx)\n    return nil\n}","preventionTips":["Check isLeaf before any child-reordering operation","Let tree.Delete handle rebalancing","Restrict Merge/borrow calls to internal nodes"],"tags":["panic","btree","invalid-operation"],"backgroundTag":"invalid-operation-for-node-type","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}