Created
September 16, 2017 15:19
-
-
Save dance2die/dd97d22fce289dd3359f307fb2c67364 to your computer and use it in GitHub Desktop.
TrieBuilder class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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