Package dev.roanh.gmark.util
Class Tree<T>
java.lang.Object
dev.roanh.gmark.util.Tree<T>
- Type Parameters:
T
- The store data type.
Implementation of a tree data structure where data can
be stored at each node in the tree and each tree node
can have any number of child nodes.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Interface for tree traversal implementations. -
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Adds a new child node to this tree node.<N> Tree<N>
cloneStructure
(Function<T, N> map) Constructs a structurally identical copy of this tree using the given function to set the data stored at each tree node.boolean
forEach
(Tree.TreeVisitor<T> nodeVisitor) Invokes the given consumer for all nodes in this tree that are in the sub tree rooted at this tree node.boolean
forEachBottomUp
(Tree.TreeVisitor<T> nodeVisitor) Invokes the given consumer for all nodes in this tree that are in the sub tree rooted at this tree node.Gets a list of child nodes for this tree node.getData()
Gets the data that is stored at this tree node.int
getDepth()
Gets the depth this node is at in the tree.Gets the parent node for this tree node.boolean
isLeaf()
Checks if this tree node is a leaf node.boolean
isRoot()
Checks if this node is the root node of the tree.iterator()
stream()
Returns a stream over all tree nodes in the sub tree rooted at this tree node.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
children
A list of child nodes for this tree node. -
parent
The parent node for this tree node. -
data
The data stored at this tree node.
-
-
Constructor Details
-
Tree
Constructs a new tree node with the given data to store.- Parameters:
data
- The data to store at this node.
-
-
Method Details
-
forEach
Invokes the given consumer for all nodes in this tree that are in the sub tree rooted at this tree node.- Parameters:
nodeVisitor
- The consumer to pass nodes to.- Returns:
- True if the search was interrupted early.
-
forEachBottomUp
Invokes the given consumer for all nodes in this tree that are in the sub tree rooted at this tree node. It will be guaranteed that all the child nodes will be visited before their parent.- Parameters:
nodeVisitor
- The consumer to pass nodes to.- Returns:
- True if the search was interrupted early.
-
getData
Gets the data that is stored at this tree node.- Returns:
- The data stored at this tree node.
-
addChild
Adds a new child node to this tree node.- Parameters:
node
- The node to add as a child.- Throws:
IllegalArgumentException
- When the given node already has a parent node.
-
getChildren
Gets a list of child nodes for this tree node.- Returns:
- The child nodes of this tree node.
-
getDepth
public int getDepth()Gets the depth this node is at in the tree. Where a depth of 0 indicates the root node of the tree.- Returns:
- The depth of this node in the tree.
-
stream
Returns a stream over all tree nodes in the sub tree rooted at this tree node.- Returns:
- A stream over all nodes in the sub tree rooted at this tree node.
-
isRoot
public boolean isRoot()Checks if this node is the root node of the tree.- Returns:
- True if this node is the root node of the tree.
-
getParent
Gets the parent node for this tree node.- Returns:
- The parent node for this tree node or
null
if this node is the root node.
-
isLeaf
public boolean isLeaf()Checks if this tree node is a leaf node.- Returns:
- True if this node is a leaf node.
-
cloneStructure
Constructs a structurally identical copy of this tree using the given function to set the data stored at each tree node.- Type Parameters:
N
- The data type for the new tree.- Parameters:
map
- The function to use to convert the data stored in this tree to the data to store at the new tree.- Returns:
- The newly created tree.
-
iterator
Note that nodes are iterated in such a way that all children of a node are always returned before their parent.
-