{"record":{"id":"2632e4b4794d8c46","repo":"TheAlgorithms/Java","slug":"point-has-wrong-dimension","errorCode":null,"errorMessage":"Point has wrong dimension","messagePattern":"Point has wrong dimension","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/trees/KDTree.java","lineNumber":247,"sourceCode":"            return new Node(points[0], axis);\n        }\n        Arrays.sort(points, Comparator.comparingInt(o -> o.getCoordinate(axis)));\n        int median = points.length >> 1;\n        Node node = new Node(points[median], axis);\n        node.left = build(Arrays.copyOfRange(points, 0, median), depth + 1);\n        node.right = build(Arrays.copyOfRange(points, median + 1, points.length), depth + 1);\n        return node;\n    }\n\n    /**\n     * Insert a point into the KDTree\n     *\n     * @param point The point to insert\n     *\n     */\n    public void insert(Point point) {\n        if (point.getDimension() != k) {\n            throw new IllegalArgumentException(\"Point has wrong dimension\");\n        }\n        root = insert(root, point, 0);\n    }\n\n    /**\n     * Insert a point into a subtree\n     *\n     * @param root The root of the subtree\n     * @param point The point to insert\n     * @param depth The current depth of the tree\n     *\n     * @return The root of the KDTree\n     */\n    private Node insert(Node root, Point point, int depth) {\n        int axis = depth % k;\n        if (root == null) {\n            return new Node(point, axis);\n        }","sourceCodeStart":229,"sourceCodeEnd":265,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/trees/KDTree.java#L229-L265","documentation":"Thrown by KDTree.insert(Point) when the supplied point's dimension differs from the tree's k. The tree splits on axis depth%k, so inserting a point of a different dimension would corrupt the structure or index out of bounds. The IllegalArgumentException enforces that inserted points match the dimensionality established at construction.","triggerScenarios":"Inserting a 2D point into a 3D tree or vice versa. Inserting points built from a different feature schema than the construction set. Reusing a point object whose coordinates were partially populated.","commonSituations":"Schema drift between tree construction and later inserts. Feature pipelines that change dimensionality over time. Test code that constructs points with the wrong coordinate count.","solutions":["Ensure inserted points share the tree's k; construct them from the same schema.","Validate point.getDimension() against the tree's dimension before calling insert.","Expose the tree's k via a getter and assert equality at the boundary.","Reject or transform out-of-schema points upstream."],"exampleFix":"// before\nkdTree.insert(newPoint);\n// after\nif (newPoint.getDimension() != kdTree.getK()) {\n    throw new IllegalArgumentException(\"point dimension mismatch\");\n}\nkdTree.insert(newPoint);","handlingStrategy":"validation","validationCode":"if (newPoint.getDimension() == kdTree.getK()) {\n    kdTree.insert(newPoint);\n} else {\n    throw new IllegalArgumentException(\"point dimension does not match tree\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Construct inserted points from the same schema as the build set.","Validate point.getDimension() against the tree's k before insert.","Guard against schema drift in feature pipelines."],"tags":["tree","invalid-argument","java","datastructures","kdtree","dimension"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}