- All Known Implementing Classes:
AbstractTreeGene,FlatTreeNode,ProgramGene,TreeNode
TreeNode class.- Since:
- 3.9
- Version:
- 7.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic final classThis class represents the path to child within a given tree. -
Method Summary
Modifier and TypeMethodDescriptionReturn an iterator that traverses the subtree rooted atthisnode in breadth-first order.Return a stream that traverses the subtree rooted atthisnode in breadth-first order.childAfter(Tree<?, ?> child) Return the child which comes immediately afterthisnode.childAt(int index) Return the child node with the given index.childAtPath(int... path) Return the child node at the givenpath.childAtPath(Tree.Path path) Return the child node at the givenpath.childBefore(Tree<?, ?> child) Return the child which comes immediately beforethisnode.intReturn the number of children this tree node consists of.Return an iterator of the children of thisTreenode.default Tree.PathReturn the path ofthischild node from the root node.Return a forward-order stream of this node's children.default intdepth()Returns the depth of the tree rooted at this node.Return an iterator that traverses the subtree rooted atthisnode in depth-first order.Return a stream that traverses the subtree rooted atthisnode in depth-first.static booleanChecks if the two given trees has the same structure with the same values.Return the first child ofthisnode, orOptional.empty()ifthisnode has no children.default TReturn the first leaf that is a descendant ofthisnode; either this node or its first child's first leaf.static intCalculates the hash code of the given tree.default booleanTests whetherthisnode is the same as theothernode.default intReturns the index of the specified child in this node's child array, or-1ifthisnode doesn't contain the givenchild.default booleanisAncestor(Tree<?, ?> node) Returntrueif the givennodeis an ancestor ofthisnode.default booleanReturntrueif the givennodeis a child ofthisnode.default booleanisDescendant(Tree<?, ?> node) Returntrueif the givennodeis a descendant ofthisnode.default booleanisEmpty()A tree is considered empty if it'svalue()isnulland has no children and parent.default booleanisLeaf()Returntrueifthisnode has no children.default booleanReturns true if and only if the givennodeis in the same tree asthisnode.default booleanisRoot()Returnstrueif this node is the root of the tree.default booleanTest if the givennodeis a sibling ofthisnode.iterator()Return an iterator that traverses the subtree rooted atthis.Return the last child ofthisnode, orOptional.empty()ifthisnode has no children.default TlastLeaf()Return the last leaf that is a descendant of this node; eitherthisnode or its last child's last leaf.default intReturns the total number of leaves that are descendants of this node.leaves()Return a stream of leaves that are descendants of this node.default intlevel()Returns the number of levels above this node.nextLeaf()Returns the leaf afterthisnode orOptional.empty()if this node is the last leaf in the tree.nextNode()Return the node that followsthisnode in a pre-order traversal ofthistree node.Return the next sibling ofthisnode in the parent's children array, ornullifthisnode has no parent, or it is the last child of the paren.parent()Return the parent node of this tree node.default Tree.Pathpath()Return theTree.Pathofthistree, such thatfinal Tree<Integer, ?> tree = ...; final Tree.Path path = tree.path(); assert tree == tree.getRoot() .childAtPath(path) .orElse(null);Returns the path from the root, to get to this node.pathFromAncestorIterator(Tree<?, ?> ancestor) Return an iterator that follows the path fromancestortothisnode.Return an iterator that traverses the subtree rooted atthisnode in post-order.Return a stream that traverses the subtree rooted atthisnode in post-order.Return an iterator that traverses the subtree rooted atthisnode in pre-order.Return a stream that traverses the subtree rooted atthisnode in pre-order.Return the leaf beforethisnode ornullifthisnode is the first leaf in the tree.Return the node that precedes this node in a pre-order traversal ofthistree node.Return the previous sibling ofthisnode in the parent's children list, orOptional.empty()if this node has no parent or is the parent's first child.default <U> Ureduce(U[] neutral, BiFunction<? super V, ? super U[], ? extends U> reducer) Performs a reduction on the elements ofthistree, using an associative reduction function.default Troot()Returns the root of the tree that contains this node.sharedAncestor(T node) Returns the nearest common ancestor to this node and the givennode.default intReturn the number of siblings ofthisnode.default intsize()Return the number of nodes ofthisnode (subtree).stream()Return a stream that traverses the subtree rooted atthisnode in breadth-first order.default StringReturn a compact string representation of the given tree.default StringtoParenthesesString(Function<? super V, String> mapper) Return a compact string representation of the given tree.static StringReturn a string representation of the given tree, like the following example.value()Return the value of the currentTreenode.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
value
Return the value of the currentTreenode. The value may benull.- Returns:
- the value of the current
Treenode
-
parent
Return the parent node of this tree node.- Returns:
- the parent node, or
Optional.empty()if this node is the root of the tree
-
childAt
Return the child node with the given index.- Parameters:
index- the child index- Returns:
- the child node with the given index
- Throws:
IndexOutOfBoundsException- if theindexis out of bounds ([0, childCount()))
-
childCount
int childCount()Return the number of children this tree node consists of.- Returns:
- the number of children this tree node consists of
-
childIterator
Return an iterator of the children of thisTreenode.- Returns:
- an iterator of the children of this
Treenode.
-
childStream
Return a forward-order stream of this node's children.- Returns:
- a stream of children of
thisnode
-
isRoot
Returnstrueif this node is the root of the tree.- Returns:
trueif this node is the root of its tree,falseotherwise
-
depth
Returns the depth of the tree rooted at this node. The depth of a tree is the longest distance fromthisnode to a leaf. Ifthisnode has no children, 0 is returned. This operation is much more expensive thanlevel()because it must effectively traverse the entire tree rooted atthisnode.- Returns:
- the depth of the tree whose root is this node
-
level
Returns the number of levels above this node. The level of a tree is the distance from the root tothisnode. Ifthisnode is the root, returns 0.- Returns:
- the number of levels above this node
-
indexOf
Returns the index of the specified child in this node's child array, or-1ifthisnode doesn't contain the givenchild. This method performs a linear search and is O(n) wherenis the number of children.- Parameters:
child- the TreeNode to search for among this node's children- Returns:
- the index of the node in this node's child array, or
-1if the node could not be found - Throws:
NullPointerException- if the givenchildisnull
-
size
Return the number of nodes ofthisnode (subtree).- Returns:
- the number of nodes of
thisnode (subtree)
-
isEmpty
A tree is considered empty if it'svalue()isnulland has no children and parent. A newly created tree node with no value is empty.final Tree<String, ?> tree = TreeNode.of(); assert tree.isEmpty();- Returns:
trueifthistree is empty,falseotherwise- Since:
- 7.0
-
childAtPath
Return the child node at the givenpath. A path consists of the child index at a give level, starting with level 1. (The root note has level zero.)tree.childAtPath(Path.of(2))will return the third child node ofthisnode, if it exists andtree.childAtPath(Path.of(2, 0))will return the first child of the third child ofthis node.- Parameters:
path- the child path- Returns:
- the child node at the given
path - Throws:
NullPointerException- if the givenpatharray isnull- Since:
- 4.4
- See Also:
-
childAtPath
Return the child node at the givenpath. A path consists of the child index at a give level, starting with level 1. (The root note has level zero.)tree.childAtPath(2)will return the third child node ofthisnode, if it exists andtree.childAtPath(2, 0)will return the first child of the third child ofthis node.- Parameters:
path- the child path- Returns:
- the child node at the given
path - Throws:
NullPointerException- if the givenpatharray isnullIllegalArgumentException- if one of the path elements is smaller than zero- Since:
- 4.3
- See Also:
-
isAncestor
Returntrueif the givennodeis an ancestor ofthisnode. This operation is at worstO(h)wherehis the distance from the root tothisnode.- Parameters:
node- the node to test- Returns:
trueif the givennodeis an ancestor ofthisnode,falseotherwise- Throws:
NullPointerException- if the givennodeisnull
-
isDescendant
Returntrueif the givennodeis a descendant ofthisnode. If the givennodeisnull,falseis returned. This operation is at worstO(h)wherehis the distance from the root tothisnode.- Parameters:
node- the node to test as descendant of this node- Returns:
trueif this node is an ancestor of the givennode- Throws:
NullPointerException- if the givennodeisnull
-
isRelated
Returns true if and only if the givennodeis in the same tree asthisnode.- Parameters:
node- the other node to check- Returns:
- true if the given
nodeis in the same tree asthisnode,falseotherwise. - Throws:
NullPointerException- if the givennodeisnull
-
pathElements
Returns the path from the root, to get to this node. The last element in the path is this node.- Returns:
- an array of TreeNode objects giving the path, where the first element in the path is the root and the last element is this node.
- Since:
- 5.1
-
path
Return theTree.Pathofthistree, such thatfinal Tree<Integer, ?> tree = ...; final Tree.Path path = tree.path(); assert tree == tree.getRoot() .childAtPath(path) .orElse(null);- Returns:
- the path from the root element to
thisnode. - Since:
- 5.1
-
root
Returns the root of the tree that contains this node. The root is the ancestor with no parent.- Returns:
- the root of the tree that contains this node
-
isChild
Returntrueif the givennodeis a child ofthisnode.- Parameters:
node- the other node to check- Returns:
trueifnodeis a child,falseotherwise- Throws:
NullPointerException- if the givennodeisnull
-
firstChild
Return the first child ofthisnode, orOptional.empty()ifthisnode has no children.- Returns:
- the first child of this node
-
lastChild
Return the last child ofthisnode, orOptional.empty()ifthisnode has no children.- Returns:
- the last child of this node
-
childAfter
Return the child which comes immediately afterthisnode. This method performs a linear search of this node's children forchildand isO(n)where n is the number of children.- Parameters:
child- the child node- Returns:
- the child of this node that immediately follows the
child, orOptional.empty()if the givennodeis the first node. - Throws:
NullPointerException- if the givenchildisnull
-
childBefore
Return the child which comes immediately beforethisnode. This method performs a linear search of this node's children forchildand isO(n)where n is the number of children.- Parameters:
child- the child node- Returns:
- the child of this node that immediately precedes the
child, ornullif the givennodeis the first node. - Throws:
NullPointerException- if the givenchildisnull
-
nextNode
Return the node that followsthisnode in a pre-order traversal ofthistree node. ReturnOptional.empty()if this node is the last node of the traversal. This is an inefficient way to traverse the entire tree use an iterator instead.- Returns:
- the node that follows this node in a pre-order traversal, or
Optional.empty()if this node is last - See Also:
-
previousNode
Return the node that precedes this node in a pre-order traversal ofthistree node. ReturnsOptional.empty()if this node is the first node of the traversal, the root of the tree. This is an inefficient way to traverse the entire tree; use an iterator instead.- Returns:
- the node that precedes this node in a pre-order traversal, or
Optional.empty()if this node is the first - See Also:
-
isSibling
Test if the givennodeis a sibling ofthisnode.- Parameters:
node- node to test as sibling of this node- Returns:
trueif thenodeis a sibling ofthisnode- Throws:
NullPointerException- if the givennodeisnull
-
siblingCount
Return the number of siblings ofthisnode. A node is its own sibling (if it has no parent or no siblings, this method returns1).- Returns:
- the number of siblings of
thisnode
-
nextSibling
Return the next sibling ofthisnode in the parent's children array, ornullifthisnode has no parent, or it is the last child of the paren. This method performs a linear search that isO(n)where n is the number of children; to traverse the entire array, use the iterator of the parent instead.- Returns:
- the sibling of
thisnode that immediately followsthisnode - See Also:
-
previousSibling
Return the previous sibling ofthisnode in the parent's children list, orOptional.empty()if this node has no parent or is the parent's first child. This method performs a linear search that is O(n) where n is the number of children.- Returns:
- the sibling of
thisnode that immediately precedes this node
-
isLeaf
Returntrueifthisnode has no children.- Returns:
trueifthisnode has no children,falseotherwise
-
firstLeaf
Return the first leaf that is a descendant ofthisnode; either this node or its first child's first leaf.thisnode is returned if it is a leaf.- Returns:
- the first leaf in the subtree rooted at this node
- See Also:
-
lastLeaf
Return the last leaf that is a descendant of this node; eitherthisnode or its last child's last leaf. Returnsthisnode if it is a leaf.- Returns:
- the last leaf in this subtree
- See Also:
-
nextLeaf
Returns the leaf afterthisnode orOptional.empty()if this node is the last leaf in the tree.In order to determine the next node, this method first performs a linear search in the parent's child-list in order to find the current node.
That implementation makes the operation suitable for short traversals from a known position. But to traverse all the leaves in the tree, you should use
depthFirstIterator()to iterator the nodes in the tree and useisLeaf()on each node to determine which are leaves.- Returns:
- return the next leaf past this node
- See Also:
-
previousLeaf
Return the leaf beforethisnode ornullifthisnode is the first leaf in the tree.In order to determine the previous node, this method first performs a linear search in the parent's child-list in order to find the current node.
That implementation makes the operation suitable for short traversals from a known position. But to traverse all the leaves in the tree, you should use
depthFirstIterator()to iterate the nodes in the tree and useisLeaf()on each node to determine which are leaves.- Returns:
- returns the leaf before
thisnode - See Also:
-
leafCount
Returns the total number of leaves that are descendants of this node. If this node is a leaf, returns1. This method isO(n), where n is the number of descendants ofthisnode.- Returns:
- the number of leaves beneath this node
- See Also:
-
leaves
Return a stream of leaves that are descendants of this node.- Returns:
- a stream of leaves that are descendants of this node
- Since:
- 7.0
-
breadthFirstIterator
Return an iterator that traverses the subtree rooted atthisnode in breadth-first order. The first node returned by the iterator isthisnode.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Returns:
- an iterator for traversing the tree in breadth-first order
- See Also:
-
iterator
Return an iterator that traverses the subtree rooted atthis. The first node returned by the iterator isthisnode.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
-
breadthFirstStream
Return a stream that traverses the subtree rooted atthisnode in breadth-first order. The first node returned by the stream isthisnode.- Returns:
- a stream for traversing the tree in breadth-first order
- See Also:
-
stream
Return a stream that traverses the subtree rooted atthisnode in breadth-first order. The first node returned by the stream isthisnode.- Returns:
- a stream for traversing the tree in breadth-first order
- See Also:
-
preorderIterator
Return an iterator that traverses the subtree rooted atthisnode in pre-order. The first node returned by the iterator isthisnode.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Returns:
- an iterator for traversing the tree in pre-order
- See Also:
-
preorderStream
Return a stream that traverses the subtree rooted atthisnode in pre-order. The first node returned by the stream isthisnode.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Returns:
- a stream for traversing the tree in pre-order
- See Also:
-
postorderIterator
Return an iterator that traverses the subtree rooted atthisnode in post-order. The first node returned by the iterator is the leftmost leaf. This is the same as a depth-first traversal.- Returns:
- an iterator for traversing the tree in post-order
- See Also:
-
postorderStream
Return a stream that traverses the subtree rooted atthisnode in post-order. The first node returned by the iterator is the leftmost leaf. This is the same as a depth-first traversal.- Returns:
- a stream for traversing the tree in post-order
- See Also:
-
depthFirstIterator
Return an iterator that traverses the subtree rooted atthisnode in depth-first order. The first node returned by the iterator is the leftmost leaf. This is the same as a postorder traversal.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Returns:
- an iterator for traversing the tree in depth-first order
- See Also:
-
depthFirstStream
Return a stream that traverses the subtree rooted atthisnode in depth-first. The first node returned by the iterator is the leftmost leaf. This is the same as a post-order traversal.- Returns:
- a stream for traversing the tree in post-order
- See Also:
-
pathFromAncestorIterator
Return an iterator that follows the path fromancestortothisnode. The iterator returnancestoras a first element, The creation of the iterator is O(m), where m is the number of nodes betweenthisnode and theancestor, inclusive.Modifying the tree by inserting, removing, or moving a node invalidates any iterator created before the modification.
- Parameters:
ancestor- the ancestor node- Returns:
- an iterator for following the path from an ancestor of
thisnode to this one - Throws:
IllegalArgumentException- if theancestoris not an ancestor of this nodeNullPointerException- if the givenancestorisnull- See Also:
-
childPath
Return the path ofthischild node from the root node. You will getthisnode, if you callchildAtPath(Path)on the root node ofthisnode.final Tree<?, ?> node = ...; final Tree<?, ?> root = node.getRoot(); final int[] path = node.childPath(); assert node == root.childAtPath(path);- Returns:
- the path of
thischild node from the root node. - Since:
- 4.4
- See Also:
-
identical
Tests whetherthisnode is the same as theothernode. The default implementation returns the object identity,this == other, of the two objects, but other implementations may use different criteria for checking the identity.- Parameters:
other- theothernode- Returns:
trueif theothernode is the same asthisnode.
-
reduce
Performs a reduction on the elements ofthistree, using an associative reduction function. This can be used for evaluating a given expression tree in pre-order.final Tree<String, ?> formula = TreeNode.parse("add(sub(6,div(230,10)),mul(5,6))"); final double result = formula.reduce(new Double[0], (op, args) -> switch (op) { case "add" -> args[0] + args[1]; case "sub" -> args[0] - args[1]; case "mul" -> args[0] * args[1]; case "div" -> args[0] / args[1]; default -> Double.parseDouble(op); } ); assert result == 13.0;- Type Parameters:
U- the result type- Parameters:
neutral- the neutral element of the reduction. In most cases this will benew U[0].reducer- the reduce function- Returns:
- the result of the reduction, or
nullifthistree is empty (isEmpty() == true) - Since:
- 7.1
-
toParenthesesString
Return a compact string representation of the given tree. The treemul ├── div │ ├── cos │ │ └── 1.0 │ └── cos │ └── π └── sin └── mul ├── 1.0 └── zis printed asmul(div(cos(1.0),cos(π)),sin(mul(1.0,z)))
- Parameters:
mapper- themapperwhich converts the tree value to a string- Returns:
- the string representation of the given tree
- Since:
- 4.3
- See Also:
-
toParenthesesString
Return a compact string representation of the given tree. The treemul ├── div │ ├── cos │ │ └── 1.0 │ └── cos │ └── π └── sin └── mul ├── 1.0 └── zis printed asmul(div(cos(1.0), cos(π)), sin(mul(1.0, z)))
- Returns:
- the string representation of the given tree
- Throws:
NullPointerException- if themapperisnull- Since:
- 4.3
- See Also:
-
hashCode
Calculates the hash code of the given tree.- Parameters:
tree- the tree where the hash is calculated from- Returns:
- the hash code of the tree
- Throws:
NullPointerException- if the giventreeisnull
-
equals
Checks if the two given trees has the same structure with the same values.- Parameters:
a- the first treeb- the second tree- Returns:
trueif the two given trees are structurally equals,falseotherwise
-
toString
Return a string representation of the given tree, like the following example.mul(div(cos(1.0), cos(π)), sin(mul(1.0, z)))
This method is intended to be used when override theObject.toString()method.- Parameters:
tree- the input tree- Returns:
- the string representation of the given tree
-