Interface TreeNode<T extends TreeNode<T>>

Type Parameters:
T - the type of the tree node
All Known Implementing Classes:
SimpleTreeNode

public interface TreeNode<T extends TreeNode<T>>
Represents a node of a tree structure.

A node has zero or one parents, and zero or more children.

All nodes in the same tree structure are of the same type <T>.

A node may support only a restricted set of parent types <P extends T>.

A node may only support a restricted set of child types <C extends T>.

Usage:

     public class MyTree implements TreeNode<MyTree> {
        private @Nullable MyTree parent;
        private ChildList<MyTree> children=new ChildList<>(this);

        @Override
        public @Nullable MyTree getParent() { return parent; }

        @Override
        public void setParent(@Nullable MyTree p) { this.parent = p; }

        @Override
        public ObservableList<MyTree> getChildren() { return children; }
     }
 
Author:
Werner Randelshofer
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Interface
    Description
    static class 
    Ancestor iterator.
  • Method Summary

    Modifier and Type
    Method
    Description
    default Iterable<T>
    Returns an iterable which can iterate through this figure and all its ancesters up to the root.
    default Iterable<T>
    Returns an iterable which can iterate through this figure and all its descendants in breadth first sequence.
    default Iterable<T>
    Returns an iterable which can iterate through this figure and all its descendants in depth first sequence.
    default void
    Dumps the figure and its descendants to system.out.
    default void
    dumpTree(Appendable out, int depth)
    Dumps the figure and its descendants.
    default <TT> @Nullable TT
    getAncestor(Class<TT> ancestorType)
    Returns the nearest ancestor of the specified type.
    default T
    getChild(int index)
    Gets the child with the specified index from the node.
    Returns the children of the tree node.
    default int
    Returns the depth of this node.
    default @Nullable T
    Gets the first child.
    default @Nullable T
    Gets the last child.
    default int
    Gets the maximal depth of the sub-tree starting at this tree node.
    @Nullable T
    Returns the parent of the tree node.
    default List<T>
    Returns the path to this node.
    default boolean
    isSuitableChild(T newChild)
    This method returns whether the provided node is a suitable child for this node.
    default boolean
    isSuitableParent(T newParent)
    This method returns whether the provided node is a suitable parent for this node.
    default Iterable<T>
    Returns an iterable which can iterate through this figure and all its descendants in postorder sequence.
    default Iterable<T>
    Returns an iterable which can iterate through this figure and all its descendants in preorder sequence.
    default Spliterator<T>
    Returns a spliterator which can iterate through this figure and all its descendants in preorder sequence.
    void
    setParent(@Nullable T newValue)
    Sets the parent of the tree.
  • Method Details

    • ancestorIterable

      default Iterable<T> ancestorIterable()
      Returns an iterable which can iterate through this figure and all its ancesters up to the root.
      Returns:
      the iterable
    • breadthFirstIterable

      default Iterable<T> breadthFirstIterable()
      Returns an iterable which can iterate through this figure and all its descendants in breadth first sequence.
      Returns:
      the iterable
    • dumpTree

      default void dumpTree()
      Dumps the figure and its descendants to system.out.
    • dumpTree

      default void dumpTree(Appendable out, int depth) throws IOException
      Dumps the figure and its descendants.
      Parameters:
      out - an output stream
      depth - the indentation depth
      Throws:
      IOException - from appendable
    • getAncestor

      default <TT> @Nullable TT getAncestor(Class<TT> ancestorType)
      Returns the nearest ancestor of the specified type.
      Type Parameters:
      TT - The ancestor type
      Parameters:
      ancestorType - The ancestor type
      Returns:
      Nearest ancestor of type <T> or null if no ancestor of this type is present. Returns this if this object is of type <T>.
    • getChild

      default T getChild(int index)
      Gets the child with the specified index from the node.
      Parameters:
      index - the index
      Returns:
      the child
    • getChildren

      List<T> getChildren()
      Returns the children of the tree node.

      In order to keep the tree structure consistent, implementations of this interface must implement the following behavior:

      • A child can only be added if the child is a suitable child for this node, and this node is a suitable parent for the child. See isSuitableChild(TreeNode), isSuitableParent(TreeNode).
      • If a child is added to this list, then this tree node removes it from its former parent, and this tree node then sets itself as the parent of the* child.
      • If a child is removed from this tree node, then this tree node sets the parent of the child to null.
      Returns:
      the children
    • getFirstChild

      default @Nullable T getFirstChild()
      Gets the first child.
      Returns:
      The first child. Returns null if the figure has no getChildren.
    • getLastChild

      default @Nullable T getLastChild()
      Gets the last child.
      Returns:
      The last child. Returns null if the figure has no getChildren.
    • getParent

      @Nullable T getParent()
      Returns the parent of the tree node.

      Note that - by convention - the parent property is changed only by a parent tree node.

      Returns:
      the parent. Returns null if the tree node has no parent.
    • setParent

      void setParent(@Nullable T newValue)
      Sets the parent of the tree.

      Note that - by convention - the parent property is changed only by a parent tree node.

      Parameters:
      newValue - the new parent
    • getPath

      default List<T> getPath()
      Returns the path to this node.
      Returns:
      path including this node
    • getDepth

      default int getDepth()
      Returns the depth of this node.
      Returns:
      depth (0 if the node is the root)
    • postorderIterable

      default Iterable<T> postorderIterable()
      Returns an iterable which can iterate through this figure and all its descendants in postorder sequence.
      Returns:
      the iterable
    • depthFirstIterable

      default Iterable<T> depthFirstIterable()
      Returns an iterable which can iterate through this figure and all its descendants in depth first sequence.
      Returns:
      the iterable
    • preorderIterable

      default Iterable<T> preorderIterable()
      Returns an iterable which can iterate through this figure and all its descendants in preorder sequence.
      Returns:
      the iterable
    • preorderSpliterator

      default Spliterator<T> preorderSpliterator()
      Returns a spliterator which can iterate through this figure and all its descendants in preorder sequence.
      Returns:
      the iterable
    • getMaxDepth

      default int getMaxDepth()
      Gets the maximal depth of the sub-tree starting at this tree node.
      Returns:
      the maximal depth
    • isSuitableParent

      default boolean isSuitableParent(T newParent)
      This method returns whether the provided node is a suitable parent for this node.

      The default implementation returns always true.

      Parameters:
      newParent - The new parent node.
      Returns:
      true if newParent is an acceptable parent
    • isSuitableChild

      default boolean isSuitableChild(T newChild)
      This method returns whether the provided node is a suitable child for this node.

      The default implementation returns always true.

      Parameters:
      newChild - The new child node.
      Returns:
      true if newChild is an acceptable child