{"id":22796023,"url":"https://github.com/krasun/trie","last_synced_at":"2025-09-23T10:50:50.546Z","repository":{"id":54865435,"uuid":"332046492","full_name":"krasun/trie","owner":"krasun","description":"Missing Trie implementation for Go","archived":false,"fork":false,"pushed_at":"2022-01-06T20:37:02.000Z","size":23,"stargazers_count":11,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-12-09T17:00:38.153Z","etag":null,"topics":["digital-tree","go","golang","golang-library","golang-package","prefix-tree","trie","trie-structure","trie-tree","tries"],"latest_commit_sha":null,"homepage":"https://scalabledeveloper.com/posts/go-and-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/krasun.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}},"created_at":"2021-01-22T19:45:41.000Z","updated_at":"2023-10-02T18:45:37.000Z","dependencies_parsed_at":"2022-08-14T05:10:19.896Z","dependency_job_id":null,"html_url":"https://github.com/krasun/trie","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/krasun%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krasun%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krasun","download_url":"https://codeload.github.com/krasun/trie/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":229333196,"owners_count":18056671,"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":["digital-tree","go","golang","golang-library","golang-package","prefix-tree","trie","trie-structure","trie-tree","tries"],"created_at":"2024-12-12T05:09:43.715Z","updated_at":"2025-09-23T10:50:45.521Z","avatar_url":"https://github.com/krasun.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# trie\n\n[![Build](https://github.com/krasun/trie/actions/workflows/build.yml/badge.svg?branch=main)](https://github.com/krasun/trie/actions/workflows/build.yml)\n[![codecov](https://codecov.io/gh/krasun/trie/branch/main/graph/badge.svg?token=rh8BDdHc2v)](https://codecov.io/gh/krasun/trie)\n[![Go Report Card](https://goreportcard.com/badge/github.com/krasun/trie)](https://goreportcard.com/report/github.com/krasun/trie)\n[![GoDoc](https://godoc.org/https://godoc.org/github.com/krasun/trie?status.svg)](https://godoc.org/github.com/krasun/trie)\n\nA missing [trie](https://en.wikipedia.org/wiki/Trie) implementation for Go. \n\n## Installation\n\n```shell\ngo get github.com/krasun/trie\n```\n\n## Usage \n\nKnown limitations: \n1. Empty string keys are **not supported**. And functions won't return an error. \n2. Keys are not ordered, so do not expect any ordering. \n\nWithout any stress:\n\n```go \nt := trie.New() // or trie.NewRuneTrie()  \n\nt.Insert(\"apple\")\nt.Insert(\"alphabet\")\nt.Insert(\"banana\")\n\nt.Contains(\"apple\") // true\nt.Contains(\"app\") // false\nt.Contains(\"banana\") // true\nt.Contains(\"ban\") // false\n\nt.SearchByPrefix(\"a\") // []string{\"apple\", \"alphabet\"}\n\nt.StartsWith(\"a\") // true\n```\n\n### A goroutine-safe (thread-safe) trie\n\nBy default, rune-wise trie are not safe to use concurrently, \nbut it is easy to make them safe. You can wrap any trie into the safe version by: \n```go\nsafeTrie := trie.Safe(trie.NewTrie())\n\n// the same interface as for a regular trie\n```\n\n## Tests \n\nTo run tests, just execute: \n```\n$ go test . -race\n```\n\n## Benchmarking \n\nZero heap allocations for `Contains` function:\n```\n$ go test -bench=. -benchmem -benchtime=1000x\ngoos: darwin\ngoarch: amd64\nop\t       0 allocs/op\nBenchmarkRuneTrieInsert-8          \t    1000\t      7196 ns/op\t    6984 B/op\t     129 allocs/op\nBenchmarkRuneTrieContains-8        \t    1000\t       517 ns/op\t       0 B/op\t       0 allocs/op\nBenchmarkWordHashSetInsert-8       \t    1000\t      1406 ns/op\t    1100 B/op\t       4 allocs/op\nBenchmarkWordHashSetContains-8     \t    1000\t       178 ns/op\t       0 B/op\t       0 allocs/op\nPASS\n```\n\nBut the hash set (map[string]{}struct) is much much more efficient than the trie. Carefully \nweigh when to use the trie.\n\n## Applications\n\nUse the trie in cases when it only outperforms hash tables or hash sets (map[string]{}struct):\n1. Search key by the prefix. \n\n## License \n\ntrie is released under [the MIT license](LICENSE).\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrasun%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrasun%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrasun%2Ftrie/lists"}