Exemplo n.º 1
0
        /// <summary>
        /// Add suffix to the tree, the loop inside will break under two conditions
        ///     1. If you have reached the leaf node
        ///     2. If you have reached end of suffix
        /// </summary>
        /// <param name="startIndices">List of index of the first character of suffix</param>
        /// <returns>Suffix tree</returns>
        private IMultiWaySuffixTree AppendSuffix(IList <int> startIndices)
        {
            IMultiWaySuffixTree tree = CreateSuffixTree();

            // Loop through subset of sequence string and build the suffix tree
            foreach (int index in startIndices)
            {
                int   startIndex     = index;
                IEdge parentEdge     = tree.Root;
                IEdge edge           = null;
                bool  continueInsert = true;

                do
                {
                    edge = tree.Find(parentEdge, GetReferenceSymbol(startIndex));

                    if (null == edge)
                    {
                        tree.Insert(parentEdge, startIndex, ReferenceLength - 1);
                        continueInsert = false;
                        break;
                    }
                    else
                    {
                        startIndex++;

                        if (edge.StartIndex < edge.EndIndex)
                        {
                            for (int counter = edge.StartIndex + 1; counter <= edge.EndIndex; counter++)
                            {
                                if (GetReferenceSymbol(startIndex) != GetReferenceSymbol(counter))
                                {
                                    parentEdge = tree.Split(edge, counter - 1);

                                    // Add the leaf edge
                                    tree.Insert(parentEdge, startIndex, ReferenceLength - 1);
                                    continueInsert = false;
                                    break;
                                }

                                startIndex++;
                            }
                        }

                        parentEdge = edge;
                    }
                } while (startIndex < ReferenceLength && continueInsert);
            }

            return(tree);
        }