예제 #1
0
 public TrieNode(char character, IDictionary<char, TrieNode> children, int wordCount, TrieNode parent)
 {
     this.Character = character;
     this.Children = children;
     this.WordCount = wordCount;
     this.Parent = parent;
 }
예제 #2
0
        /// <summary>
        /// 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)
        {
            while (true)
            {
                if (word.Length == 0)
                {
                    trieNode.WordCount++;
                }
                else
                {
                    var firstChar = FirstChar(word);
                    TrieNode child;
                    trieNode.Children.TryGetValue(firstChar, out child);

                    if (child == null)
                    {
                        child = GetTrieNode(firstChar, trieNode);
                        trieNode.Children[firstChar] = child;
                    }

                    var characterRemoved = FirstCharRemoved(word);
                    trieNode = child;
                    word = characterRemoved;
                    continue;
                }
                break;
            }
        }
예제 #3
0
 public Trie(TrieNode root)
 {
     this.root = root;
 }
예제 #4
0
 /// <summary>
 /// Get a new TrieNode instance.
 /// </summary>
 /// <param name="character">Character of the TrieNode.</param>
 private static TrieNode GetTrieNode(char character, TrieNode parent)
 {
     return new TrieNode(character, new Dictionary<char, TrieNode>(), 0, parent);
 }
예제 #5
0
        /// <summary>
        /// Method to remove word. Remove only if node does not 
        /// have children and is not a word node and has a parent node.
        /// </summary>
        private void RemoveNode(TrieNode trieNode)
        {
            while (true)
            {
                // 1. should not have any children
                // 2. should not be a word
                // 3. do not remove root node
                if (trieNode.Children.Count == 0 && !trieNode.IsWord && !Equals(trieNode, this.root))
                {
                    var parent = trieNode.Parent;
                    trieNode.Parent.Children.Remove(trieNode.Character);
                    trieNode.Parent = null;
                    trieNode = parent;
                    continue;
                }

                break;
            }
        }
예제 #6
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--;
            }
        }