{"record":{"id":"8ebf7275797482fc","repo":"TheAlgorithms/C-Sharp","slug":"minimum-degree-must-be-at-least-2","errorCode":null,"errorMessage":"Minimum degree must be at least 2.","messagePattern":"Minimum degree must be at least 2\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"DataStructures/BTree/BTree.cs","lineNumber":70,"sourceCode":"    /// </summary>\n    private BTreeNode<TKey>? root;\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"BTree{TKey}\"/>\n    ///     class with the specified minimum degree.\n    /// </summary>\n    /// <param name=\"minimumDegree\">\n    ///     Minimum degree (t) of the B-Tree. Must be at least 2.\n    ///     Each node can contain at most 2t-1 keys.\n    /// </param>\n    /// <exception cref=\"ArgumentException\">\n    ///     Thrown when minimumDegree is less than 2.\n    /// </exception>\n    public BTree(int minimumDegree = 2)\n    {\n        if (minimumDegree < 2)\n        {\n            throw new ArgumentException(\"Minimum degree must be at least 2.\", nameof(minimumDegree));\n        }\n\n        MinimumDegree = minimumDegree;\n        comparer = Comparer<TKey>.Default;\n    }\n\n    /// <summary>\n    ///     Initializes a new instance of the <see cref=\"BTree{TKey}\"/>\n    ///     class with the specified minimum degree and custom comparer.\n    /// </summary>\n    /// <param name=\"minimumDegree\">\n    ///     Minimum degree (t) of the B-Tree. Must be at least 2.\n    /// </param>\n    /// <param name=\"customComparer\">\n    ///     Comparer to use when comparing keys.\n    /// </param>\n    /// <exception cref=\"ArgumentException\">\n    ///     Thrown when minimumDegree is less than 2.","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/TheAlgorithms/C-Sharp/blob/96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c/DataStructures/BTree/BTree.cs#L52-L88","documentation":"This ArgumentException is thrown by the BTree(int minimumDegree = 2) constructor when a minimum degree below 2 is passed. A B-tree requires a minimum degree of at least 2 (each node must be able to hold at least 1 key and have at least 2 children); any lower value makes the tree's split/merge invariants mathematically impossible. The parameter name is included in the exception, pointing directly at the invalid argument.","triggerScenarios":"Invoking new BTree<TKey>(minimumDegree) with minimumDegree of 1, 0, or a negative number — e.g. a computed/default-derived value that is 0 or 1, or passing 1 believing it means 'one-key nodes'. The parameterless form (default 2) never throws.","commonSituations":"Reading the degree from configuration where a default of 0 leaks through; an off-by-one assumption that minimum degree counts keys rather than children; arithmetic like degree = branchingFactor - 1 producing 1 when branchingFactor is 2.","solutions":["Pass a minimumDegree value of at least 2; use the parameterless constructor (default 2) if unsure.","Clamp or validate the incoming value before constructing: if (degree < 2) degree = 2; or throw your own descriptive error.","If the degree comes from configuration, add a config validation step enforcing minimumDegree >= 2 with a clear message.","Double-check terminology: minimum degree t means each non-root node has at least t-1 keys and t children."],"exampleFix":"// before\nvar tree = new BTree<int>(1); // ArgumentException\n\n// after\nint degree = Math.Max(2, configuredDegree);\nvar tree = new BTree<int>(degree);","handlingStrategy":"validation","validationCode":"// C#\nif (minimumDegree < 2)\n    throw new ArgumentOutOfRangeException(nameof(minimumDegree),\n        \"B-Tree minimum degree must be at least 2.\");\nvar tree = new BTree<TKey>(minimumDegree);","typeGuard":null,"tryCatchPattern":"// C#\nBTree<TKey> tree;\ntry\n{\n    tree = new BTree<TKey>(minimumDegree);\n}\ncatch (ArgumentException ex)\n{\n    // fall back to default degree\n    tree = new BTree<TKey>();\n}","preventionTips":["Validate config-sourced degrees at load time (must be >= 2).","Prefer the parameterless constructor (default degree 2) unless tuning is deliberate.","Remember minimum degree counts children per node, not keys.","Clamp with Math.Max(2, value) for computed degrees."],"tags":["argumentexception","b-tree","constructor","minimum-degree"],"backgroundTag":"invalid-argument-value","analyzedSha":"96e2905cab7bc6b33ac0a34ee5bb82ddccbcbb6c","analyzedAt":"2026-09-13T17:04:01.438Z","contentChangedAt":"2026-09-13T17:04:01.438Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}