public void Add(string word, int index = 0) { if (word.Length == index + 1) { IsWord = true; } else { var next = word[index + 1]; WordNode child; if (!Children.TryGetValue(next, out child)) { Children[next] = new WordNode(next); } else if (Children[next].Children.Count == 0) { Children[next] = new WordNode(next) { IsWord = true } } ; Children[next].Add(word, index + 1); } }
private void Build(string[] words) { foreach (var word in words) { var first = word[0]; WordNode child; if (!Children.TryGetValue(first, out child)) { child = new WordNode(first); Children.Add(first, child); } child.Add(word); } }
public void Add(string word, int index = 0) { if (word.Length == index + 1) IsWord = true; else { var next = word[index + 1]; WordNode child; if (!Children.TryGetValue(next, out child)) Children[next] = new WordNode(next); else if (Children[next].Children.Count == 0) Children[next] = new WordNode(next) { IsWord = true }; Children[next].Add(word, index + 1); } }