Can I use arbitrary metrics to search KD-Trees?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
KD-Trees (K-Dimensional Trees) are a fundamental data structure for organizing points in a k-dimensional space. They are widely used for efficient nearest-neighbor searches, range queries, and spatial lookups. Typically, KD-Trees rely on the Euclidean distance metric. But what happens when you need a different distance function? This article examines when and how arbitrary metrics can be used with KD-Trees, what breaks when the metric does not match the tree's structure, and what alternatives exist.
How KD-Trees Work
A KD-Tree is a binary tree that recursively partitions space by splitting along one coordinate axis at a time. At depth , the tree splits on dimension . Each internal node stores a splitting value, and the two subtrees contain all points on either side of that split.
The key property that makes KD-Tree search efficient is pruning: during a nearest-neighbor search, entire subtrees can be skipped if the closest possible point in that subtree is farther than the current best candidate. This pruning relies on a tight relationship between the distance metric and the axis-aligned splits.
What Is a Metric?
A metric on a set is a function that satisfies four properties:
- Non-negativity: for all
- Identity of indiscernibles:
- Symmetry: for all
- Triangle inequality: for all
Common metrics include Euclidean distance (), Manhattan distance (), Chebyshev distance (), and the Minkowski family .
Can You Use Arbitrary Metrics?
The short answer: you can use any metric (Minkowski family) with KD-Trees and still get correct results with effective pruning. For metrics outside this family, correctness is preserved but pruning effectiveness degrades, sometimes severely.
Why Metrics Work
The distance between two points and in dimensions is:
For any metric, the distance from a query point to the nearest point in an axis-aligned region can be computed exactly using the per-dimension distances. This means the pruning bounds remain tight, and the search stays efficient.
Manhattan Distance (L1)
Manhattan distance sums the absolute differences along each axis:
This metric works well with KD-Trees. The bounding regions are axis-aligned boxes, and the minimum distance from a point to a box can be computed by summing per-axis gaps. Pruning remains effective.
Custom Non-Lp Metrics
Consider a custom metric like:
where . This metric is valid (it satisfies all four metric properties for appropriate ), but it does not decompose neatly along axes in the same way metrics do. The pruning bounds become loose because the tree cannot tightly estimate the minimum distance to a subtree's bounding box.
The result: the search still returns correct answers (you can always fall back to exhaustive checking of non-pruned subtrees), but you lose the average-case performance. In the worst case, nearly every node is visited, making it no better than a brute-force scan.
Metrics That Break Pruning Entirely
Some valid metrics make KD-Tree pruning almost useless:
- Weighted metrics with dimension-dependent weights that do not align with the split axes
- Learned metrics from machine learning embeddings, such as Mahalanobis distance where is a positive-definite matrix
- Edit distance or other non-geometric metrics applied to vectorized data
For these, consider alternative data structures.
Alternatives for Non-Euclidean Metrics
| Data Structure | Best For | Key Property |
| Ball Tree | Any metric satisfying triangle inequality | Partitions using balls, not axis-aligned splits |
| VP-Tree (Vantage Point) | General metric spaces | Only requires a distance function |
| Cover Tree | Low intrinsic dimensionality | Theoretically optimal for doubling metrics |
| LSH (Locality-Sensitive Hashing) | Approximate nearest neighbors | Sublinear time for high dimensions |
Ball Trees and VP-Trees are the most common replacements when you need exact nearest neighbors with a non-Euclidean metric. They partition space using distance-based criteria rather than coordinate-axis splits, so their pruning remains effective as long as the triangle inequality holds.
Performance Comparison
| Metric | KD-Tree Pruning Quality | Recommended Structure |
| Euclidean () | Excellent | KD-Tree |
| Manhattan () | Good | KD-Tree |
| Chebyshev () | Good | KD-Tree |
| Minkowski () | Good | KD-Tree |
| Mahalanobis | Poor | Ball Tree or VP-Tree |
| Custom weighted | Poor to moderate | Ball Tree |
| Non-geometric (edit distance) | Not applicable | VP-Tree or LSH |
Common Pitfalls
- Assuming that any valid metric will give good KD-Tree performance. Correctness and efficiency are separate concerns.
- Forgetting that KD-Trees degrade in high dimensions regardless of metric (the "curse of dimensionality"). For or so, brute force or approximate methods often win.
- Using a function that is not a true metric (violates triangle inequality) and expecting pruning to work. Without the triangle inequality, pruning can skip valid nearest neighbors and return wrong results.
Conclusion
KD-Trees work well with any metric, not just Euclidean distance. For metrics outside the Minkowski family, KD-Trees still return correct results but lose their pruning advantage, often degrading to brute-force performance. When your metric does not align with axis-parallel splits, Ball Trees or VP-Trees are better choices because they partition space based on distance rather than coordinates.
Related reading
- Can I use arbitrary metrics to search KD-Trees?
- can it be solved in linear time, did this in n2 time
- Can NS3 be paired with real distributed algorithms?
- Can ONN be faster than ON
- Can I use Kafka queue in my Rest WEBSERVICE
- Can list_display in a Django ModelAdmin display attributes of ForeignKey fields?
- Can someone help solve this recurrence relation?
- Can two groups of N people find each other around a circle?

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.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.