/// <summary>
 /// Create a new TrieNode instance.
 /// </summary>
 /// <param name="character">The character for the TrieNode.</param>
 /// <param name="children">Children of TrieNode.</param>
 /// <param name="isWord">If root TrieNode to this TrieNode is a word.</param>
 /// <param name="wordCount"></param>
 internal TrieNode(char character, IDictionary<char, TrieNode> children,
     int wordCount, TrieNode parent)
 {
     Character = character;
     Children = children;
     WordCount = wordCount;
     Parent = parent;
 }
 /// <summary>
 /// Create a new TrieNode instance.
 /// </summary>
 /// <param name="character">Character of the TrieNode.</param>
 internal static TrieNode CreateTrieNode(char character, TrieNode parent)
 {
     return new TrieNode(character, new Dictionary<char, TrieNode>(), 0, parent);
 }
 internal void SetChild(TrieNode child)
 {
     Children[child.Character] = child;
 }
 /// <summary>
 /// Recursive 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_Recursive(TrieNode rootTrieNode)
 {
     if (Children.Count == 0 // should not have any children
         && !IsWord // should not be a word
         && this != rootTrieNode // do not remove root node
         )
     {
         var parent = Parent;
         Parent.RemoveChild(Character);
         Parent = null;
         parent.RemoveNode_Recursive(rootTrieNode);
     }
 }
 internal void RemoveNode(TrieNode rootTrieNode)
 {
     // set this node's word count to 0
     WordCount = 0;
     RemoveNode_Recursive(rootTrieNode);
 }
Example #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.GetChildren())
            {
                buffer.Append(child.Character);
                GetWords(child, words, buffer);

                // Remove recent character
                buffer.Length--;
            }
        }
Example #7
0
 /// <summary>
 /// Create a new Trie instance.
 /// </summary>
 /// <param name="rootTrieNode">The root TrieNode.</param>
 internal Trie(TrieNode rootTrieNode)
 {
     this.rootTrieNode = rootTrieNode;
 }
Example #8
0
        /// <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--;
            }
        }
Example #9
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.WordCount++;
                return;
            }

            var c = Utilities.FirstChar(word);
            TrieNode child = trieNode.GetChild(c);
            if (child == null)
            {
                child = TrieFactory.CreateTrieNode(c, trieNode);
                trieNode.SetChild(child);
            }

            var cRemoved = Utilities.FirstCharRemoved(word);
            AddWord(child, cRemoved);
        }