TrieNode is an internal object to encapsulate recursive, helper etc. methods.
Example #1
0
 /// <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;
 }
Example #2
0
 /// <summary>
 /// Get a new TrieNode instance.
 /// </summary>
 /// <param name="character">Character of the TrieNode.</param>
 internal static TrieNode GetTrieNode(char character, 
     TrieNode parent)
 {
     return new TrieNode(character,
         new Dictionary<char, TrieNode>(),
         0,
         parent
         );
 }
Example #3
0
        /// <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--;
            }
        }
Example #4
0
 /// <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);
     }
 }
Example #5
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)
 {
     foreach (var c in word)
     {
         var child = trieNode.GetChild(c);
         if (child == null)
         {
             child = new TrieNode(c);
             trieNode.SetChild(child);
         }
         trieNode = child;
     }
     trieNode.WordCount++;
 }
Example #6
0
 /// <summary>
 /// Create a new Trie instance.
 /// </summary>
 internal Trie()
 {
     rootTrieNode = new TrieNode(' ');
 }
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 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)
 {
     if (trieNode.Children.Count == 0 // should not have any children
         && !trieNode.IsWord // should not be a word
         && trieNode != rootTrieNode // do not remove root node
         )
     {
         var parent = trieNode.Parent;
         trieNode.Parent.Children.Remove(trieNode.Character);
         trieNode.Parent = null;
         RemoveNode(parent);
     }
 }
Example #9
0
 internal void SetChild(TrieNode child)
 {
     Children[child.Character] = child;
 }
Example #10
0
 /// <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);
     }
 }
Example #11
0
 internal void SetChild(TrieNode child)
 {
     Children[child.Character] = child;
 }
Example #12
0
 internal void RemoveNode(TrieNode rootTrieNode)
 {
     // set this node's word count to 0
     WordCount = 0;
     RemoveNode_Recursive(rootTrieNode);
 }
Example #13
0
 internal void SetChild(TrieNode child)
 {
     if (child == null)
     {
         throw new ArgumentNullException(nameof(child));
     }
     Children[child.Character] = child;
 }
Example #14
0
 internal void RemoveNode(TrieNode rootTrieNode)
 {
     // set this node's word count to 0
     WordCount = 0;
     RemoveNode_Recursive(rootTrieNode);
 }
Example #15
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 == 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--;
            }
        }
Example #16
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++;
     }
     else
     {
         var c = Utilities.FirstChar(word);
         TrieNode child;
         trieNode.Children.TryGetValue(c, out child);
         if (child == null)
         {
             child = TrieFactory.GetTrieNode(c, trieNode);
             trieNode.Children[c] = child;
         }
         var cRemoved = Utilities.FirstCharRemoved(word);
         AddWord(child, cRemoved);
     }
 }
Example #17
0
 /// <summary>
 /// Create a new TrieMap instance.
 /// </summary>
 public TrieMap()
 {
     rootTrieNode = new TrieNode <TValue>(' ');
 }