All Projects → kpol → trie

kpol / trie

Licence: MIT license
Trie (a.k.a. prefix tree) C# implementation. Has constant-time string prefix lookup.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to trie

trie-perf
Performance shootout of various trie implementations
Stars: ✭ 18 (-78.57%)
Mutual labels:  trie, prefix-tree
prefixtree
A prefix tree (trie) implementation in go
Stars: ✭ 21 (-75%)
Mutual labels:  trie, prefix-tree
Leetcode
High-quality LeetCode solutions
Stars: ✭ 178 (+111.9%)
Mutual labels:  data-structure, trie
fixie-trie
Compact tries for fixed-width keys
Stars: ✭ 23 (-72.62%)
Mutual labels:  data-structure, trie
csharp-trie
A trie (prefix tree) data structure implementation in C#.
Stars: ✭ 30 (-64.29%)
Mutual labels:  trie, prefix-tree
Data Structures
Data-Structures using C++.
Stars: ✭ 121 (+44.05%)
Mutual labels:  data-structure, trie
Skiplist
skip list with rank, code less than z_set in redis
Stars: ✭ 136 (+61.9%)
Mutual labels:  data-structure
Staticvec
Implements a fixed-capacity stack-allocated Vec alternative backed by an array, using const generics.
Stars: ✭ 236 (+180.95%)
Mutual labels:  data-structure
Apachecn Algo Zh
ApacheCN 数据结构与算法译文集
Stars: ✭ 10,498 (+12397.62%)
Mutual labels:  data-structure
Cs61a
Structure and Interpretation of Computer Programs
Stars: ✭ 123 (+46.43%)
Mutual labels:  data-structure
go-time-series
Time series implementation in Go
Stars: ✭ 27 (-67.86%)
Mutual labels:  data-structure
goblin
A golang http router based on trie tree.
Stars: ✭ 52 (-38.1%)
Mutual labels:  trie
C Macro Collections
Easy to use, header only, macro generated, generic and type-safe Data Structures in C
Stars: ✭ 192 (+128.57%)
Mutual labels:  data-structure
Data structures and algorithms in python
📖 Worked Solutions of "Data Structures & Algorithms in Python", written by Michael T. Goodrich, Roberto Tamassia and Michael H. Goldwasser. ✏️
Stars: ✭ 147 (+75%)
Mutual labels:  data-structure
Go Patricia
A generic patricia trie (also called radix tree) implemented in Go (Golang)
Stars: ✭ 243 (+189.29%)
Mutual labels:  data-structure
Binarytree
Python Library for Studying Binary Trees
Stars: ✭ 1,694 (+1916.67%)
Mutual labels:  data-structure
Mnemonist
Curated collection of data structures for the JavaScript/TypeScript language.
Stars: ✭ 1,752 (+1985.71%)
Mutual labels:  data-structure
CP
Competitive Coding
Stars: ✭ 25 (-70.24%)
Mutual labels:  data-structure
Ordereddictionary
Ordered dictionary data structure implementation in Swift
Stars: ✭ 176 (+109.52%)
Mutual labels:  data-structure
Leetcode Solutions
🏋️ Python / Modern C++ Solutions of All 2111 LeetCode Problems (Weekly Update)
Stars: ✭ 2,787 (+3217.86%)
Mutual labels:  data-structure

Trie

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.
Reference: Wikipedia – trie

CI Build Nuget

Advantages

  • Looking up keys is faster. Looking up a key of length key takes O(|key|) time
  • Looking up prefixes is faster. Looking up a prefix takes O(|prefix|) time
  • Removing takes O(|key|) time

The library provides four implementations of the trie data structure:

  • TrieSet<T>
  • Trie<TKey, TValue>
  • StringTrieSet
  • StringTrie<TValue>

Tutorial

Trie<TValue> implements IDictionary<string, TValue> interface.

Trie initialization:

var trie = new StringTrie<TValue>();

or using constructor which accepts IEqualityComparer<char> comparer interface:

var trie = new StringTrie<TValue>(comparer);

To add items to trie:

trie.Add("key", value);
trie.AddRange(trieEntries);

The Add, AddRange methods throw ArgumentNullException if a value with the specified key already exists, however setting the Item property overwrites the old value. In other words, StringTrie<TValue> has the same behavior as Dictionary<TKey, TValue>.

The main advantage of trie is really fast prefix lookup. To find all items of StringTrie<TValue> which have keys with given prefix use GetByPrefix method which returns IEnumerable<StringEntry<TValue>>:

var result = trie.GetByPrefix("ABC");

Benchmark tests

For performance tests I used 58110 English words of length from 2 to 22 chars. The table below shows prefix lookup time comparing to the Linq Where and string.StartsWith. Number of prefixes: 10

Method Mean Error StdDev
Trie_GetByPrefix 1.879 ms 0.0258 ms 0.0229 ms
Linq_StartsWith 42.685 ms 0.5857 ms 0.5192 ms

© Kirill Polishchuk

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].