{"record":{"id":"e66d515ff512044c","repo":"TheAlgorithms/Java","slug":"invalid-node","errorCode":null,"errorMessage":"Invalid node: {}","messagePattern":"Invalid node: (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java","lineNumber":131,"sourceCode":"         */\n        private int findCentroid(int u, int p, int totalSize) {\n            for (int v : adj.get(u)) {\n                if (v != p && !removed[v] && subtreeSize[v] > totalSize / 2) {\n                    return findCentroid(v, u, totalSize);\n                }\n            }\n            return u;\n        }\n\n        /**\n         * Gets the parent of a node in the centroid tree.\n         *\n         * @param node the node\n         * @return parent node in centroid tree, or -1 if root\n         */\n        public int getParent(int node) {\n            if (node < 0 || node >= n) {\n                throw new IllegalArgumentException(\"Invalid node: \" + node);\n            }\n            return parent[node];\n        }\n\n        /**\n         * Gets the root of the centroid tree.\n         *\n         * @return root node\n         */\n        public int getRoot() {\n            return root;\n        }\n\n        /**\n         * Gets the number of nodes in the tree.\n         *\n         * @return number of nodes\n         */","sourceCodeStart":113,"sourceCodeEnd":149,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java#L113-L149","documentation":"Thrown by CentroidTree.getParent(int node) when the node index is outside the valid range [0, n). getParent indexes the internal parent[] array, so an out-of-range index would cause ArrayIndexOutOfBoundsException. The IllegalArgumentException rejects invalid node identifiers with their offending value.","triggerScenarios":"Passing a node id derived from 1-indexed external data into a 0-indexed tree. Passing n (the count) as a node id. Passing a negative value from a sentinel or uninitialized variable.","commonSituations":"Mixing 0-based and 1-based indexing between input parsing and the algorithm. Off-by-one when iterating node ids (e.g., i <= n instead of i < n). Using a node id returned as -1 (no-parent sentinel) without filtering.","solutions":["Ensure node ids are in [0, n-1]; convert 1-based external ids by subtracting 1.","Filter out sentinel values (e.g., -1 from getParent itself) before passing them back in.","Validate node range at the boundary where external data enters your code.","Iterate with i < n rather than i <= n."],"exampleFix":"// before\nint p = ct.getParent(nodeId);\n// after\nif (nodeId < 0 || nodeId >= n) {\n    throw new IllegalArgumentException(\"bad node id: \" + nodeId);\n}\nint p = ct.getParent(nodeId);","handlingStrategy":"validation","validationCode":"if (node >= 0 && node < n) {\n    int parent = ct.getParent(node);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert 1-based external ids to 0-based before use.","Filter sentinel values like -1 before passing them back as node ids.","Iterate with i < n, not i <= n."],"tags":["tree","invalid-argument","java","datastructures","indexing","centroid-decomposition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}