skillfed

kdtree

A Python implemntation of a kd-tree

kdtree v0.16 78.2K downloads/30d#14,464 on PyPI381
Permissive license ISC license AGING released

What it is and what it does

kdtree is a pure-Python implementation of kd-trees, a data structure that partitions points in k-dimensional space to accelerate spatial queries. It lets you build a tree from any collection of indexable objects (tuples, lists, namedtuples, or custom classes that support indexing), then search for the nearest neighbor to any query point in logarithmic time. The tree supports standard operations: insertion, deletion, traversal (inorder and level-order), and rebalancing.

You'd use this when you need to find the closest point(s) in a dataset to a given location—common in machine learning, computational geometry, clustering, and spatial analysis. The package is straightforward: create a tree, add or remove points as needed, and query for nearest neighbors. It handles any number of dimensions and works with any objects that look like tuples to the tree, so you can attach metadata (a payload) to each point without needing a separate index.

Use it for:

  • Find the nearest landmark, location, or object in a spatial dataset given a query coordinate
  • Implement k-nearest-neighbor search for machine learning or clustering algorithms
  • Build a spatial index for collision detection or proximity queries in games or simulations
  • Store and query multi-dimensional data (e.g., feature vectors) with fast nearest-neighbor lookup
  • Attach metadata to spatial points and retrieve both the point and its associated data in one query

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Constructs, modifies, and searches kd-trees—spatial data structures for organizing points in multi-dimensional space to enable efficient nearest-neighbor queries.

Yes, if you need a lightweight, dependency-free kd-tree for nearest-neighbor queries in Python. The package is stable (ISC-licensed, no known vulnerabilities) and suitable for small to medium datasets. However, the last release was 2017-10-19 and maintenance is aging; for production use at scale or with modern Python versions, verify compatibility and consider whether a more actively maintained alternative (such as scipy.spatial.KDTree) better fits your needs.

Install

kdtree on PyPI

pip

pip install kdtree

uv

uv add kdtree

poetry

poetry add kdtree

Installing kdtree

Before you install

Low install friction; pure Python wheel. Maintenance is aging—last release was 2017-10-19 and last commit 2025-05-27, so the codebase is stable but not actively developed. No runtime dependencies.

License in practice

ISC license is permissive; you can use, modify, and distribute this package with minimal restrictions, including in commercial software.

Quickstart

import kdtree

# Create tree from list of points (tuples, lists, or indexable objects)
tree = kdtree.create([(2, 3, 4), (4, 5, 6), (5, 3, 2)])

# Find nearest neighbor to point (1, 2, 3)
nearest = tree.search_nn((1, 2, 3))

# Add and remove points
tree.add((5, 4, 3))
tree = tree.remove((5, 4, 3))

Verify before relying

  • Whether the package is actively maintained or if aging status poses a risk for future Python versions
  • Performance characteristics (insertion/search time complexity) for large datasets
  • Whether rebalancing is automatic or must be called manually to maintain tree efficiency

Package facts

License ISC license (permissive)
Python support not specified
Install friction low — pure-Python wheel
Runtime dependencies none
Maintenance aging — 3,221 days since the last release
Last repo commit
First released
Downloads 78,179/month — #14,464 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: kdtree-0.16-py2.py3-none-any.whl

Development Status :: 5 - Production/StableIntended Audience :: DevelopersLicense :: OSI Approved :: ISC License (ISCL)Operating System :: OS IndependentProgramming Language :: PythonProgramming Language :: Python :: 2Programming Language :: Python :: 2.6Programming Language :: Python :: 2.7Programming Language :: Python :: 3Programming Language :: Python :: 3.3Programming Language :: Python :: 3.4Programming Language :: Python :: 3.5Programming Language :: Python :: 3.6Programming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPyTopic :: Software Development :: LibrariesTopic :: Utilities

Tags

kd-tree nearest neighbor searchspatial indexing pythonmultidimensional point searchkdtree construction and querynearest point lookupspatial data structurek-dimensional tree
spatial-indexingnearest-neighbordata-structures

More Libraries packages