How to traverse a tree from sklearn AgglomerativeClustering?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
AgglomerativeClustering builds a hierarchical merge tree, but scikit-learn does not hand you a ready-made recursive node structure. Instead, the fitted model exposes arrays such as children_ and optionally distances_, and you reconstruct the tree logic from those arrays.
Understand What children_ Contains
After fitting, children_ stores one row per merge. Each row contains the two cluster indices that were merged at that step.
For n_samples original points, leaf nodes are indexed from 0 to n_samples - 1. Internal merged nodes start at n_samples and continue upward in merge order.
Reconstruct a Tree Recursively
A common way to traverse the hierarchy is to write a recursive function that turns a node index into either a leaf or an internal node.
The key offset is node_id - n_samples. That converts an internal node index into the correct row of children_.
Traverse the Tree for Leaves or Merge Order
Once you have a recursive structure, ordinary tree traversal patterns work naturally.
This is useful when you want the original sample indices contained in a cluster or subtree.
You can also walk the internal nodes to inspect merge distances and cluster sizes.
Build a Linkage Matrix for Dendrogram Tools
If the goal is plotting with SciPy's dendrogram utilities, many users build a linkage matrix from children_, distances_, and cluster sizes.
That format is handy when you want standard dendrogram plotting rather than custom recursive traversal logic.
Common Pitfalls
- Forgetting that internal node indices start at
n_samples, not at zero. - Indexing
children_directly with a node id instead of subtractingn_samplesfor internal nodes. - Expecting
distances_to exist without fitting in a way that computes distances. - Confusing original sample indices with internal cluster-node indices.
- Reconstructing the tree manually without also tracking subtree sizes when dendrogram-style output is needed.
Summary
- '
AgglomerativeClusteringexposes the merge tree through arrays such aschildren_.' - Leaves are original sample indices, and internal nodes begin at
n_samples. - Reconstruct the hierarchy by recursively resolving internal node indices back into rows of
children_. - Use recursive traversal to inspect leaves, merge order, distances, or subtree sizes.
- Build a linkage matrix if your real goal is dendrogram tooling rather than a custom tree object.
Related reading
- How to tune GaussianNB?
- How to tune parameters in Random Forest, using Scikit Learn?
- How to turn off dropout for testing in Tensorflow?
- How to turn off dropout for testing in Tensorflow?
- How to unbatch a Tensorflow 2.0 Dataset
- How to understand RandomForestExplainer output R package
- How to traverse cyclic directed graphs with modified DFS algorithm
- How to turn integers into Fibonacci coding efficiently?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.