Package com.globalmentor.collections
Class AbstractSuffixTree<E extends SuffixTree.Edge>
- java.lang.Object
-
- com.globalmentor.collections.AbstractSuffixTree<E>
-
- 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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected class
AbstractSuffixTree.AbstractNode
Represents a node in a suffix tree.-
Nested classes/interfaces inherited from interface com.globalmentor.collections.SuffixTree
SuffixTree.Edge, SuffixTree.Node
-
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractSuffixTree(boolean explicit)
Constructor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected E
addEdge(SuffixTree.Node parentNode, SuffixTree.Node childNode, int start, int end)
Creates a new edge and adds it to the tree.protected abstract void
addEdge(E edge)
Adds an edge to the tree.protected AbstractSuffixTree.AbstractNode
addNode()
Creates a new node and adds it to the internal list of nodes.protected abstract E
createEdge(SuffixTree.Node parentNode, SuffixTree.Node childNode, int start, int end)
Creates a new edge.protected abstract AbstractSuffixTree.AbstractNode
createNode(int index)
Creates a new node.SuffixTree.Node
getNode(int nodeIndex)
Retrieves the identified node.int
getNodeCount()
java.lang.Iterable<SuffixTree.Node>
getNodes()
SuffixTree.Node
getRootNode()
Retrieves the root node of the tree.boolean
isExplicit()
protected abstract void
removeEdge(E edge)
Removes an edge from the tree.protected AbstractSuffixTree.AbstractNode
splitEdge(E edge, int length)
Splits an edge into two.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.globalmentor.collections.SuffixTree
getEdges
-
-
-
-
Method Detail
-
isExplicit
public boolean isExplicit()
- Specified by:
isExplicit
in interfaceSuffixTree
- 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 interfaceSuffixTree
- Returns:
- A read-only iterable of the nodes in the tree.
-
getNodeCount
public int getNodeCount()
- Specified by:
getNodeCount
in interfaceSuffixTree
- 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 interfaceSuffixTree
- Returns:
- The identified node.
-
getNode
public SuffixTree.Node getNode(int nodeIndex)
Description copied from interface:SuffixTree
Retrieves the identified node.- Specified by:
getNode
in interfaceSuffixTree
- Parameters:
nodeIndex
- The index of the node to retrieve.- Returns:
- The identified node.
-
addNode
protected final AbstractSuffixTree.AbstractNode addNode()
Creates a new node and adds it to the internal list of nodes. This implementation delegates tocreateNode(int)
.- Returns:
- The newly created 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 isnull
.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 anAbstractSuffixTree.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 isnull
.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 isnull
.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 isnull
.
-
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.
-
-