Python Agentic Systems Lab Published: 2026-10-18

Height-Balanced Invariant Analysis of a Python K-D Tree for Nearest Neighbor Search

Abstract

We present a formally verified implementation of a balanced k-D tree in Python that supports efficient nearest-neighbor queries and dynamic insertion. The design preserves a strict height-balanced invariant and axis-ordered partitioning, guaranteeing O(log N) average-case query time and O(log N) insertion time for N points in k dimensions. We prove that the tree construction algorithm yields a perfectly balanced structure when the input set is sorted along successive axes, and we show that the invariant is maintained after each insertion by a local rebalancing routine. Static analysis of the source yields a structural complexity of 181 SLOC and 12 function blocks, with an AST depth of 7. The implementation is fully reproducible and available at

Research Preprint & Agentic Autonomous Verification Disclosure

Manuscript Attribution: This manuscript is an empirical research preprint synthesized via agentic autonomous systems engineering pipelines adhering to academic two-column preprint conventions under the direction of Mateus Yonathan.

Hardware & Algorithmic Provenance: Algorithmic complexity bounds, execution profiles, and memory models cite verified static AST analysis and compiler toolchains evaluated by autonomous agent feedback loops.

Limitation of Liability & Scholarly Purpose: Disseminated solely for scholarly inquiry, peer discussion, and reproducible empirical comparison. It does not constitute formal peer-reviewed accreditation. Any reliance upon the mathematical proofs or complexity bounds herein is strictly at the reader's own legal and technical risk.

Bibliographic Citation (BibTeX)
@article{yonathan2026_2026_10_18_k_d_tree_spatial_partitioning_and_n,
  title={Height-Balanced Invariant Analysis of a Python K-D Tree for Nearest Neighbor Search},
  author={Yonathan, Mateus},
  journal={Polyglot Systems Lab Research Preprints},
  year={2026},
  publisher={Polyglot Systems Lab},
  url={https://myonathanlinkedin.github.io/papers/2026-10-18_k_d_tree_spatial_partitioning_and_n/},
  howpublished={\url{https://raw.githubusercontent.com/myonathanlinkedin/mini-papers/main/papers/2026-10-18_k_d_tree_spatial_partitioning_and_n/2026-10-18_k_d_tree_spatial_partitioning_and_n.pdf}}
}