Exemple #1
0
        /// <summary>
        /// Adds a word to the <see cref="SuffixTree"/>.
        /// </summary>
        /// <param name="word">The word to add.</param>
        public void Add(string word)
        {
            var  curNode             = Root;
            bool wordWasAlreadyAdded = false;

            for (int i = word.Length - 1; i >= 0; i--)
            {
                if (curNode.children.ContainsKey(word[i]))
                {
                    if (i == 0)
                    {
                        wordWasAlreadyAdded = curNode.children[word[i]].IsTerminal;
                        curNode.children[word[i]].IsTerminal = true;
                    }
                    else
                    {
                        curNode = curNode.children[word[i]];
                    }
                }
                else
                {
                    if (i == 0)
                    {
                        var newNode = new SuffixTreeNode(word[i], true);
                        curNode.children.Add(word[i], newNode);
                    }
                    else
                    {
                        var newNode = new SuffixTreeNode(word[i], false);
                        curNode.children.Add(word[i], newNode);

                        curNode = curNode.children[word[i]];
                    }
                }
            }

            if (!wordWasAlreadyAdded)
            {
                Count++;
            }
        }
Exemple #2
0
 /// <summary>
 /// Creates a new instance of the <see cref="SuffixTree"/> class.
 /// </summary>
 public SuffixTree()
 {
     Root = new SuffixTreeNode(default(char), false);
 }
Exemple #3
0
 /// <summary>
 /// Removes all words from the <see cref="SuffixTree"/>.
 /// </summary>
 public void Clear()
 {
     Root  = new SuffixTreeNode(default(char), false);
     Count = 0;
 }