Interface Tree<V>

All Superinterfaces:
Graph<V>
All Known Implementing Classes:
RootedTree

public interface Tree<V> extends Graph<V>
Represents a tree data structure - a connected acyclic graph.

A tree is a graph where any two vertices are connected by exactly one path.

Since:
1.0
Author:
Silvere Martin-Michiellot, Gemini AI (Google DeepMind)
  • Method Details

    • getRoot

      Optional<V> getRoot()
      Returns the root of this tree.
      Returns:
      the root vertex, or empty if the tree is unrooted or empty
    • getParent

      Optional<V> getParent(V vertex)
      Returns the parent of the given vertex.
      Parameters:
      vertex - the vertex to find the parent of
      Returns:
      the parent vertex, or empty if vertex is root
    • getChildren

      List<V> getChildren(V vertex)
      Returns all children of the given vertex.
      Parameters:
      vertex - the parent vertex
      Returns:
      list of child vertices (empty if leaf)
    • isLeaf

      default boolean isLeaf(V vertex)
      Checks if the given vertex is a leaf (has no children).
      Parameters:
      vertex - the vertex to check
      Returns:
      true if vertex has no children
    • isRoot

      default boolean isRoot(V vertex)
      Checks if the given vertex is the root.
      Parameters:
      vertex - the vertex to check
      Returns:
      true if vertex is the root
    • depth

      default int depth(V vertex)
      Returns the depth of a vertex (distance from root).
      Parameters:
      vertex - the vertex
      Returns:
      depth (0 for root)
    • height

      default int height()
      Returns the height of the tree (maximum depth).
      Returns:
      the height of the tree
    • getLeaves

      default List<V> getLeaves()
      Returns all leaf vertices.
      Returns:
      list of leaf vertices
    • getAncestors

      default List<V> getAncestors(V vertex)
      Returns all ancestors of a vertex (path to root).
      Parameters:
      vertex - the vertex
      Returns:
      list of ancestors from parent to root
    • getDescendants

      default List<V> getDescendants(V vertex)
      Returns all descendants of a vertex.
      Parameters:
      vertex - the vertex
      Returns:
      list of all descendants
    • lowestCommonAncestor

      default Optional<V> lowestCommonAncestor(V v1, V v2)
      Finds the lowest common ancestor of two vertices.
      Parameters:
      v1 - first vertex
      v2 - second vertex
      Returns:
      the LCA, or empty if not found
    • preOrder

      default void preOrder(Consumer<V> visitor)
      Pre-order traversal of the tree.
      Parameters:
      visitor - the visitor to call for each vertex
    • preOrderFrom

      default void preOrderFrom(V vertex, Consumer<V> visitor)
      Pre-order traversal starting from a vertex.
    • postOrder

      default void postOrder(Consumer<V> visitor)
      Post-order traversal of the tree.
      Parameters:
      visitor - the visitor to call for each vertex
    • postOrderFrom

      default void postOrderFrom(V vertex, Consumer<V> visitor)
      Post-order traversal starting from a vertex.
    • isDirected

      default boolean isDirected()
      Returns this tree as a graph (already is a graph).
      Specified by:
      isDirected in interface Graph<V>
      Returns:
      true if directed, false if undirected