/// <summary> /// DFS traversal starting from given TrieNode and yield. /// </summary> private IEnumerable <TResult> Traverse <TResult>(TrieNode <TValue> trieNode, StringBuilder buffer, Func <StringBuilder, TValue, TResult> transform) { if (trieNode == null) { yield break; } if (trieNode.HasValue()) { yield return(transform ( buffer, trieNode.Value )); } foreach (var child in trieNode.GetChildren()) { // buffer is not used always but it's ok buffer.Append(child.Character); foreach (var item in Traverse(child, buffer, transform)) { yield return(item); } buffer.Length--; } }
/// <summary> /// Gets word count in the Trie. /// </summary> private void GetCount(TrieNode trieNode, Wrapped <int> count, bool isUnique) { if (trieNode.IsWord) { count.Value += isUnique ? 1 : trieNode.WordCount; } foreach (var child in trieNode.GetChildren()) { GetCount(child, count, isUnique); } }
/// <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> /// Gets all the words recursively starting from given TrieNode. /// </summary> private IEnumerable <string> Traverse(TrieNode trieNode, StringBuilder buffer) { if (trieNode == null) { yield break; } if (trieNode.IsWord) { yield return(buffer.ToString()); } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); foreach (var word in Traverse(child, buffer)) { yield return(word); } buffer.Length--; } }
/// <summary> /// Gets shortest words recursively starting from given TrieNode. /// </summary> private void GetShortestWords(TrieNode trieNode, ICollection <string> shortestWords, StringBuilder buffer, Wrapped <int> length) { if (trieNode.IsWord) { if (buffer.Length < length.Value) { shortestWords.Clear(); length.Value = buffer.Length; } if (buffer.Length == length.Value) { shortestWords.Add(buffer.ToString()); } } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); GetShortestWords(child, shortestWords, buffer, length); 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--; } }
/// <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 == null) { return; } 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 shortest words starting from given TrieNode. /// </summary> private void GetShortestWords(TrieNode trieNode, ICollection<string> shortestWords, StringBuilder buffer, Wrapped<int> length) { if (trieNode.IsWord) { if (buffer.Length < length.Value) { shortestWords.Clear(); length.Value = buffer.Length; } if (buffer.Length == length.Value) { shortestWords.Add(buffer.ToString()); } } foreach (var child in trieNode.GetChildren()) { buffer.Append(child.Character); GetShortestWords(child, shortestWords, buffer, length); // Remove recent character buffer.Length--; } }
/// <summary> /// Get word count in the Trie. /// </summary> private void GetCount(TrieNode trieNode, Wrapped<int> count, bool isUnique) { if (trieNode.IsWord) { count.Value += isUnique ? 1 : trieNode.WordCount; } foreach (var child in trieNode.GetChildren()) { GetCount(child, count, isUnique); } }