Spatial Data Structures

Spatial indexing structures for multi-dimensional data: k-d trees, R-trees, quadtrees, octrees, grid files, and space-filling curves (Z-order, Hilbert).

The data

Structures

NameDimensionsQuery timeSpaceDescriptionOperationsUse casesNotes
k-d Tree (k-dimensional tree)Any kO(n^(1-1/k)) average for nearest neighborO(n)Binary tree alternating split axis at each level; partitions k-dimensional space
  • nearest neighbor
  • range search
  • k-nearest neighbors
  • 3D graphics (ray tracing acceleration)
  • Point cloud processing
  • Recommendation systems (k-NN)
  • Robotics path planning
Not suitable for high dimensions (curse of dimensionality); best for k ≤ 20
R-Tree2D/3D spatialO(log n) averageO(n)Balanced tree grouping nearby objects with minimum bounding rectangles (MBRs); optimized for disk storage
  • range search
  • nearest neighbor
  • spatial join
  • window query
  • GIS systems (PostGIS, SpatiaLite)
  • Location-based services
  • Game engines (collision detection)
  • Database spatial indexes
Variants: R*-tree (better split strategy), R+-tree (no overlap), STR-packed (bulk loading)
Quadtree2DO(log n) averageO(n)Recursive subdivision of 2D space into 4 quadrants; leaf nodes contain points or regions
  • point query
  • range search
  • nearest neighbor
  • region query
  • 2D game collision detection
  • Image compression (quadtree encoding)
  • Mesh generation
  • Sparse matrix storage
Point quadtree (stores points) vs region quadtree (stores areas); unbalanced without care
Octree3DO(log n) averageO(n)3D extension of quadtree; recursively subdivides space into 8 octants
  • point query
  • range search
  • frustum culling
  • collision detection
  • 3D game engines (Unreal, Unity)
  • Point cloud processing (LiDAR)
  • Voxel engines (Minecraft-like)
  • 3D mesh simplification
Essential for 3D spatial partitioning; often combined with frustum culling for rendering
Grid File2D/low-dimensionalO(1) for exact match, O(k) for rangeO(n + grid_size)Uniform grid overlaying space; each cell points to objects within it
  • point query
  • range search
  • nearest neighbor
  • Particle systems
  • Molecular dynamics
  • Simple collision detection
  • Real-time physics engines
Simple but suffers from curse of dimensionality; cell size critically affects performance
Z-Order Curve (Morton Order)Any kO(log n) with B-treeO(n)Maps multi-dimensional points to 1D by interleaving binary representations; preserves locality
  • range search via B-tree on Z-values
  • nearest neighbor
  • Geohashing (geographic indexing)
  • GPU spatial sorting
  • Database indexing (HBase, Accumulo)
  • Texture memory layout
Can use standard B-tree/B+-tree on Z-values; locality preservation not as good as Hilbert curve
Hilbert Curve2D (extendable)O(log n) with B-treeO(n)Space-filling curve with better locality preservation than Z-order; maps 2D to 1D continuously
  • range search
  • nearest neighbor via 1D index
  • Image processing
  • Database indexing (better locality than Z-order)
  • Distributed hash tables
  • Cache-oblivious algorithms
More complex to compute than Z-order but significantly better spatial locality; used in Google S2 Geometry

Fetch the same bytes

The static files are identical to what the API returns, but with no rate limit and no server round trip. Use the API when you want a query and a content type; use the files when you want to cache one document.

curl "https://yjtoon.com/api/dataset/spatial-data-structures?format=toon"
const res = await fetch(
  "https://yjtoon.com/static-data/dataset/spatial-data-structures.toon"
);
const toon = await res.text();

Rate limit: 120 requests per minute per IP, no key and no signup. API reference →

Topics

  • spatial
  • kd-tree
  • rtree
  • quadtree
  • octree
  • space-filling-curves
  • geospatial
  • nearest-neighbor