예제 #1
0
 /// <summary>
 /// Create a new Trie instance.
 /// </summary>
 /// <param name="rootTrieNode">The root TrieNode.</param>
 internal Trie(TrieNode rootTrieNode)
 {
     this.RootTrieNode = rootTrieNode;
 }
예제 #2
0
        /// <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--;
            }
        }
예제 #3
0
        /// <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 firstLetter = Utilities.FirstChar(word);
                TrieNode child;
                trieNode.Children.TryGetValue(firstLetter, out child);

                if (child == null)
                {
                    child = TrieFactory.GetTrieNode(firstLetter);
                    trieNode.Children[firstLetter] = child;
                }

                var firstLetterRemoved = Utilities.FirstCharRemoved(word);

                this.AddWord(child, firstLetterRemoved);
            }
        }