{"id":17174750,"url":"https://github.com/benhoyt/pybktree","last_synced_at":"2026-04-02T00:39:31.293Z","repository":{"id":46177659,"uuid":"80550753","full_name":"benhoyt/pybktree","owner":"benhoyt","description":"Python BK-tree data structure to allow fast querying of \"close\" matches","archived":false,"fork":false,"pushed_at":"2021-11-10T08:25:00.000Z","size":13,"stargazers_count":189,"open_issues_count":1,"forks_count":23,"subscribers_count":5,"default_branch":"master","last_synced_at":"2026-03-27T14:49:38.821Z","etag":null,"topics":["data-structures","levenshtein-distance","python","tree"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/benhoyt.png","metadata":{"files":{"readme":"README.rst","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-31T18:51:15.000Z","updated_at":"2026-02-19T02:28:46.000Z","dependencies_parsed_at":"2022-08-30T03:00:33.293Z","dependency_job_id":null,"html_url":"https://github.com/benhoyt/pybktree","commit_stats":null,"previous_names":["jetsetter/pybktree"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/benhoyt/pybktree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhoyt%2Fpybktree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhoyt%2Fpybktree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhoyt%2Fpybktree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhoyt%2Fpybktree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/benhoyt","download_url":"https://codeload.github.com/benhoyt/pybktree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/benhoyt%2Fpybktree/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31053554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-27T16:55:14.406Z","status":"ssl_error","status_checked_at":"2026-03-27T16:55:07.885Z","response_time":164,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["data-structures","levenshtein-distance","python","tree"],"created_at":"2024-10-14T23:54:48.775Z","updated_at":"2026-04-02T00:39:31.263Z","avatar_url":"https://github.com/benhoyt.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"pybktree\n========\n\npybktree is a generic, pure Python implementation of a `BK-tree`_ data\nstructure, which allows fast querying of \"close\" matches (for example, matches\nwith small hamming distance or Levenshtein distance). This module is based on\nthe algorithm by Nick Johnson in his `blog article on BK-trees`_.\n\nThe library is `on the Python Package Index (PyPI)`_ and works on both Python\n3 and Python 2.7. To install it, fire up a command prompt, activate your\nvirtual environment if you're using one, and type:\n\n::\n\n    pip install pybktree\n\nExample usage:\n\n.. code:: python\n\n    \u003e\u003e\u003e tree = pybktree.BKTree(pybktree.hamming_distance, [0, 4, 5, 14])\n    \u003e\u003e\u003e tree.add(15)              # add element 15\n    \u003e\u003e\u003e sorted(tree)              # BKTree instances are iterable\n    [0, 4, 5, 14, 15]\n    \u003e\u003e\u003e sorted(tree.find(13, 1))  # find elements at most 1 bit away from element 13\n    [(1, 5), (1, 15)]\n\nIf you need to track the ID, key, or filename of the original item, use a\ntuple or namedtuple. Repeating the above example with an ``Item`` namedtuple:\n\n.. code:: python\n\n    \u003e\u003e\u003e import collections\n    \u003e\u003e\u003e Item = collections.namedtuple('Item', 'bits id')\n    \u003e\u003e\u003e def item_distance(x, y):\n    ...     return pybktree.hamming_distance(x.bits, y.bits)\n    \u003e\u003e\u003e tree = pybktree.BKTree(item_distance, [Item(0, 'a'), Item(4, 'b'),\n                                               Item(5, 'c'), Item(14, 'd')])\n    \u003e\u003e\u003e tree.add(Item(15, 'e'))\n    \u003e\u003e\u003e sorted(tree.find(Item(13, 'x'), 1))\n    [(1, Item(bits=5, id='c')), (1, Item(bits=15, id='e'))]\n\nFor large trees and fairly small N when calling ``find()``, using a BKTree is\nmuch faster than doing a linear search. This is especially good when you're\nde-duping a few hundred thousand photos -- with a linear search that would\nbecome a very slow, O(N) for every photo, so O(N²) in total.\n\nA lookup in a BKTree is much faster than linear for small distance thresholds --\nthough it goes up to O(N) far large distance thresholds, so won't be valuable in\nthose cases. See Maximilian Knespel's `detailed analysis`_.\n\nRead the code in `pybktree.py`_ for more details – it's pretty small!\n\nOther BK-tree modules I found on GitHub while writing this one:\n\n* `ahupp/bktree`_: this one is pretty good, but it's not on PyPI, and it's\n  recursive\n* `ryanfox/bktree`_: this one is hard to customize, ``search()`` doesn't\n  return distances, it's slower, and was buggy (though I think he fixed it\n  recently)\n\npybktree was written by `Ben Hoyt`_ and is licensed with a\npermissive MIT license (see `LICENSE.txt`_).\n\n\n.. _BK-tree: https://en.wikipedia.org/wiki/BK-tree\n.. _blog article on BK-trees: http://blog.notdot.net/2007/4/Damn-Cool-Algorithms-Part-1-BK-Trees\n.. _on the Python Package Index (PyPI): https://pypi.python.org/pypi/pybktree\n.. _pybktree.py: https://github.com/benhoyt/pybktree/blob/master/pybktree.py\n.. _ahupp/bktree: https://github.com/ahupp/bktree\n.. _ryanfox/bktree: https://github.com/ryanfox/bktree\n.. _Ben Hoyt: http://benhoyt.com/\n.. _LICENSE.txt: https://github.com/benhoyt/pybktree/blob/master/LICENSE.txt\n.. _detailed analysis: https://github.com/benhoyt/pybktree/issues/5\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenhoyt%2Fpybktree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbenhoyt%2Fpybktree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbenhoyt%2Fpybktree/lists"}