Class UniTreeNode<T>

java.lang.Object
org.bzdev.util.UniTreeNode<T>
All Implemented Interfaces:
Iterable<T>

public class UniTreeNode<T> extends Object implements Iterable<T>
Class used to construct a tree of elements traversable in one direction. The tree can be traversed only from a leaf or intermediate node towards the root. As a result, the tree is thread safe without the need for locking (a UniTreeNode's fields are modified only in constructors), resulting in a light-weight implementation.

Each leaf's view of the tree is basically a unidirectional link list, but where multiple lists may share common nodes. There is no operation to remove items from the tree: memory will be reclaimed by the garbage collector once references to a a UniTreeNode are dropped.

  • Constructor Details

    • UniTreeNode

      public UniTreeNode(T element)
      Constructor.
      Parameters:
      element - the element associated with a new root node
  • Method Details

    • add

      public UniTreeNode<T> add(T element)
      Add a node to this tree.
      Parameters:
      element - the element to add to this tree
      Returns:
      the new node
    • addTo

      public static <T> UniTreeNode<T> addTo(T element, UniTreeNode<T> tree)
      Add a node to a tree.
      Type Parameters:
      T - the type of the element to add to this tree
      Parameters:
      element - the element to add to this tree
      tree - an existing tree
      Returns:
      the new node
    • getElement

      public T getElement()
      Get the element associated with a node.
      Returns:
      the element associated with this node
    • parent

      public UniTreeNode<T> parent()
      Get the parent of this node.
      Returns:
      the next node
    • iterator

      public Iterator<T> iterator()
      Specified by:
      iterator in interface Iterable<T>
    • spliterator

      public Spliterator<T> spliterator()
      Specified by:
      spliterator in interface Iterable<T>
    • stream

      public Stream<T> stream()
      Return a stream of the elements from this leaf to the root of the tree.
      Returns:
      a stream
    • parallelStream

      public Stream<T> parallelStream()
      Return a parallel stream of the elements from this leaf to the root of the tree.
      Returns:
      a parallel stream