kd_tree
Template class implementing a k-dimensional tree data structure for efficient nearest neighbor and radius search operations.Template parameters
Point type that supports array-style indexing with
operator[]Number of dimensions in the point space
Constructor
Constructs a KD-tree from a vector of pointsParameters:
pts(const std::vector<PointT>*): Pointer to vector of points to index
Methods
Finds all points within a specified radius of a query pointParameters:
q(const PointT&): Query pointeps(double): Search radius (epsilon)
eps from query point qInternal structure
Internal node structure containing:
index(std::size_t): Index of the point in the original vectorleft(std::size_t): Index of left child noderight(std::size_t): Index of right child nodeaxis(std::size_t): Splitting axis for this node
Private members
Pointer to the vector of points
Vector of tree nodes representing the KD-tree structure
Static helper methods
Extracts coordinate value from a point at specified axisParameters:
p(const PointT&): Pointaxis(std::size_t): Dimension axis
Computes squared Euclidean distance between two pointsParameters:
a(const PointT&): First pointb(const PointT&): Second point
Example usage
Example with 3D points
Use cases
The KD-tree is particularly useful for:- DBSCAN clustering: Finding neighbors within epsilon radius
- Nearest neighbor search: Efficient spatial queries
- Range queries: Finding all points within a specified region
- High-dimensional data: Scales well with moderate dimensionality
Performance
- Construction time: O(n log n) where n is the number of points
- Radius search: O(log n) average case, O(n) worst case
- Space complexity: O(n) for storing tree nodes