Class Tree<T>

java.lang.Object
dev.roanh.gmark.util.Tree<T>
Type Parameters:
T - The store data type.
All Implemented Interfaces:
Iterable<Tree<T>>

public class Tree<T> extends Object implements Iterable<Tree<T>>
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

    Nested Classes
    Modifier and Type
    Class
    Description
    static interface 
    Interface for tree traversal implementations.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private List<Tree<T>>
    A list of child nodes for this tree node.
    private T
    The data stored at this tree node.
    private Tree<T>
    The parent node for this tree node.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Tree(T data)
    Constructs a new tree node with the given data to store.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addChild(Tree<T> node)
    Adds a new child node to this tree node.
    <N> Tree<N>
    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
    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.
    Gets the data that is stored at this tree node.
    int
    Gets the depth this node is at in the tree.
    Gets the parent node for this tree node.
    boolean
    Checks if this tree node is a leaf node.
    boolean
    Checks if this node is the root node of the tree.
    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

      private List<Tree<T>> children
      A list of child nodes for this tree node.
    • parent

      private Tree<T> parent
      The parent node for this tree node.
    • data

      private T data
      The data stored at this tree node.
  • Constructor Details

    • Tree

      public Tree(T data)
      Constructs a new tree node with the given data to store.
      Parameters:
      data - The data to store at this node.
  • Method Details

    • forEach

      public 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.
      Parameters:
      nodeVisitor - The consumer to pass nodes to.
      Returns:
      True if the search was interrupted early.
    • forEachBottomUp

      public 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. 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

      public T getData()
      Gets the data that is stored at this tree node.
      Returns:
      The data stored at this tree node.
    • addChild

      public void addChild(Tree<T> node) throws IllegalArgumentException
      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

      public List<Tree<T>> 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

      public Stream<Tree<T>> 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

      public Tree<T> 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

      public <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.
      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

      public Iterator<Tree<T>> iterator()

      Note that nodes are iterated in such a way that all children of a node are always returned before their parent.

      Specified by:
      iterator in interface Iterable<T>