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.
Calls to methods of this class are forwarded to corresponding methods of the TreeTraversal class along with
functions that map a node to its parent or to a list of its children, as required.
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends ITreeNode<T>,U>
TfindNode(T root, Iterable<U> path, BiPredicate<T, U> matcher) Searches a tree ofITreeNodes, starting from the specified 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) Returnstrueif the specified node is an ancestor of the specified target node.static <T extends ITreeNode<T>>
booleanisAncestor(T node, T target, boolean testForIdentity) Returnstrueif 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 ofITreeNodes 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 ofITreeNodes 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 ofITreeNodes, 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 ofITreeNodes, 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) Returnstrueif and only if the specifiedITreeNodeor one of its ancestors satisfies the specified test.treeToString(T root, int indentIncrement, Function<T, String> converter) Returns a string representation of the tree ofITreeNodes whose root is the specified node.static <T extends ITreeNode<T>>
booleanvisitAscending(T startNode, Function<T, Boolean> action) Ascends a tree ofITreeNodes 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 ofITreeNodes 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 ofITreeNodes, 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 ofITreeNodes, 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. The root node is assumed to be the first ancestor of the specified node whose parent isnull.- 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
nodebelongs.
-
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
Returnstrueif 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:
trueifnodeis an ancestor oftarget.
-
isAncestor
public static <T extends ITreeNode<T>> boolean isAncestor(T node, T target, boolean testForIdentity) Returnstrueif 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 oftargetor (iftestForIdentityistrue) is potentially identical totarget.target- the node that is a potential descendant ofnodeor (iftestForIdentityistrue) is potentially identical tonode.testForIdentity- iftrue,nodeandtargetwill be tested for identity as well as for an ancestor–descendant relationship.- Returns:
trueifnodeis the ancestor oftarget, ortestForIdentityistrueandnodeis 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 belowrootis desired.- Returns:
- the depth of
nodebelowroot, or -1 ifrootis 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
nodefrom 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
roottonode, or, ifrootis not an ancestor ofnode, the path tonodefrom 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
nodefrom 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
includeRootistrue, or -
the ancestor of the target node that is a child of the root of the tree to which the target node belongs, if
includeRootisfalse.
- 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 ofrootwill 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
roottonode. - 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 ofITreeNodes, starting from the specified 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 which the search will start.path- a path descriptor whose elements are tested sequentially againstrootand its descendants withmatcher.matcher- the matcher that tests a node against an element ofpath.- Returns:
- the node that matches
path, ornullif 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 ofITreeNodes, 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 ofITreeNodes, each of which will haveactionapplied to it.preorder- iftrue, each node will be visited before its descendants; otherwise, each node will be visited after its descendants.includeRoot- iftrue,rootwill haveactionapplied 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:
falseif the traversal of the tree was terminated byaction, possibly before all nodes were visited;trueotherwise.
-
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 ofITreeNodes, 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 ofITreeNodes, each of which will haveactionapplied to it.includeRoot- iftrue,rootwill haveactionapplied 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:
falseif the traversal of the tree was terminated byaction, possibly before all nodes were visited;trueotherwise.
-
visitAscending
public static <T extends ITreeNode<T>> boolean visitAscending(T startNode, Function<T, Boolean> action) Ascends a tree ofITreeNodes 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:
falseif the ascent of the tree was terminated byaction, possibly before all nodes were visited;trueotherwise.
-
visitAscending
public static <T extends ITreeNode<T>> boolean visitAscending(T startNode, T endNode, boolean includeEnd, Function<T, Boolean> action) Ascends a tree ofITreeNodes 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.endNodeis visited ifincludeEndistrue. IfendNodeisnull,startNodeand all its ancestors will be visited.includeEnd- iftrue,endNodewill haveactionapplied 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:
falseif the ascent of the tree was terminated byaction, possibly before all nodes were visited;trueotherwise.
-
testAscending
Returnstrueif and only if the specifiedITreeNodeor 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 tostartNodeand its ancestors.- Returns:
trueif and only ifstartNodeor 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 ofITreeNodes, 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,rootwill 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
truewhentestis applied to it, ornullif 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 ofITreeNodes, 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,rootwill 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
truewhentestis applied to it, ornullif no node satisfies the test.
-
searchAscending
Ascends a tree ofITreeNodes 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
truewhentestis applied to it, ornullif 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 ofITreeNodes 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.endNodeis included in the search ifincludeEndistrue. IfendNodeisnull,startNodeand all its ancestors will be included in the search.includeEnd- iftrue,endNodewill havetestapplied 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
truewhentestis applied to it, ornullif 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 ofITreeNodes 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.
-