public class Tree<T>
extends java.lang.Object
implements java.io.Serializable
Node
object that manages the relationships with other elements.
A tree can have multiple root nodes, thus making it effectively a forrest. Since this denomination is far less known than the term tree, we will stick with the latter term.
| Modifier and Type | Field and Description |
|---|---|
static long |
serialVersionUID |
| Constructor and Description |
|---|
Tree() |
| Modifier and Type | Method and Description |
|---|---|
T |
add(T data)
Adds the given element to the tree.
|
T |
addChild(T parent,
T data)
Adds the a new element as child of the parent element.
|
T |
addChildAfter(T sibling,
T data)
Adds the given element to the parent of the sibling after the sibling.
|
T |
addChildAt(T parent,
int index,
T data)
Adds the given element to the parent at the specified position.
|
T |
addChildBefore(T sibling,
T data)
Adds the given element to the parent of the sibling before the sibling.
|
T |
addParent(T data,
T newParent)
Adds an element as parent of the given element.
|
void |
addSubtree(Tree<T> subtree)
Adds the given tree as subtree starting at the root.
|
void |
addSubtree(T parent,
Tree<T> subtree)
Adds the given tree as subtree starting at the given parent node.
|
static <T> Tree<T> |
asDegeneratedTree(T... l)
Helper method to build a degenerated tree (i.e. a tree with only a single path) from the given list of elements.
|
java.util.List<T> |
asList()
Returns the elements of the tree as pre-ordered list.
|
static <T> Tree<T> |
asTree(T... l)
Helper method to build a tree from the given list of elements.
|
int |
childIndexOf(T data)
Returns the index of the first occurrence of the specified element within the list of children of its parent
node or -1 if this list does not contain the element.
|
void |
clear()
Removes all elements from the tree.
|
T |
closest(T data,
Filter<T> filter)
Returns either the element itself or the closest parent element that satisfies the filter condition.
|
boolean |
contains(T data)
Checks if the tree contains a particular element.
|
Tree<T> |
copy()
Returns a copy of this tree.
|
Tree<T> |
copy(Filter<T> filter)
Returns a copy of this tree that results from applying the
filter(Filter) method after the copy is made. |
Tree<T> |
filter(Filter<T> filter)
Retains all elements from the tree that are accepted by the
Filter. |
Tree<T> |
filter(Filter<T> filter,
boolean retainSubTree)
|
Tree<T> |
filterEager(Filter<T> filter)
Retains all elements from the tree that are accepted by the
Filter. |
T |
find(Filter<T> filter)
Returns the first element that is accepted by the given
Filter. |
java.util.List<T> |
findAll(Filter<T> filter)
Returns a list of elements that are accepted by the given
Filter. |
java.util.Set<T> |
getAll()
Returns a set of all elements of the tree.
|
java.util.List<java.util.List<T>> |
getBranches()
Return a list of all branches of the tree.
|
java.util.Iterator<java.util.List<T>> |
getBranchesIterator() |
java.util.List<T> |
getBreadthFirstList()
Returns the elements of the tree as list using a breath first traversal.
|
java.util.List<T> |
getChildren(T parent)
Returns the list of children of the given element.
|
int |
getDepth()
Returns the depth of the tree.
|
int |
getDepth(T data)
Returns the depth of the element.
|
java.util.List<T> |
getDescendants(T data)
Returns a list of descendants for a given node
|
java.util.List<T> |
getLeafs()
Returns all leaf elements of the tree.
|
java.util.List<T> |
getLevel(int level)
Returns a list of all nodes on the given level.
|
java.util.Set<T> |
getMatchSet(Filter<T> filter)
Returns the elements of the tree accepted by the given filter as set.
|
T |
getParent(T data)
Returns the parent element for the given element.
|
java.util.List<T> |
getPath(T data)
Returns the path (including the element itself) to the given element as list.
|
Tree<T> |
getPathTree(T data)
Returns the path (including the element itself) to the given element as (degenerated) tree.
|
java.util.List<T> |
getPreorderList()
Returns the elements of the tree as pre-ordered list.
|
java.util.List<T> |
getPreorderList(Filter<T> filter)
Returns the elements of the tree accepted by the given filter as pre-ordered list.
|
java.util.List<T> |
getPreorderList(T data)
Returns the elements of the subtree rooted at the given element as pre-ordered list.
|
java.util.List<T> |
getRoots()
Returns all root elements of the tree.
|
java.util.List<T> |
getSiblings(T data)
Returns the list of siblings of the given element.
|
java.util.List<T> |
getSiblings(T data,
boolean includeSelf)
Returns the list of siblings of the given element.
|
Tree<T> |
getSubtree(T data)
Returns the subtree underneath the given element.
|
long |
getVersion()
A tree instance carries a version information which can be used to track changes of the tree.
|
boolean |
hasChildren(T parent)
Tests if the given element has children.
|
int |
indexOf(T data)
Returns the index of the first occurrence of the specified element in this tree or -1 if this list does not
contain the element.
|
boolean |
isAncestor(T data,
T data2)
Checks, if an element is an ancestor of another element.
|
boolean |
isChild(T data,
T data2)
Checks if an element is a child element of another element.
|
boolean |
isDescendant(T data,
T data2)
Checks if an element is a descendant of another element.
|
boolean |
isFirstChild(T data)
Checks if a given element is the first child of its parent node.
|
boolean |
isLastChild(T data)
Checks if a given element is the last child of its parent node.
|
boolean |
isLeaf(T data) |
boolean |
isRoot(T data) |
T |
leastCommonSubsumer(T data1,
T data2)
Returns the least common subsumer for the given elements.
|
void |
limit(int count)
Removes all but the first
count elements of the tree |
<S> Tree<S> |
map(Mapper<T,S> mapper)
|
<S> Tree<S> |
map(T data,
Mapper<T,S> mapper)
Maps the subtree from a given element of generic type
T to a homomorphous tree of type
S using the given Mapper. |
void |
move(T data,
T newParent)
Moves the element to a new parent, removing it from its current parent.
|
void |
prune(T data)
Prunes the tree to the given element.
|
Tree<T> |
pruneCopy(T data)
Copies the tree and prunes it to the given element.
|
boolean |
pull(T data)
Pulls the given element from the tree, i.e. the node is removed from the tree and all children of the node (if
any) are moved to the parent of the element.
|
T |
push(T data)
Adds the given element to the tree.
|
boolean |
remove(Filter<T> f)
Removes all elements that are accepted by the given filter.
|
boolean |
remove(T data)
Removes the given element from the tree.
|
void |
replace(T data,
T replacement)
Replaces the given element with another element.
|
Tree<ItemWrapper<T>> |
reverse() |
void |
shrink(Filter<T> filter)
Retains all elements from the tree that are accepted by the
Filter. |
void |
shrinkSubtree(T data,
Filter<T> filter)
Retains all elements from the given subtree that are accepted by the
Filter. |
int |
size()
Returns the size of the tree (i.e. the number of elements in the tree).
|
void |
sort(java.util.Comparator<T> comparator)
Sorts the tree with the given
Comparator. |
Tree<T> |
stabilize(Filter<T> filter)
Applies the specified filter repeatedly to the tree until no more elements are removed by the filter.
|
java.util.stream.Stream<T> |
stream()
Returns an unordered stream of the elements of the tree.
|
java.lang.String |
toString() |
java.lang.String |
toString(ElementFormatter<T> formatter) |
void |
visit(PrePostVisitor<T> visitor)
Visits the elements of the tree in pre-order, starting at the root nodes.
|
boolean |
visit(T data,
Visitor<T> visitor)
Visits the elements of a subtree in pre-order.
|
void |
visit(Visitor<T> visitor)
Visits the elements of the tree in pre-order, starting at the root nodes.
|
void |
visitDescendants(T data,
Visitor<T> visitor)
Visits the descendants of an element in pre-order.
|
void |
visitSiblings(T data,
Visitor<T> visitor)
Visits the siblings of an element in pre-order.
|
public static final long serialVersionUID
public static <T> Tree<T> asTree(T... l)
T - the target type for the new treel - the list of elementspublic static <T> Tree<T> asDegeneratedTree(T... l)
T - the target type for the new treel - the list of elementspublic T add(T data)
data - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treepublic T addChild(T parent, T data)
parent - the parent elementdata - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treejava.lang.IllegalArgumentException - if the parent element is not found in the treepublic T push(T data)
data - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treepublic T addParent(T data, T newParent)
data - the data elementnewParent - the new parentpublic void addSubtree(Tree<T> subtree)
subtree - the subtree to addpublic void addSubtree(T parent, Tree<T> subtree)
parent - the parent node to add the subtree tosubtree - the subtree to addpublic T addChildAt(T parent, int index, T data)
parent - the sibling elementindex - the insert positiondata - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treejava.lang.IllegalArgumentException - if the parent element is not found in the treejava.lang.IndexOutOfBoundsException - if the position is not validpublic T addChildBefore(T sibling, T data)
sibling - the sibling elementdata - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treejava.lang.IllegalArgumentException - if the sibling element is not found in the treepublic T addChildAfter(T sibling, T data)
sibling - the sibling elementdata - the element to addjava.lang.IllegalArgumentException - if the element has already been added to the treejava.lang.IllegalArgumentException - if the sibling element is not found in the treepublic void move(T data, T newParent)
null,
the element is moved to the root node.data - the element to movenewParent - the new parent for the element or null for the root nodejava.lang.IllegalArgumentException - if the element is not found in the treejava.lang.IllegalArgumentException - if the parent is not found in the treepublic boolean remove(T data)
data - the element to removetrue if the element was been removed, false if the tree did not contain the
elementpublic void replace(T data, T replacement)
data - the element to replacereplacement - the replacementpublic void prune(T data)
data - the element to prune topublic Tree<T> pruneCopy(T data)
data - the element to prune topublic void clear()
public boolean pull(T data)
data - the element to pulltrue if the element was been pulled, false if the tree does not contain the
elementpublic boolean remove(Filter<T> f)
remove(T) method.f - the Filter to applytrue if at least one element has been removed, false otherwisepublic java.util.Set<T> getAll()
public java.util.List<T> getLeafs()
public java.util.List<T> getDescendants(T data)
data - the subtree rootpublic boolean hasChildren(T parent)
parent - the element to testtrue if the given node has children, false otherwisepublic java.util.List<T> getSiblings(T data)
data - the elementjava.lang.IllegalArgumentException - if the element is not found in the treepublic java.util.List<T> getSiblings(T data, boolean includeSelf)
data - the elementincludeSelf - include the element itself in the collection of siblingsjava.lang.IllegalArgumentException - if the element is not found in the treepublic Tree<T> getSubtree(T data)
data - the root of the subtreepublic boolean isFirstChild(T data)
data - the elementtrue if the element is the first child of its parent, false otherwisepublic T getParent(T data)
data - the elementnull if the parent element is a root elementjava.lang.IllegalArgumentException - if the element is not found in the treepublic java.util.List<T> getRoots()
public java.util.List<T> getChildren(T parent)
parent - the parent elementjava.lang.IllegalArgumentException - if the parent element is not found in the treepublic boolean isLastChild(T data)
data - the elementtrue if the element is the last child of its parent, false otherwisepublic int getDepth(T data)
0, children of root nodes have a depth of 1 and so forth.data - the elementjava.lang.IllegalArgumentException - if the element is not found in the treepublic int getDepth()
public Tree<T> getPathTree(T data)
data - the elementjava.lang.IllegalArgumentException - if the element is not found in the treepublic java.util.List<T> getPath(T data)
data - the elementjava.lang.IllegalArgumentException - if the element is not found in the treepublic void visit(Visitor<T> visitor)
visitor - the Visitor to applypublic void visit(PrePostVisitor<T> visitor)
visitor - the Visitor to applypublic boolean visit(T data, Visitor<T> visitor)
data - the root of the subtree to visitvisitor - the Visitor to applytrue if all elements have been visited, false otherwise (i.e. the
Visitor.visit(Object) method returned false for some object)java.lang.IllegalArgumentException - if the element is not found in the treepublic void visitDescendants(T data, Visitor<T> visitor)
Visitor.data - the root of the subtree to visitvisitor - the Visitor to applyjava.lang.IllegalArgumentException - if the element is not found in the treepublic void visitSiblings(T data, Visitor<T> visitor)
Visitor.data - the element which siblings to visitvisitor - the Visitor to applypublic boolean contains(T data)
data - the element to check ontrue if the tree contains the given element, false otherwisepublic boolean isChild(T data, T data2)
data - the element to testdata2 - the parent to check againsttrue if the element is a child element, false otherwisepublic boolean isDescendant(T data, T data2)
data - the element to testdata2 - the predecessor to check againsttrue if the element is a descendant, false otherwisepublic boolean isAncestor(T data, T data2)
data - the ancestor to check againstdata2 - the item to checktrue if the element is an ancestor, false otherwisepublic int size()
public java.util.stream.Stream<T> stream()
public java.util.List<T> asList()
public java.util.List<T> getPreorderList()
public java.util.List<T> getPreorderList(Filter<T> filter)
filter - the Filter to usepublic java.util.List<T> getPreorderList(T data)
data - the subtree rootjava.lang.IllegalArgumentException - if the element is not found in the treepublic java.util.Set<T> getMatchSet(Filter<T> filter)
filter - the Filter to usepublic java.util.List<T> getBreadthFirstList()
public java.util.List<java.util.List<T>> getBranches()
public java.util.Iterator<java.util.List<T>> getBranchesIterator()
public java.util.List<T> getLevel(int level)
level - the target level for the result elementspublic void sort(java.util.Comparator<T> comparator)
Comparator. Sorting the tree is defined as sorting the children of each
element according to the comparator.comparator - the Comparator used to sort the childrenpublic Tree<T> filter(Filter<T> filter)
Filter. If an element is retained, all
elements on the path to a root element are retained as well. The elements that are rejected are removed using the
remove(T) method.filter - the Filter to usepublic Tree<T> filter(Filter<T> filter, boolean retainSubTree)
filter - the Filter to useretainSubTree - retain also the Sub-Tree of a matching element.public Tree<T> filterEager(Filter<T> filter)
Filter. If an element is rejected, all
children are rejected as well. The elements that are rejected are removed using the remove(T) method.filter - the Filter to usepublic Tree<T> stabilize(Filter<T> filter)
filter - the filter to applypublic void shrink(Filter<T> filter)
Filter. The elements that are rejected are
removed using the pull(T) method.filter - the Filter to usepublic void shrinkSubtree(T data, Filter<T> filter)
Filter. The elements that are
rejected are removed using the pull(T) method.data - the subtree rootfilter - the Filter to usepublic void limit(int count)
count elements of the treecount - the number of elements (in pre-order) to keeppublic Tree<T> copy(Filter<T> filter)
filter(Filter) method after the copy is made.filter - the Filter to usepublic <S> Tree<S> map(Mapper<T,S> mapper)
S - the target typemapper - the Mapper to useSpublic <S> Tree<S> map(T data, Mapper<T,S> mapper)
T to a homomorphous tree of type
S using the given Mapper.S - the target typedata - the subtree rootmapper - the Mapper to useSjava.lang.IllegalArgumentException - if the element is not found in the treepublic T find(Filter<T> filter)
Filter.public java.util.List<T> findAll(Filter<T> filter)
Filter.public T closest(T data, Filter<T> filter)
data - the elem to start fromfilter - the Filter to applypublic T leastCommonSubsumer(T data1, T data2)
data1 - the first data elementdata2 - the second data elementpublic int indexOf(T data)
data - the element to look forpublic int childIndexOf(T data)
data - the element to look forpublic Tree<ItemWrapper<T>> reverse()
public boolean isRoot(T data)
data - the element to checktrue if the item is a root node, false otherwisepublic boolean isLeaf(T data)
data - the element to checktrue if the item is a leaf node, false otherwisepublic java.lang.String toString()
toString in class java.lang.Objectpublic java.lang.String toString(ElementFormatter<T> formatter)
public long getVersion()