PHPackages                             hexogen/kdtree - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. hexogen/kdtree

ActiveLibrary[Caching](/categories/caching)

hexogen/kdtree
==============

file system KDTree index

v0.2.6(1y ago)2095.2k↓44.1%3MITPHPPHP ^7.1|^8.0CI failing

Since Jan 10Pushed 1y ago1 watchersCompare

[ Source](https://github.com/hexogen/kdtree)[ Packagist](https://packagist.org/packages/hexogen/kdtree)[ Docs](https://github.com/hexogen/kdtree)[ RSS](/packages/hexogen-kdtree/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependencies (4)Versions (11)Used By (0)

K-D Tree
========

[](#k-d-tree)

[![Latest Version on Packagist](https://camo.githubusercontent.com/18503cdb39455b495376a7a708b17df3853dae6e819077c6ed9fd6d95a1749f0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6865786f67656e2f6b64747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hexogen/kdtree)[![Build Status](https://camo.githubusercontent.com/6ac38f8e9ef680564e6bce38cf479e1c146dc00f5c4d000232015b4a1ba1161c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6865786f67656e2f6b64747265652f74657374732e796d6c3f6272616e63683d6d6173746572)](https://github.com/hexogen/kdtree/actions?query=workflow%3ATests)[![codecov](https://camo.githubusercontent.com/4bf7e1fe3ea5c56a7b6072ad1f6fcc395671e043623e197aba8bd4a6e14cc4a6/68747470733a2f2f636f6465636f762e696f2f67682f6865786f67656e2f6b64747265652f67726170682f62616467652e7376673f746f6b656e3d3137364c345541305931)](https://codecov.io/gh/hexogen/kdtree)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/1d176b19c0bb4dac4ed73daa9691c10461cf46a55ccd83979c694616f80553c4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6865786f67656e2f6b64747265652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/hexogen/kdtree)

PHP multidimensional K-D Tree implementation.

To receive all benefits from K-D Tree, use file system implementation(FSKDTree). FSKDTree stores tree in binary format and uses lazy loading while traversing through nodes. Current approach provides much higher performance compared to deserialization.

Install
-------

[](#install)

Via Composer

```
$ composer require hexogen/kdtree
```

Usage
-----

[](#usage)

### Tree creation

[](#tree-creation)

```
//Item container with 2 dimensional points
$itemList = new ItemList(2);

//Adding 2 - dimension items to the list
$itemList->addItem(new Item(1, [1.2, 4.3]));
$itemList->addItem(new Item(2, [1.3, 3.4]));
$itemList->addItem(new Item(3, [4.5, 1.2]));
$itemList->addItem(new Item(4, [5.2, 3.5]));
$itemList->addItem(new Item(5, [2.1, 3.6]));

//Building tree with given item list
$tree = new KDTree($itemList);
```

### Searching nearest items to the given point

[](#searching-nearest-items-to-the-given-point)

```
//Creating search engine with custom algorithm (currently Nearest Search)
$searcher = new NearestSearch($tree);

//Retrieving a result ItemInterface[] array with given size (currently 2)
$result = $searcher->search(new Point([1.25, 3.5]), 2);

echo $result[0]->getId(); // 2
echo $result[0]->getNthDimension(0); // 1.3
echo $result[0]->getNthDimension(1); // 3.4

echo $result[1]->getId(); // 1
echo $result[1]->getNthDimension(0); // 1.2
echo $result[1]->getNthDimension(1); // 4.3
```

### Persist tree to a binary file

[](#persist-tree-to-a-binary-file)

```
//Init tree writer
$persister = new FSTreePersister('/path/to/dir');

//Save the tree to /path/to/dir/treeName.bin
$persister->convert($tree, 'treeName.bin');
```

### File system version of the tree

[](#file-system-version-of-the-tree)

```
//ItemInterface factory
$itemFactory = new ItemFactory();

//Then init new instance of file system version of the tree
$fsTree = new FSKDTree('/path/to/dir/treeName.bin', $itemFactory);

//Now use fs kdtree to search
$fsSearcher = new NearestSearch($fsTree);

//Retrieving a result ItemInterface[] array with given size (currently 2)
$result = $fsSearcher->search(new Point([1.25, 3.5]), 2);

echo $result[0]->getId(); // 2
echo $result[1]->getId(); // 1
```

Change log
----------

[](#change-log)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Testing
-------

[](#testing)

```
$ composer test
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Volodymyr Basarab](https://github.com/hexogen)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance32

Infrequent updates — may be unmaintained

Popularity41

Moderate usage in the ecosystem

Community22

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~311 days

Recently: every ~236 days

Total

10

Last Release

701d ago

PHP version history (3 changes)0.1.0PHP ~7.0

0.2.0PHP ~7.1

0.2.1PHP ^7.1|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/fbe2c119d7d9b5123a151d05120fd8e07bea6830a4e19831c54252dc41377ea3?d=identicon)[hexogen](/maintainers/hexogen)

---

Top Contributors

[![hexogen](https://avatars.githubusercontent.com/u/4229736?v=4)](https://github.com/hexogen "hexogen (112 commits)")[![philsturgeon](https://avatars.githubusercontent.com/u/67381?v=4)](https://github.com/philsturgeon "philsturgeon (44 commits)")[![frankdejonge](https://avatars.githubusercontent.com/u/534693?v=4)](https://github.com/frankdejonge "frankdejonge (20 commits)")[![ravage84](https://avatars.githubusercontent.com/u/625761?v=4)](https://github.com/ravage84 "ravage84 (10 commits)")[![jotaelesalinas](https://avatars.githubusercontent.com/u/2042875?v=4)](https://github.com/jotaelesalinas "jotaelesalinas (8 commits)")[![RobLoach](https://avatars.githubusercontent.com/u/25086?v=4)](https://github.com/RobLoach "RobLoach (7 commits)")[![hansott](https://avatars.githubusercontent.com/u/3886384?v=4)](https://github.com/hansott "hansott (6 commits)")[![bcrowe](https://avatars.githubusercontent.com/u/752603?v=4)](https://github.com/bcrowe "bcrowe (5 commits)")[![reinink](https://avatars.githubusercontent.com/u/882133?v=4)](https://github.com/reinink "reinink (5 commits)")[![colinodell](https://avatars.githubusercontent.com/u/202034?v=4)](https://github.com/colinodell "colinodell (5 commits)")[![EmanueleMinotto](https://avatars.githubusercontent.com/u/417201?v=4)](https://github.com/EmanueleMinotto "EmanueleMinotto (5 commits)")[![marcqualie](https://avatars.githubusercontent.com/u/101022?v=4)](https://github.com/marcqualie "marcqualie (4 commits)")[![hassankhan](https://avatars.githubusercontent.com/u/1781985?v=4)](https://github.com/hassankhan "hassankhan (3 commits)")[![browner12](https://avatars.githubusercontent.com/u/5232313?v=4)](https://github.com/browner12 "browner12 (3 commits)")[![alexbilbie](https://avatars.githubusercontent.com/u/77991?v=4)](https://github.com/alexbilbie "alexbilbie (2 commits)")[![assertchris](https://avatars.githubusercontent.com/u/200609?v=4)](https://github.com/assertchris "assertchris (2 commits)")[![barryvdh](https://avatars.githubusercontent.com/u/973269?v=4)](https://github.com/barryvdh "barryvdh (2 commits)")[![kdubuc](https://avatars.githubusercontent.com/u/895661?v=4)](https://github.com/kdubuc "kdubuc (2 commits)")[![mAAdhaTTah](https://avatars.githubusercontent.com/u/4371429?v=4)](https://github.com/mAAdhaTTah "mAAdhaTTah (2 commits)")[![sanmai](https://avatars.githubusercontent.com/u/139488?v=4)](https://github.com/sanmai "sanmai (1 commits)")

---

Tags

algorithmscachedata-structuresindexkd-treemultidimensionalitysearch-algorithmsearchdata structuresalgorithmskdtreehexogen

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/hexogen-kdtree/health.svg)

```
[![Health](https://phpackages.com/badges/hexogen-kdtree/health.svg)](https://phpackages.com/packages/hexogen-kdtree)
```

###  Alternatives

[doganoo/php-algorithms

A collection of common algorithms implemented in PHP. The collection is based on "Cracking the Coding Interview" by Gayle Laakmann McDowell

9456.3k7](/packages/doganoo-php-algorithms)[redtrine/redtrine

Redis-based advanced PHP data structures.

3414.5k](/packages/redtrine-redtrine)[iazaran/smart-cache

Smart Cache is a caching optimization package designed to enhance the way your Laravel application handles data caching. It intelligently manages large data sets by compressing, chunking, or applying other optimization strategies to keep your application performant and efficient.

21114.2k](/packages/iazaran-smart-cache)[ichhabrecht/intcache

Turn uncachable page objects into cacheable links

193.9k](/packages/ichhabrecht-intcache)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
