TheAlgorithms/Java · error · IllegalArgumentException
Point has wrong dimension
Error message
Point has wrong dimension
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/trees/KDTree.java:247
return new Node(points[0], axis);
}
Arrays.sort(points, Comparator.comparingInt(o -> o.getCoordinate(axis)));
int median = points.length >> 1;
Node node = new Node(points[median], axis);
node.left = build(Arrays.copyOfRange(points, 0, median), depth + 1);
node.right = build(Arrays.copyOfRange(points, median + 1, points.length), depth + 1);
return node;
}
/**
* Insert a point into the KDTree
*
* @param point The point to insert
*
*/
public void insert(Point point) {
if (point.getDimension() != k) {
throw new IllegalArgumentException("Point has wrong dimension");
}
root = insert(root, point, 0);
}
/**
* Insert a point into a subtree
*
* @param root The root of the subtree
* @param point The point to insert
* @param depth The current depth of the tree
*
* @return The root of the KDTree
*/
private Node insert(Node root, Point point, int depth) {
int axis = depth % k;
if (root == null) {
return new Node(point, axis);
}View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
kdTree.insert(newPoint);
// after
if (newPoint.getDimension() != kdTree.getK()) {
throw new IllegalArgumentException("point dimension mismatch");
}
kdTree.insert(newPoint); Defensive patterns
Strategy: validation
Validate before calling
if (newPoint.getDimension() == kdTree.getK()) {
kdTree.insert(newPoint);
} else {
throw new IllegalArgumentException("point dimension does not match tree");
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Points must have the same dimension
- Points array cannot be empty
- Invalid node: {}
- Number of nodes must be positive
- Tree must have exactly n-1 edges
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/2632e4b4794d8c46.
Report an issue: GitHub.