Esempio n. 1
0
        /// <summary>
        /// Get word's longest suffix and value
        /// </summary>
        /// <param name="word">
        /// A <see cref="String"/>
        /// </param>
        /// <returns>
        /// A <see cref="Pair`2"/>
        /// </returns>
        public Pair <String, T> getLongestSuffixAndValue(String word)
        {
            SuffixTreeNode <T> cnode = root;
            int  longestSuffixIndex  = -1;
            T    valueToReturn       = default(T);
            char c;

            for (int i = word.Length - 1; i >= 0; i--)
            {
                c     = word[i];
                cnode = cnode[c];
                if (cnode != null)
                {
                    if (!cnode.ValueIsNull)
                    {
                        longestSuffixIndex = i;
                        valueToReturn      = cnode.Value;
                    }
                }
                else
                {
                    break;
                }
            }
            if (longestSuffixIndex != -1)
            {
                return(new Pair <String, T>(word.Substring(longestSuffixIndex), valueToReturn));
            }
            return(null);
        }
Esempio n. 2
0
        /// <summary>
        /// Add suffix to the Suffix Tree
        /// </summary>
        /// <param name="suffix">
        /// A <see cref="String"/>
        /// </param>
        /// <param name="val">
        /// A <see cref="T"/>
        /// </param>
        public void addSuffix(String suffix, T val)
        {
            SuffixTreeNode <T> node = root;
            char c;

            for (int i = suffix.Length - 1; i >= 0; i--)
            {
                c    = suffix[i];
                node = node.addEdge(c);
            }
            node.ValueIsNull = false;
            node.Value       = val;
        }
Esempio n. 3
0
        private SuffixTreeNode <T> addEdge(char c, T val, bool isNull)
        {
            SuffixTreeNode <T> node;

            if (!edges.TryGetValue(c, out node))
            {
                node = new SuffixTreeNode <T>(val);
                edges.Add(c, node);
                if (isNull)
                {
                    node.isNull = true;
                }
            }
            return(node);
        }
Esempio n. 4
0
        /// <summary>
        /// Checks if Suffix Tree contains word
        /// </summary>
        /// <param name="word">
        /// A <see cref="String"/>
        /// </param>
        /// <returns>
        /// A <see cref="System.Boolean"/>
        /// </returns>
        public bool contains(String word)
        {
            SuffixTreeNode <T> cnode = root;
            char c;

            for (int i = word.Length - 1; i >= 0; i--)
            {
                c     = word[i];
                cnode = cnode[c];
                if (cnode == null)
                {
                    return(false);
                }
            }
            if (cnode != null && !cnode.ValueIsNull)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 5
0
        /// <summary>
        /// Get all the suffixes in the word and their values
        /// </summary>
        /// <param name="word">
        /// A <see cref="String"/>
        /// </param>
        /// <returns>
        /// A <see cref="List`1"/>
        /// </returns>
        public List <Pair <String, T> > getLongestSuffixesAndValues(String word)
        {
            SuffixTreeNode <T> cnode = root;
            char c;
            List <Pair <String, T> > res = new List <Pair <String, T> >();

            for (int i = word.Length - 1; i >= 0; i--)
            {
                c     = word[i];
                cnode = cnode[c];
                if (cnode != null)
                {
                    if (!cnode.ValueIsNull)
                    {
                        res.Add(new Pair <String, T>(word.Substring(i), cnode.Value));
                    }
                }
                else
                {
                    break;
                }
            }
            return(res);
        }
Esempio n. 6
0
 public SuffixTree()
 {
     root       = new SuffixTreeNode <T>();
     properties = new Dictionary <String, int>(2);
 }