TheAlgorithms/Java · error · IllegalArgumentException
Tree cannot be empty or null
Error message
Tree cannot be empty or null
What it means
Thrown by the CentroidTree constructor when the supplied adjacency list is null or empty. Centroid decomposition requires at least one node and recurses on adj, so a null/empty list would NPE or produce a meaningless tree. The IllegalArgumentException is a fail-fast guard at construction.
Source
Thrown at src/main/java/com/thealgorithms/datastructures/trees/CentroidDecomposition.java:50
* Represents the centroid tree structure.
*/
public static final class CentroidTree {
private final int n;
private final List<List<Integer>> adj;
private final int[] parent;
private final int[] subtreeSize;
private final boolean[] removed;
private int root;
/**
* Constructs a centroid tree from an adjacency list.
*
* @param adj adjacency list representation of the tree (0-indexed)
* @throws IllegalArgumentException if tree is empty or null
*/
public CentroidTree(List<List<Integer>> adj) {
if (adj == null || adj.isEmpty()) {
throw new IllegalArgumentException("Tree cannot be empty or null");
}
this.n = adj.size();
this.adj = adj;
this.parent = new int[n];
this.subtreeSize = new int[n];
this.removed = new boolean[n];
Arrays.fill(parent, -1);
// Build centroid tree starting from node 0
this.root = decompose(0, -1);
}
/**
* Recursively builds the centroid tree.
*
* @param u current node
* @param p parent in centroid treeView on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure the adjacency list has at least one (possibly empty) entry per node before constructing.
- Skip CentroidTree construction entirely when the graph is empty, handling that case separately.
- Validate the upstream graph builder to never return null; return an empty structure or throw earlier.
- Use buildFromEdges with n>=1 and valid edges instead of constructing directly from an adjacency list.
Example fix
// before
CentroidTree ct = new CentroidTree(adj);
// after
if (adj == null || adj.isEmpty()) {
throw new IllegalStateException("Graph has no nodes");
}
CentroidTree ct = new CentroidTree(adj); Defensive patterns
Strategy: validation
Validate before calling
if (adj != null && !adj.isEmpty()) {
CentroidTree ct = new CentroidTree(adj);
} else {
// handle empty graph separately
} Type guard
java.util.Objects.requireNonNull(adj, "adjacency list");
Prevention
- Skip CentroidTree construction for empty graphs.
- Ensure adjacency lists have one entry per node.
- Prefer buildFromEdges with validated n and edges.
When it happens
Trigger: Passing null for the adjacency list. Passing an empty List<List<Integer>>. Passing a list that was built conditionally and left empty when no nodes exist.
Common situations: Graph builders that return empty when the input graph has no vertices. Config/serialization that yields null for a missing graph section. Test scaffolding that constructs the tree before building edges.
Related errors
- Edges cannot be null
- Number of nodes must be positive
- Stack cannot be null
- Invalid node: {}
- Tree must have exactly n-1 edges
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/4352fdde4e1da215.
Report an issue: GitHub.