Class AbstractSuffixTree<E extends SuffixTree.Edge>

  • All Implemented Interfaces:
    SuffixTree
    Direct Known Subclasses:
    CharSequenceSuffixTree

    public abstract class AbstractSuffixTree<E extends SuffixTree.Edge>
    extends java.lang.Object
    implements SuffixTree
    An abstract base implementation of a suffix tree for a sequence of elements (most commonly characters).
    Author:
    Garret Wilson
    • Constructor Detail

      • AbstractSuffixTree

        protected AbstractSuffixTree​(boolean explicit)
        Constructor.
        Parameters:
        explicit - Whether the suffix tree is explicit, with every suffix ending on a leaf node.
    • Method Detail

      • isExplicit

        public boolean isExplicit()
        Specified by:
        isExplicit in interface SuffixTree
        Returns:
        Whether the suffix tree is explicit, with every suffix ending on a leaf node.
      • getNodes

        public java.lang.Iterable<SuffixTree.Node> getNodes()
        Specified by:
        getNodes in interface SuffixTree
        Returns:
        A read-only iterable of the nodes in the tree.
      • getNodeCount

        public int getNodeCount()
        Specified by:
        getNodeCount in interface SuffixTree
        Returns:
        The number of nodes in the suffix tree.
      • getRootNode

        public SuffixTree.Node getRootNode()
        Description copied from interface: SuffixTree
        Retrieves the root node of the tree. This is a convenience method to retrieve the node with index zero.
        Specified by:
        getRootNode in interface SuffixTree
        Returns:
        The identified node.
      • getNode

        public SuffixTree.Node getNode​(int nodeIndex)
        Description copied from interface: SuffixTree
        Retrieves the identified node.
        Specified by:
        getNode in interface SuffixTree
        Parameters:
        nodeIndex - The index of the node to retrieve.
        Returns:
        The identified node.
      • createNode

        protected abstract AbstractSuffixTree.AbstractNode createNode​(int index)
        Creates a new node. By default a node is considered a leaf node.
        Parameters:
        index - The index of the node to create.
        Returns:
        The index of the newly created node.
      • addEdge

        protected final E addEdge​(SuffixTree.Node parentNode,
                                  SuffixTree.Node childNode,
                                  int start,
                                  int end)
        Creates a new edge and adds it to the tree. The given parent node will be set to be a branch node, and the given child node will have its parent node set to the given parent node.

        This method delegates to createEdge(SuffixTree.Node, SuffixTree.Node, int, int).

        Parameters:
        parentNode - The parent node representing the root end of the edge.
        childNode - The child node representing the leaf end of the edge.
        start - The position of the start element, inclusive.
        end - The position of the end element, exclusive.
        Returns:
        The tree after it receives a new edge.
        Throws:
        java.lang.NullPointerException - if the given parent node and/or child node is null.
        java.lang.IllegalArgumentException - if the given end is less than the start.
        java.lang.IllegalStateException - if there already exists an edge with the same parent node and first element.
        java.lang.ClassCastException - if the given parent node is not an AbstractSuffixTree.AbstractNode.
        See Also:
        AbstractSuffixTree.AbstractNode.setParentNode(SuffixTree.Node), AbstractSuffixTree.AbstractNode.setLeaf(boolean)
      • createEdge

        protected abstract E createEdge​(SuffixTree.Node parentNode,
                                        SuffixTree.Node childNode,
                                        int start,
                                        int end)
        Creates a new edge.
        Parameters:
        parentNode - The parent node representing the root end of the edge.
        childNode - The child node representing the leaf end of the edge.
        start - The position of the start element, inclusive.
        end - The position of the end element, exclusive.
        Returns:
        The tree after it receives a new edge.
        Throws:
        java.lang.NullPointerException - if the given parent node and/or child node is null.
        java.lang.IllegalArgumentException - if the given end is less than the start.
        java.lang.IllegalStateException - if there already exists an edge with the same parent node and first element.
      • addEdge

        protected abstract void addEdge​(E edge)
        Adds an edge to the tree.
        Parameters:
        edge - The edge to add.
        Throws:
        java.lang.NullPointerException - if the given edge is null.
        java.lang.IllegalStateException - if there already exists an edge with the same parent node and first element.
      • removeEdge

        protected abstract void removeEdge​(E edge)
        Removes an edge from the tree. If the edge does not exist, no action occurs.
        Parameters:
        edge - The edge to remove.
        Throws:
        java.lang.NullPointerException - if the given edge is null.
      • splitEdge

        protected AbstractSuffixTree.AbstractNode splitEdge​(E edge,
                                                            int length)
        Splits an edge into two. The first, near edge will be of the given length; the second, far edge will be of the remaining length (that is, the length of the original edge minus the given length). A new node will be created as the mid-point between the original edge nodes, becoming the child node of the first edge and the parent node of the second edge.
        Parameters:
        edge - The edge to split.
        length - The position at which to split the edge.
        Returns:
        The created node splitting the edge.