Package uk.blankaspect.common.tree
Class TreeUtils
java.lang.Object
uk.blankaspect.common.tree.TreeUtils
This class contains utility methods that relate to trees whose nodes implement the
ITreeNode
interface.-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ITreeNode<T>,
U>
TfindNode
(T root, Iterable<U> path, BiPredicate<T, U> matcher) Searches a tree ofITreeNode
s, starting from the specified root node, for a node whose path from the root matches the specified path descriptor.static <T extends ITreeNode<T>>
intgetDepth
(T node) Returns the depth of the specified node (ie, the number of levels below the root node of the tree to which the specified node belongs).static <T extends ITreeNode<T>>
intgetDepth
(T root, T node) Returns the number of levels of the specified node below the specified root node.getIndices
(T node, int baseIndex) Returns a list of indices to the specified node from the root of the tree to which it belongs.getIndices
(T root, T node, boolean includeRoot, int baseIndex) Returns a list of indices of the path from the specified root node to the specified node, which is assumed to be a descendant of the specified root.getPath
(T node) Returns the path to the specified node from the root of the tree to which it belongs.getPath
(T root, T node) Returns the path from the specified root node to the specified node, which is assumed to be a descendant of the specified root.static <T extends ITreeNode<T>>
TgetRoot
(T node) Returns the root node of the tree to which the specified node belongs.getSiblings
(T node) Returns a list of the siblings of the specified node.static <T extends ITreeNode<T>>
booleanisAncestor
(T node, T target) Returnstrue
if the specified node is an ancestor of the specified target node.static <T extends ITreeNode<T>>
booleanisAncestor
(T node, T target, boolean testForIdentity) Returnstrue
if the specified node is an ancestor of the specified target node or, optionally, if the node is identical to the target.static <T extends ITreeNode<T>>
TsearchAscending
(T startNode, Predicate<T> test) Ascends a tree ofITreeNode
s from the specified start node to the root node, applying the specified test to each node that is visited until a node is found that satisfies the test.static <T extends ITreeNode<T>>
TsearchAscending
(T startNode, T endNode, boolean includeEnd, Predicate<T> test) Ascends a tree ofITreeNode
s from the specified start node to the specified end node, applying the specified test to each node that is visited until a node is found that satisfies the test.static <T extends ITreeNode<T>>
TsearchBreadthFirst
(T root, boolean includeRoot, Predicate<T> test) Performs a breadth-first search of a tree ofITreeNode
s, starting from the specified root node and applying the specified test to each node that is visited until a node is found that satisfies the test.static <T extends ITreeNode<T>>
TsearchDepthFirst
(T root, boolean preorder, boolean includeRoot, Predicate<T> test) Performs a depth-first search of a tree ofITreeNode
s, starting from the specified root node and applying the specified test to each node that is visited until a node is found that satisfies the test.static <T extends ITreeNode<T>>
booleantestAscending
(T startNode, Predicate<T> test) Returnstrue
if and only if the specifiedITreeNode
or one of its ancestors satisfies the specified test.treeToString
(T root, int indentIncrement, Function<T, String> converter) Returns a string representation of the tree ofITreeNode
s whose root is the specified node.static <T extends ITreeNode<T>>
booleanvisitAscending
(T startNode, Function<T, Boolean> action) Ascends a tree ofITreeNode
s from the specified node to the root node, applying the specified action to each node that is visited, including the root.static <T extends ITreeNode<T>>
booleanvisitAscending
(T startNode, T endNode, boolean includeEnd, Function<T, Boolean> action) Ascends a tree ofITreeNode
s from the specified node to the specified end node, applying the specified action to each node that is visited.static <T extends ITreeNode<T>>
booleanvisitBreadthFirst
(T root, boolean includeRoot, Function<T, Boolean> action) Performs a breadth-first traversal of a tree ofITreeNode
s, starting from the specified root node and applying the specified action to each node that is visited.static <T extends ITreeNode<T>>
booleanvisitDepthFirst
(T root, boolean preorder, boolean includeRoot, Function<T, Boolean> action) Performs a depth-first traversal of a tree ofITreeNode
s, starting from the specified root node and applying the specified action to each node that is visited.
-
Method Details
-
getRoot
Returns the root node of the tree to which the specified node belongs.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node whose root ancestor is desired.- Returns:
- the root node of the tree to which
node
belongs.
-
getSiblings
Returns a list of the siblings of the specified node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node whose siblings are desired.- Returns:
- a list of the siblings of
node
.
-
isAncestor
Returnstrue
if the specified node is an ancestor of the specified target node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node that is a potential ancestor oftarget
.target
- the node that is a potential descendant ofnode
.- Returns:
true
ifnode
is an ancestor oftarget
.
-
isAncestor
public static <T extends ITreeNode<T>> boolean isAncestor(T node, T target, boolean testForIdentity) Returnstrue
if the specified node is an ancestor of the specified target node or, optionally, if the node is identical to the target.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node that is a potential ancestor oftarget
or (iftestForIdentity
istrue
) is potentially identical totarget
.target
- the node that is a potential descendant ofnode
or (iftestForIdentity
istrue
) is potentially identical tonode
.testForIdentity
- iftrue
,node
andtarget
will be tested for identity as well as for an ancestor–descendant relationship.- Returns:
true
ifnode
is the ancestor oftarget
, ortestForIdentity
istrue
andnode
is identical totarget
.
-
getDepth
Returns the depth of the specified node (ie, the number of levels below the root node of the tree to which the specified node belongs).- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node whose depth is desired.- Returns:
- the depth of
node
. - See Also:
-
getDepth
Returns the number of levels of the specified node below the specified root node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the root of the tree node whose depth is desired.node
- the node whose depth belowroot
is desired.- Returns:
- the depth of
node
belowroot
, or -1 ifroot
is not an ancestor ofnode
. - See Also:
-
getPath
Returns the path to the specified node from the root of the tree to which it belongs. The path is a list of nodes that includes the root and the target node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node whose path is desired.- Returns:
- a list of nodes that denotes the path to
node
from the root of the tree to which it belongs. - See Also:
-
getPath
Returns the path from the specified root node to the specified node, which is assumed to be a descendant of the specified root. The path is a list of nodes that includes the root and the target node.If the specified root node is not an ancestor of the target node, the path will be a list of nodes from the root of the tree to which the target node belongs, which will be the first element of the list.
- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at which the path will start, unless it is not an ancestor ofnode
.node
- the node whose path is desired.- Returns:
- a list of nodes that denotes the path from
root
tonode
, or, ifroot
is not an ancestor ofnode
, the path tonode
from the root of the tree to which it belongs. - See Also:
-
getIndices
Returns a list of indices to the specified node from the root of the tree to which it belongs. Each element of the list is the sum of the specified base index and the index of the corresponding node in its parent's list of children, or -1 if the node is not a child of its parent. The first element of the list is a child of the root node. The list is empty if the target node is a root node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
node
- the node whose indices are desired.baseIndex
- the base index that is added to each valid index in the list of indices.- Returns:
- a list of nodes that denotes the path to
node
from the root of the tree to which it belongs. - See Also:
-
getIndices
public static <T extends ITreeNode<T>> List<Integer> getIndices(T root, T node, boolean includeRoot, int baseIndex) Returns a list of indices of the path from the specified root node to the specified node, which is assumed to be a descendant of the specified root. Each element of the list is the sum of the specified base index and the index of the corresponding node in its parent's list of children, or -1 if the node has no parent or is not a child of its parent. The list of indices may optionally include the index of the root node.If the specified root node is not an ancestor of the target node, the list of indices will start at
- the root of the tree to which the target node belongs, if
includeRoot
istrue
, or - the ancestor of the target node that is a child of the root of the tree to which the target node belongs,
if
includeRoot
isfalse
.
- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at which the indices will start, unless it is not an ancestor ofnode
.node
- the node whose indices are desired.includeRoot
- the index ofroot
will be included in the list of indices.baseIndex
- the base index that is added to each valid index in the list of indices.- Returns:
- a list of nodes that denotes the path from
root
tonode
. - See Also:
- the root of the tree to which the target node belongs, if
-
findNode
public static <T extends ITreeNode<T>,U> T findNode(T root, Iterable<U> path, BiPredicate<T, U> matcher) Searches a tree ofITreeNode
s, starting from the specified root node, for a node whose path from the root matches the specified path descriptor. The first element of the path descriptor is tested against the root node with the specified matcher: if there is no match, the search terminates immediately; otherwise, the root node becomes the current node, and the search proceeds by descending the tree, testing successive elements against the children of the current node until either no children match or the last element of the path descriptor is matched.- Type Parameters:
T
- the type of the nodes of the tree.U
- the type of the elements of the path.- Parameters:
root
- the node at the root of a tree ofITreeNode
s at which the search will start.path
- a path descriptor whose elements are tested sequentially againstroot
and its descendants withmatcher
.matcher
- the matcher that tests a node against an element ofpath
.- Returns:
- the node that matches
path
, ornull
if no matching node is found.
-
visitDepthFirst
public static <T extends ITreeNode<T>> boolean visitDepthFirst(T root, boolean preorder, boolean includeRoot, Function<T, Boolean> action) Performs a depth-first traversal of a tree ofITreeNode
s, starting from the specified root node and applying the specified action to each node that is visited. The root node may optionally be visited.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at the root of a tree ofITreeNode
s, each of which will haveaction
applied to it.preorder
- iftrue
, each node will be visited before its descendants; otherwise, each node will be visited after its descendants.includeRoot
- iftrue
,root
will haveaction
applied to it.action
- the action that will be applied to each node of the tree that is visited. If the action returnsfalse
, the traversal of the tree will be terminated.- Returns:
false
if the traversal of the tree was terminated byaction
, possibly before all nodes were visited;true
otherwise.
-
visitBreadthFirst
public static <T extends ITreeNode<T>> boolean visitBreadthFirst(T root, boolean includeRoot, Function<T, Boolean> action) Performs a breadth-first traversal of a tree ofITreeNode
s, starting from the specified root node and applying the specified action to each node that is visited. The root node may optionally be visited.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at the root of a tree ofITreeNode
s, each of which will haveaction
applied to it.includeRoot
- iftrue
,root
will haveaction
applied to it.action
- the action that will be applied to each node of the tree that is visited. If the action returnsfalse
, the traversal of the tree will be terminated.- Returns:
false
if the traversal of the tree was terminated byaction
, possibly before all nodes were visited;true
otherwise.
-
visitAscending
public static <T extends ITreeNode<T>> boolean visitAscending(T startNode, Function<T, Boolean> action) Ascends a tree ofITreeNode
s from the specified node to the root node, applying the specified action to each node that is visited, including the root.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
startNode
- the node from which the ascent will start.action
- the action that will be applied to each node of the tree that is visited. If the action returnsfalse
, the ascent of the tree will be terminated.- Returns:
false
if the ascent of the tree was terminated byaction
, possibly before all nodes were visited;true
otherwise.
-
visitAscending
public static <T extends ITreeNode<T>> boolean visitAscending(T startNode, T endNode, boolean includeEnd, Function<T, Boolean> action) Ascends a tree ofITreeNode
s from the specified node to the specified end node, applying the specified action to each node that is visited. The end node may optionally be visited.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
startNode
- the node from which the ascent will start.endNode
- the node at which the ascent will end.endNode
is visited ifincludeEnd
istrue
. IfendNode
isnull
,startNode
and all its ancestors will be visited.includeEnd
- iftrue
,endNode
will haveaction
applied to it.action
- the action that will be applied to each node of the tree that is visited. If the action returnsfalse
, the ascent of the tree will be terminated.- Returns:
false
if the ascent of the tree was terminated byaction
, possibly before all nodes were visited;true
otherwise.
-
testAscending
Returnstrue
if and only if the specifiedITreeNode
or one of its ancestors satisfies the specified test.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
startNode
- the node from which the ascent will start.test
- the test that will be applied tostartNode
and its ancestors.- Returns:
true
if and only ifstartNode
or one of its ancestors satisfiestest
.
-
searchDepthFirst
public static <T extends ITreeNode<T>> T searchDepthFirst(T root, boolean preorder, boolean includeRoot, Predicate<T> test) Performs a depth-first search of a tree ofITreeNode
s, starting from the specified root node and applying the specified test to each node that is visited until a node is found that satisfies the test. If such a node is found, the search terminates immediately and the matching node is returned. The root node may optionally be included in the search.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at which the search will start.preorder
- iftrue
, each node will be visited before its descendants; otherwise, each node will be visited after its descendants.includeRoot
- iftrue
,root
will be included in the search.test
- the test that will be applied to each node of the tree that is visited until the test succeeds.- Returns:
- the first node that returns
true
whentest
is applied to it, ornull
if no node satisfies the test.
-
searchBreadthFirst
public static <T extends ITreeNode<T>> T searchBreadthFirst(T root, boolean includeRoot, Predicate<T> test) Performs a breadth-first search of a tree ofITreeNode
s, starting from the specified root node and applying the specified test to each node that is visited until a node is found that satisfies the test. If such a node is found, the search terminates immediately and the matching node is returned. The root node may optionally be included in the search.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at which the search will start.includeRoot
- iftrue
,root
will be included in the search.test
- the test that will be applied to each node of the tree that is visited until the test succeeds.- Returns:
- the first node that returns
true
whentest
is applied to it, ornull
if no node satisfies the test.
-
searchAscending
Ascends a tree ofITreeNode
s from the specified start node to the root node, applying the specified test to each node that is visited until a node is found that satisfies the test. If such a node is found, the search terminates immediately and the matching node is returned. The root is included in the search.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
startNode
- the node from which the search will start.test
- the test that will be applied to each node of the tree that is visited until the test succeeds.- Returns:
- the first node that returns
true
whentest
is applied to it, ornull
if no node satisfies the test.
-
searchAscending
public static <T extends ITreeNode<T>> T searchAscending(T startNode, T endNode, boolean includeEnd, Predicate<T> test) Ascends a tree ofITreeNode
s from the specified start node to the specified end node, applying the specified test to each node that is visited until a node is found that satisfies the test. If such a node is found, the search terminates immediately and the matching node is returned. The end node may optionally be included in the search.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
startNode
- the node from which the search will start.endNode
- the node at which the search will end.endNode
is included in the search ifincludeEnd
istrue
. IfendNode
isnull
,startNode
and all its ancestors will be included in the search.includeEnd
- iftrue
,endNode
will havetest
applied to it.test
- the test that will be applied to each node of the tree that is visited until the test succeeds.- Returns:
- the first node that returns
true
whentest
is applied to it, ornull
if no node satisfies the test.
-
treeToString
public static <T extends ITreeNode<T>> String treeToString(T root, int indentIncrement, Function<T, String> converter) Returns a string representation of the tree ofITreeNode
s whose root is the specified node.- Type Parameters:
T
- the type of the nodes of the tree.- Parameters:
root
- the node at the root of the tree.indentIncrement
- the number of spaces by which the indent of a line of text will be incremented for each level of the tree belowroot
.converter
- the function that will convert each node to its string representation.- Returns:
- a string representation of the tree whose root is
root
.
-