{"id":13710941,"url":"https://github.com/kyroy/kdtree","last_synced_at":"2026-01-17T17:06:04.755Z","repository":{"id":57484572,"uuid":"126839580","full_name":"kyroy/kdtree","owner":"kyroy","description":"A k-d tree implementation in Go.","archived":false,"fork":false,"pushed_at":"2024-04-09T13:27:23.000Z","size":37,"stargazers_count":136,"open_issues_count":5,"forks_count":28,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-03T23:22:54.103Z","etag":null,"topics":["go","golang","kdtree","library","nearest-neighbor","nearest-neighbor-search","tree"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kyroy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-26T14:15:34.000Z","updated_at":"2024-08-03T14:34:49.000Z","dependencies_parsed_at":"2024-06-18T14:04:54.933Z","dependency_job_id":"1b10913e-767d-4d1e-80a7-f1c41508ca47","html_url":"https://github.com/kyroy/kdtree","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyroy%2Fkdtree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyroy%2Fkdtree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyroy%2Fkdtree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kyroy%2Fkdtree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kyroy","download_url":"https://codeload.github.com/kyroy/kdtree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224528285,"owners_count":17326329,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["go","golang","kdtree","library","nearest-neighbor","nearest-neighbor-search","tree"],"created_at":"2024-08-02T23:01:02.470Z","updated_at":"2026-01-17T17:06:04.728Z","avatar_url":"https://github.com/kyroy.png","language":"Go","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"# kdtree\n\n[![GoDoc](https://godoc.org/github.com/kyroy/kdtree?status.svg)](https://godoc.org/github.com/kyroy/kdtree)\n[![Build Status](https://travis-ci.org/kyroy/kdtree.svg?branch=master)](https://travis-ci.org/kyroy/kdtree)\n[![Codecov](https://img.shields.io/codecov/c/github/kyroy/kdtree.svg)](https://codecov.io/gh/kyroy/kdtree)\n[![Go Report Card](https://goreportcard.com/badge/github.com/kyroy/kdtree)](https://goreportcard.com/report/github.com/kyroy/kdtree)\n[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://github.com/kyroy/kdtree/blob/master/LICENSE)\n\nA [k-d tree](https://en.wikipedia.org/wiki/K-d_tree) implementation in Go with:\n- n-dimensional points\n- k-nearest neighbor search\n- range search\n- remove without rebuilding the whole subtree\n- data attached to the points\n- using own structs by implementing a simple 2 function interface \n\n\n## Usage\n\n```bash\ngo get github.com/kyroy/kdtree\n```\n\n```go\nimport \"github.com/kyroy/kdtree\"\n````\n\n\n### Implement the `kdtree.Point` interface\n\n```go\n// Point specifies one element of the k-d tree.\ntype Point interface {\n\t// Dimensions returns the total number of dimensions\n\tDimensions() int\n\t// Dimension returns the value of the i-th dimension\n\tDimension(i int) float64\n}\n```\n\n\n### `points.Point2d`\n\n```go\ntype Data struct {\n\tvalue string\n}\n\nfunc main() {\n\ttree := kdtree.New([]kdtree.Point{\n\t\t\u0026points.Point2D{X: 3, Y: 1},\n\t\t\u0026points.Point2D{X: 5, Y: 0},\n\t\t\u0026points.Point2D{X: 8, Y: 3},\n\t})\n\n\t// Insert\n\ttree.Insert(\u0026points.Point2D{X: 1, Y: 8})\n\ttree.Insert(\u0026points.Point2D{X: 7, Y: 5})\n\n\t// KNN (k-nearest neighbor)\n\tfmt.Println(tree.KNN(\u0026points.Point{Coordinates: []float64{1, 1, 1}}, 2))\n\t// [{3.00 1.00} {5.00 0.00}]\n\t\n\t// RangeSearch\n\tfmt.Println(tree.RangeSearch(kdrange.New(1, 8, 0, 2)))\n\t// [{5.00 0.00} {3.00 1.00}]\n    \n\t// Points\n\tfmt.Println(tree.Points())\n\t// [{3.00 1.00} {1.00 8.00} {5.00 0.00} {8.00 3.00} {7.00 5.00}]\n\n\t// Remove\n\tfmt.Println(tree.Remove(\u0026points.Point2D{X: 5, Y: 0}))\n\t// {5.00 0.00}\n\n\t// String\n\tfmt.Println(tree)\n\t// [[{1.00 8.00} {3.00 1.00} [\u003cnil\u003e {8.00 3.00} {7.00 5.00}]]]\n\n\t// Balance\n\ttree.Balance()\n\tfmt.Println(tree)\n\t// [[[{3.00 1.00} {1.00 8.00} \u003cnil\u003e] {7.00 5.00} {8.00 3.00}]]\n}\n```\n\n### n-dimensional Points (`points.Point`)\n```go\ntype Data struct {\n\tvalue string\n}\n\nfunc main() {\n    tree := kdtree.New([]kdtree.Point{\n        points.NewPoint([]float64{7, 2, 3}, Data{value: \"first\"}),\n        points.NewPoint([]float64{3, 7, 10}, Data{value: \"second\"}),\n        points.NewPoint([]float64{4, 6, 1}, Data{value: \"third\"}),\n    })\n    \n    // Insert\n    tree.Insert(points.NewPoint([]float64{12, 4, 6}, Data{value: \"fourth\"}))\n    tree.Insert(points.NewPoint([]float64{8, 1, 0}, Data{value: \"fifth\"}))\n    \n    // KNN (k-nearest neighbor)\n    fmt.Println(tree.KNN(\u0026points.Point{Coordinates: []float64{1, 1, 1}}, 2))\n    // [{[4 6 1] {third}} {[7 2 3] {first}}]\n    \n    // RangeSearch\n    fmt.Println(tree.RangeSearch(kdrange.New(1, 15, 1, 5, 0, 5)))\n    // [{[7 2 3] {first}} {[8 1 0] {fifth}}]\n    \n    // Points\n    fmt.Println(tree.Points())\n    // [{[3 7 10] {second}} {[4 6 1] {third}} {[8 1 0] {fifth}} {[7 2 3] {first}} {[12 4 6] {fourth}}]\n\n    // Remove\n    fmt.Println(tree.Remove(points.NewPoint([]float64{3, 7, 10}, nil)))\n    // {[3 7 10] {second}}\n\n    // String\n    fmt.Println(tree)\n    // [[\u003cnil\u003e {[4 6 1] {third}} [{[8 1 0] {fifth}} {[7 2 3] {first}} {[12 4 6] {fourth}}]]]\n\n    // Balance\n    tree.Balance()\n    fmt.Println(tree)\n    // [[[{[7 2 3] {first}} {[4 6 1] {third}} \u003cnil\u003e] {[8 1 0] {fifth}} {[12 4 6] {fourth}}]]\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyroy%2Fkdtree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkyroy%2Fkdtree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkyroy%2Fkdtree/lists"}