{"id":22279052,"url":"https://github.com/kpol/trie","last_synced_at":"2025-05-16T11:06:26.139Z","repository":{"id":2649926,"uuid":"3640156","full_name":"kpol/trie","owner":"kpol","description":"Trie (a.k.a. prefix tree) C# implementation. Has constant-time string prefix lookup.","archived":false,"fork":false,"pushed_at":"2025-01-19T21:53:28.000Z","size":1500,"stargazers_count":108,"open_issues_count":2,"forks_count":19,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-05-04T01:39:54.930Z","etag":null,"topics":["algorithm","c-sharp","data-structure","prefix-tree","trie"],"latest_commit_sha":null,"homepage":"","language":"C#","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/kpol.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":"2012-03-06T16:49:55.000Z","updated_at":"2025-04-25T17:00:49.000Z","dependencies_parsed_at":"2024-03-07T10:55:49.997Z","dependency_job_id":"52221da8-de69-4c18-9467-03489158ca36","html_url":"https://github.com/kpol/trie","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpol%2Ftrie","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpol%2Ftrie/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpol%2Ftrie/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kpol%2Ftrie/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kpol","download_url":"https://codeload.github.com/kpol/trie/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252420971,"owners_count":21745152,"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","c-sharp","data-structure","prefix-tree","trie"],"created_at":"2024-12-03T15:18:01.910Z","updated_at":"2025-05-16T11:06:21.130Z","avatar_url":"https://github.com/kpol.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"Trie\n------\n**Trie** (a.k.a. prefix tree)  is an ordered tree data structure that is used to store an associative array where the keys are usually strings. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.  \n*Reference*: [Wikipedia](http://en.wikipedia.org/wiki/Trie)\n\n[![CI Build](https://github.com/kpol/trie/workflows/CI%20Build/badge.svg)](https://github.com/kpol/trie/actions?query=workflow%3A%22CI+Build%22)\n[![Nuget](https://img.shields.io/nuget/v/KTrie.svg?logo=nuget)](https://www.nuget.org/packages/KTrie)\n\nAdvantages\n------\n - Looking up keys is faster. Looking up a key of length **key** takes **O(|key|)** time\n - Looking up prefixes is faster. Looking up a prefix takes **O(|prefix|)** time\n - Removing takes **O(|key|)** time\n\n```\nTrie trie = [\"star\", \"start\", \"stack\", \"stop\", \"stay\", \"key\"];\n\n          {root}\n            /\\\n           s  k\n          /    \\\n         t      e\n        / \\      \\\n      a    o     [y]\n    / | \\    \\\n  [r][y] c   [p]\n  /       \\\n[t]       [k]\n\nwhere [char] -- is end of word\n```\n\nThe library provides two implementations of the trie data structure:\n - `Trie` : `ICollection\u003cstring\u003e`, this is a set which stores unique strings\n - `TrieDictionary\u003cTValue\u003e` : `IDictionary\u003cstring, TValue\u003e`, this is a key-value-pair collection\n\nTutorial\n------\nTrieDictionary initialization:\n\n    // Initialization\n    TrieDictionary\u003cint\u003e trie = [];\n\n    // or using constructor with comparer\n    IEqualityComparer\u003cchar\u003e comparer = ...; // specify the comparer\n    TrieDictionary\u003cint\u003e trieWithComparer = new(comparer);\n\nAdding items to trie\n\n    trie.Add(\"key\", 17);\n\nThe `Add` method throws `ArgumentNullException` if a value with the specified key already exists, however setting the `Item` property overwrites the old value. In other words, `TrieDictionary\u003cTValue\u003e` has the same behavior as `Dictionary\u003cTKey, TValue\u003e`.\n\nThe main advantage of trie is really fast prefix lookup. To find all items of `TrieDictionary\u003cTValue\u003e` which have keys with given prefix use `StartsWith` method which returns `IEnumerable\u003cKeyValuePair\u003cstring, TValue\u003e\u003e`:\n\n    var result = trie.StartsWith(\"abc\");\n\nAnother handy method is `Matches(IReadOnlyList\u003cCharacter\u003e pattern)`\n\n    var result = trie.Matches([Character.Any, 'c', Character.Any, Character.Any, 't']);\n\nwhich will return all words that match this regex: `^.c.{2}t$`, e.g.: `octet`, `scout`, `scoot`. \n\nThere are two overloads of the `StartsWith` method:\n - `StartsWith(string value)`\n - `StartsWith(IReadOnlyList\u003cCharacter\u003e pattern)`\n\nBenchmark tests\n------\nFor performance tests I used 370105 English words (from: https://github.com/dwyl/english-words).\n\n| Method                               | Mean          | Error        | StdDev       | Allocated    |\n|------------------------------------- |--------------:|-------------:|-------------:|-------------:|\n| Load_Trie                            | 211,557.48 us | 1,981.525 us | 1,756.570 us |  72741.27 KB |\n| Load_DictionaryWithAllPrefixes       | 577,935.48 us | 6,096.177 us | 5,090.583 us | 317389.57 KB |\n| Trie_StartsWith                      |  11,420.52 us |    78.619 us |    69.693 us |   3604.64 KB |\n| Linq_StartsWith                      | 117,671.68 us | 1,777.550 us | 1,662.722 us |   2843.55 KB |\n| Linq_GroupedByFirstLetter_StartsWith |  10,544.61 us |   206.705 us |   339.622 us |   2844.41 KB |\n| Linq_DictionaryWithAllPrefixes       |   3,593.91 us |    69.920 us |    80.520 us |   2840.66 KB |\n| Trie_Matches                         |      15.13 us |     0.298 us |     0.446 us |     18.05 KB |\n| Trie_PatternStartsWith               |      66.07 us |     1.306 us |     1.504 us |     65.65 KB |\n| String_PatternMatching               |     887.43 us |    13.962 us |    12.377 us |      1.56 KB |\n| String_PrefixPatternMatching         |     911.10 us |    14.261 us |    13.340 us |     33.72 KB |\n| Regex_PatternMatching                |  27,146.03 us |   232.150 us |   217.153 us |      1.57 KB |\n| Regex_PrefixPatternMatching          |  27,414.88 us |   265.306 us |   248.168 us |     33.73 KB |\n\n------\n\u0026copy; Kirill Polishchuk\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkpol%2Ftrie","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkpol%2Ftrie","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkpol%2Ftrie/lists"}