Class RootedTree<V>

java.lang.Object
org.episteme.core.mathematics.discrete.RootedTree<V>
All Implemented Interfaces:
Serializable, Graph<V>, Tree<V>

public class RootedTree<V> extends Object implements Tree<V>, Serializable
A mutable implementation of a rooted tree.

This implementation stores parent-child relationships efficiently and supports dynamic tree construction. Suitable for:

  • Phylogenetic tree construction (UPGMA, Neighbor-Joining)
  • Decision tree building
  • Hierarchical clustering
  • Parse tree construction

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

    • RootedTree

      public RootedTree()
      Creates an empty tree.
    • RootedTree

      public RootedTree(V root)
      Creates a tree with the given root.
      Parameters:
      root - the root vertex
  • Method Details

    • setRoot

      public void setRoot(V root)
      Sets the root of this tree.
      Parameters:
      root - the new root
    • addChild

      public void addChild(V parent, V child)
      Adds a child to a parent vertex.
      Parameters:
      parent - the parent vertex
      child - the child vertex to add
    • getRoot

      public Optional<V> getRoot()
      Description copied from interface: Tree
      Returns the root of this tree.
      Specified by:
      getRoot in interface Tree<V>
      Returns:
      the root vertex, or empty if the tree is unrooted or empty
    • getParent

      public Optional<V> getParent(V vertex)
      Description copied from interface: Tree
      Returns the parent of the given vertex.
      Specified by:
      getParent in interface Tree<V>
      Parameters:
      vertex - the vertex to find the parent of
      Returns:
      the parent vertex, or empty if vertex is root
    • getChildren

      public List<V> getChildren(V vertex)
      Description copied from interface: Tree
      Returns all children of the given vertex.
      Specified by:
      getChildren in interface Tree<V>
      Parameters:
      vertex - the parent vertex
      Returns:
      list of child vertices (empty if leaf)
    • vertices

      public Set<V> vertices()
      Description copied from interface: Graph
      Returns all vertices in this graph.
      Specified by:
      vertices in interface Graph<V>
      Returns:
      unmodifiable set of vertices
    • edges

      public Set<Graph.Edge<V>> edges()
      Description copied from interface: Graph
      Returns all edges in this graph.
      Specified by:
      edges in interface Graph<V>
      Returns:
      unmodifiable set of edges
    • addVertex

      public boolean addVertex(V vertex)
      Description copied from interface: Graph
      Adds a vertex to the graph.
      Specified by:
      addVertex in interface Graph<V>
      Parameters:
      vertex - the vertex to add
      Returns:
      true if vertex was added, false if already present
    • addEdge

      public boolean addEdge(V source, V target)
      Description copied from interface: Graph
      Adds an edge between two vertices.
      Specified by:
      addEdge in interface Graph<V>
      Parameters:
      source - the source vertex
      target - the target vertex
      Returns:
      true if edge was added
    • neighbors

      public Set<V> neighbors(V vertex)
      Description copied from interface: Graph
      Returns neighbors of a vertex.
      Specified by:
      neighbors in interface Graph<V>
      Parameters:
      vertex - the vertex
      Returns:
      set of adjacent vertices
    • degree

      public int degree(V vertex)
      Description copied from interface: Graph
      Returns the degree (number of edges) of a vertex.
      Specified by:
      degree in interface Graph<V>
      Parameters:
      vertex - the vertex
      Returns:
      degree of vertex
    • vertexCount

      public int vertexCount()
      Description copied from interface: Graph
      Returns the number of vertices in the graph.
      Specified by:
      vertexCount in interface Graph<V>
      Returns:
      the number of vertices
    • edgeCount

      public int edgeCount()
      Returns the number of edges in the tree. For a tree with n vertices, this is always n-1.
      Returns:
      number of edges
    • subtree

      public RootedTree<V> subtree(V vertex)
      Returns the subtree rooted at the given vertex.
      Parameters:
      vertex - the root of the subtree
      Returns:
      a new tree containing the subtree
    • toString

      public String toString()
      Overrides:
      toString in class Object