Repository navigation
Trie
Our Trie class provides a generic implementation of the Trie data structure. A Trie is similar in functionality to a Dictionary in that it provides a mapping from keys to values. It's strength over a traditional dictionary is that it is designed for use cases where keys can be decomposed into key bits and stored in a tree structure. This both reduces memory usage by avoiding unnecessary duplication of key data but also allows for efficient prefix searches. Tries are a useful data structure for doing auto-complete and other prefix search type tasks, they also lends themselves to use as a cache for value interning.
Our Trie may use any value or reference type as the key and key bit types, however the value type must be a reference type.
Our Trie implementation is designed to be thread safe and allows for multiple concurrent readers and writers provided they are accessing different parts of the tree structure. Note that it is theoretically possible for two threads to modify the tree in conflicting ways so generally we would suggest sticking to multi-reader or single writer.
Our Trie has a single constructor like so:
public Trie(Func<TKey, IEnumerable<TKeyBit>> keyMapper)This simply takes a function which decomposes your key type into a sequence of key bits. This is the default decomposition function used for storage and lookup, for some lookup operations you can supply a specific lookup function which is useful for custom searches.
For the rest of these examples we'll assume we've created a simple Trie of strings to strings like so:
//StringTrie is a convinience class that provides a Trie<String, char, T>
StringTrie<T> trie = new StringTrie<String>();Notice we used the convenience class StringTrie to simplify our code, this is equivalent to doing the following:
//Here we use a lambda to provide our key to key bit mapping function
Trie<String, char, String> trie = new Trie<String, char, String>(s => s.ToCharArray());The basic add and remove functionality looks very similar to most other .NET data structures. You can add data like so:
trie.Add("mykey", "Some value");And remove data like so:
trie.Remove("mykey");To search the Trie there is a Find() method with various overloads provided. A simple Find() attempts to retrieve the TrieNode associated with a given key, it returns either the node associated with that key or null if the key doesn't lead to a Node e.g.
TrieNode<char, String> node = trie.Find("mykey");Sometimes you may want to lookup a node either using a custom key mapping function or a custom set of key bits. For example we can lookup a node based on the first 3 characters of a key:
TrieNode<char, String> node = trie.Find("mykey", s => s.ToCharArray().Take(3));Sometimes it is useful to move to a specific node even if that node does not yet exist. For this we provide a MoveToNode() method. This method is similar to Add() in that it moves to the node that corresponds to the given key but it returns the node rather than setting a value on the Node. This gives your more control over how you manipulate your Trie.
TrieNode<char, String> node = trie.MoveToNode("mykey");
//Do what we want with the node...See TrieNode for the APIs available on a node.