Interface Tree<V>
- All Superinterfaces:
Graph<V>
- All Known Implementing Classes:
RootedTree
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)
-
Nested Class Summary
Nested classes/interfaces inherited from interface Graph
Graph.Edge<V> -
Method Summary
Modifier and TypeMethodDescriptiondefault intReturns the depth of a vertex (distance from root).getAncestors(V vertex) Returns all ancestors of a vertex (path to root).getChildren(V vertex) Returns all children of the given vertex.getDescendants(V vertex) Returns all descendants of a vertex.Returns all leaf vertices.Returns the parent of the given vertex.getRoot()Returns the root of this tree.default intheight()Returns the height of the tree (maximum depth).default booleanReturns this tree as a graph (already is a graph).default booleanChecks if the given vertex is a leaf (has no children).default booleanChecks if the given vertex is the root.lowestCommonAncestor(V v1, V v2) Finds the lowest common ancestor of two vertices.default voidPost-order traversal of the tree.default voidpostOrderFrom(V vertex, Consumer<V> visitor) Post-order traversal starting from a vertex.default voidPre-order traversal of the tree.default voidpreOrderFrom(V vertex, Consumer<V> visitor) Pre-order traversal starting from a vertex.
-
Method Details
-
getRoot
-
getParent
-
getChildren
-
isLeaf
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
Checks if the given vertex is the root.- Parameters:
vertex- the vertex to check- Returns:
- true if vertex is the root
-
depth
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
-
getAncestors
-
getDescendants
-
lowestCommonAncestor
-
preOrder
-
preOrderFrom
-
postOrder
-
postOrderFrom
-
isDirected
default boolean isDirected()Returns this tree as a graph (already is a graph).- Specified by:
isDirectedin interfaceGraph<V>- Returns:
- true if directed, false if undirected
-