TrieNode is an internal object to encapsulate recursive, helper etc. methods.
コード例 #1
0
ファイル: TrieNode.cs プロジェクト: nelfurion/TelerikAcademy
 /// <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;
 }
コード例 #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
         );
 }
コード例 #3
0
ファイル: Trie.cs プロジェクト: rmandvikar/csharp-trie
        /// <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--;
            }
        }
コード例 #4
0
ファイル: Trie.cs プロジェクト: rmandvikar/csharp-trie
 /// <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);
     }
 }
コード例 #5
0
ファイル: Trie.cs プロジェクト: rmandvikar/csharp-trie
 /// <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++;
 }
コード例 #6
0
ファイル: Trie.cs プロジェクト: rmandvikar/csharp-trie
 /// <summary>
 /// Create a new Trie instance.
 /// </summary>
 internal Trie()
 {
     rootTrieNode = new TrieNode(' ');
 }
コード例 #7
0
ファイル: Trie.cs プロジェクト: MarKamenov/TelerikAcademy
 /// <summary>
 /// Create a new Trie instance.
 /// </summary>
 /// <param name="rootTrieNode">The root TrieNode.</param>
 internal Trie(TrieNode rootTrieNode)
 {
     this.rootTrieNode = rootTrieNode;
 }
コード例 #8
0
ファイル: Trie.cs プロジェクト: MarKamenov/TelerikAcademy
 /// <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);
     }
 }
コード例 #9
0
 internal void SetChild(TrieNode child)
 {
     Children[child.Character] = child;
 }
コード例 #10
0
ファイル: TrieNode.cs プロジェクト: nelfurion/TelerikAcademy
 /// <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);
     }
 }
コード例 #11
0
ファイル: TrieNode.cs プロジェクト: nelfurion/TelerikAcademy
 internal void SetChild(TrieNode child)
 {
     Children[child.Character] = child;
 }
コード例 #12
0
ファイル: TrieNode.cs プロジェクト: nelfurion/TelerikAcademy
 internal void RemoveNode(TrieNode rootTrieNode)
 {
     // set this node's word count to 0
     WordCount = 0;
     RemoveNode_Recursive(rootTrieNode);
 }
コード例 #13
0
ファイル: TrieNode.cs プロジェクト: rmandvikar/csharp-trie
 internal void SetChild(TrieNode child)
 {
     if (child == null)
     {
         throw new ArgumentNullException(nameof(child));
     }
     Children[child.Character] = child;
 }
コード例 #14
0
 internal void RemoveNode(TrieNode rootTrieNode)
 {
     // set this node's word count to 0
     WordCount = 0;
     RemoveNode_Recursive(rootTrieNode);
 }
コード例 #15
0
ファイル: Trie.cs プロジェクト: rmandvikar/csharp-trie
        /// <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--;
            }
        }
コード例 #16
0
ファイル: Trie.cs プロジェクト: MarKamenov/TelerikAcademy
 /// <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);
     }
 }
コード例 #17
0
 /// <summary>
 /// Create a new TrieMap instance.
 /// </summary>
 public TrieMap()
 {
     rootTrieNode = new TrieNode <TValue>(' ');
 }