/// <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.GetChildren()) { buffer.Append(child.Character); GetWords(child, words, buffer); // Remove recent character buffer.Length--; } }
/// <summary> /// Recursive method to get longest words starting from given TrieNode. /// </summary> private void GetLongestWords(TrieNode trieNode, ICollection<string> longestWords, StringBuilder buffer, ref int length) { if (trieNode.IsWord) { if (buffer.Length > length) { longestWords.Clear(); length = buffer.Length; } if (buffer.Length >= length) { longestWords.Add(buffer.ToString()); } } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); GetLongestWords(child, longestWords, buffer, ref length); // Remove recent character buffer.Length--; } }