/// <summary> /// Recursive method to get all the words starting from given TrieNode. /// </summary> private void GetWords(TrieNode trieNode, ICollection<string> words, StringBuilder buffer) { if (trieNode.IsWord) { words.Add(buffer.ToString()); } foreach (var child in trieNode.Children.Values) { buffer.Append(child.Character); this.GetWords(child, words, buffer); // Remove recent character buffer.Length--; } }
/// <summary> /// Create a new Trie instance. /// </summary> /// <param name="rootTrieNode">The root TrieNode.</param> internal Trie(TrieNode rootTrieNode) { this.rootTrieNode = rootTrieNode; }
/// <summary> /// Recursive method to add word. Gets the first char of the word, /// creates the child TrieNode if null, and recurses with the first /// char removed from the word. If the word length is 0, return. /// </summary> private void AddWord(TrieNode trieNode, char[] word) { if (word.Length == 0) { trieNode.IsWord = true; trieNode.WordCount++; } else { var c = Utilities.FirstChar(word); TrieNode child; trieNode.Children.TryGetValue(c, out child); if (child == null) { child = TrieFactory.GetTrieNode(c); trieNode.Children[c] = child; } var cRemoved = Utilities.FirstCharRemoved(word); this.AddWord(child, cRemoved); } }