Skip to content

Instantly share code, notes, and snippets.

@dance2die
Created September 16, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dance2die/dd97d22fce289dd3359f307fb2c67364 to your computer and use it in GitHub Desktop.
Save dance2die/dd97d22fce289dd3359f307fb2c67364 to your computer and use it in GitHub Desktop.
TrieBuilder class
public class TrieBuilder
{
public TrieNode BuildTrie(IEnumerable<string> words)
{
TrieNode root = new TrieNode();
foreach (var word in words)
{
Insert(root, word);
}
return root;
}
private void Insert(TrieNode current, string word)
{
foreach (char c in word)
{
current.Children.TryGetValue(c, out TrieNode node);
if (node == null)
{
node = new TrieNode();
current.Children.Add(c, node);
}
current = node;
}
current.IsCompleteWord = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment