{"id":21978500,"url":"https://github.com/shivammg/trie","last_synced_at":"2025-08-07T07:10:13.053Z","repository":{"id":37443340,"uuid":"496515764","full_name":"shivamMg/trie","owner":"shivamMg","description":"A Trie implementation in Go meant for auto-completion use cases. Supports Levenshtein distance search.","archived":false,"fork":false,"pushed_at":"2023-03-22T13:01:23.000Z","size":6483,"stargazers_count":111,"open_issues_count":0,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-21T08:38:04.635Z","etag":null,"topics":["algorithm","autocomplete","data-structures","edit-distance","go","golang","levenshtein-distance","prefix-tree","search","trie","trie-tree-autocomplete"],"latest_commit_sha":null,"homepage":"https://shivammg.github.io/trie","language":"Go","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/shivamMg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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":"2022-05-26T07:00:59.000Z","updated_at":"2025-04-11T00:12:04.000Z","dependencies_parsed_at":"2024-06-19T00:07:21.467Z","dependency_job_id":"473e9a19-344c-4c39-9bfe-566a7e6d5389","html_url":"https://github.com/shivamMg/trie","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamMg%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamMg%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamMg%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shivamMg%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shivamMg","download_url":"https://codeload.github.com/shivamMg/trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251360443,"owners_count":21577274,"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":["algorithm","autocomplete","data-structures","edit-distance","go","golang","levenshtein-distance","prefix-tree","search","trie","trie-tree-autocomplete"],"created_at":"2024-11-29T16:24:33.563Z","updated_at":"2025-04-28T17:50:10.481Z","avatar_url":"https://github.com/shivamMg.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## trie [![godoc](https://godoc.org/github.com/shivammg/trie?status.svg)](https://godoc.org/github.com/shivamMg/trie) ![Build](https://github.com/shivamMg/trie/actions/workflows/ci.yml/badge.svg?branch=master) [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n\nAn implementation of the [Trie](https://en.wikipedia.org/wiki/Trie) data structure in Go. It provides more features than the usual Trie prefix-search, and is meant to be used for auto-completion.\n\n### Demo\n\nA WebAssembly demo can be tried at [shivamMg.github.io/trie](https://shivammg.github.io/trie/).\n\n### Features\n\n- Keys are `[]string` instead of `string`, thereby supporting more use cases - e.g. []string{the quick brown fox} can be a key where each word will be a node in the Trie\n- Support for Put key and Delete key\n- Support for Prefix search - e.g. searching for _nation_ might return _nation_, _national_, _nationalism_, _nationalist_, etc.\n- Support for Edit distance search (aka Levenshtein distance) - e.g. searching for _wheat_ might return similar looking words like _wheat_, _cheat_, _heat_, _what_, etc.\n- Order of search results is deterministic. It follows insertion order.\n\n### Examples\n\n```go\ntri := trie.New()\n// Put keys ([]string) and values (any)\ntri.Put([]string{\"the\"}, 1)\ntri.Put([]string{\"the\", \"quick\", \"brown\", \"fox\"}, 2)\ntri.Put([]string{\"the\", \"quick\", \"sports\", \"car\"}, 3)\ntri.Put([]string{\"the\", \"green\", \"tree\"}, 4)\ntri.Put([]string{\"an\", \"apple\", \"tree\"}, 5)\ntri.Put([]string{\"an\", \"umbrella\"}, 6)\n\ntri.Root().Print()\n// Output (full trie with terminals ending with ($)):\n// ^\n// ├─ the ($)\n// │  ├─ quick\n// │  │  ├─ brown\n// │  │  │  └─ fox ($)\n// │  │  └─ sports\n// │  │     └─ car ($)\n// │  └─ green\n// │     └─ tree ($)\n// └─ an\n//    ├─ apple\n//    │  └─ tree ($)\n//    └─ umbrella ($)\n\nresults := tri.Search([]string{\"the\", \"quick\"})\nfor _, res := range results.Results {\n\tfmt.Println(res.Key, res.Value)\n}\n// Output (prefix-based search):\n// [the quick brown fox] 2\n// [the quick sports car] 3\n\nkey := []string{\"the\", \"tree\"}\nresults = tri.Search(key, trie.WithMaxEditDistance(2), // An edit can be insert, delete, replace\n\ttrie.WithEditOps())\nfor _, res := range results.Results {\n\tfmt.Println(res.Key, res.EditDistance) // EditDistance is number of edits needed to convert to [the tree]\n}\n// Output (results not more than 2 edits away from [the tree]):\n// [the] 1\n// [the green tree] 1\n// [an apple tree] 2\n// [an umbrella] 2\n\nresult := results.Results[2]\nfmt.Printf(\"To convert %v to %v:\\n\", result.Key, key)\nprintEditOps(result.EditOps)\n// Output (edit operations needed to covert a result to [the tree]):\n// To convert [an apple tree] to [the tree]:\n// - delete \"an\"\n// - replace \"apple\" with \"the\"\n// - don't edit \"tree\"\n\nresults = tri.Search(key, trie.WithMaxEditDistance(2), trie.WithTopKLeastEdited(), trie.WithMaxResults(2))\nfor _, res := range results.Results {\n\tfmt.Println(res.Key, res.Value, res.EditDistance)\n}\n// Output (top 2 least edited results):\n// [the] 1 1\n// [the green tree] 4 1\n```\n\n### References\n\n* https://en.wikipedia.org/wiki/Levenshtein_distance#Iterative_with_full_matrix\n* http://stevehanov.ca/blog/?id=114\n* https://gist.github.com/jlherren/d97839b1276b9bd7faa930f74711a4b6","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivammg%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshivammg%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshivammg%2Ftrie/lists"}