Record Class TreeStatistics
java.lang.Object
java.lang.Record
cloud.opencode.base.tree.operation.TreeStatistics
public record TreeStatistics(int nodeCount, int leafCount, int maxDepth, int maxWidth, double avgBranchingFactor, Map<Integer,Integer> widthByLevel)
extends Record
Tree Statistics - Comprehensive tree metrics collected in a single pass
树统计 - 单次遍历收集的全面树指标
Collects comprehensive tree metrics including node count, leaf count, max depth, max width, average branching factor, and width per level using a single BFS traversal.
使用单次 BFS 遍历收集全面的树指标,包括节点数、叶子节点数、 最大深度、最大宽度、平均分支因子和每层宽度。
Features | 主要功能:
- Single-pass BFS collection of all metrics - 单次 BFS 收集所有指标
- Immutable record-based result - 基于不可变 record 的结果
- Derived metrics (internal node count, leaf ratio) - 派生指标
- Human-readable summary - 人类可读的摘要
Usage Examples | 使用示例:
TreeStatistics stats = TreeStatistics.of(roots);
int nodes = stats.nodeCount();
int leaves = stats.leafCount();
int maxDepth = stats.maxDepth();
int maxWidth = stats.maxWidth();
double avgBranching = stats.avgBranchingFactor();
Map<Integer, Integer> widths = stats.widthByLevel();
String summary = stats.summary();
Performance | 性能特性:
- Time complexity: O(n) single BFS pass - 时间复杂度: O(n) 单次 BFS
- Space complexity: O(w) where w is max width - 空间复杂度: O(w),w 为最大宽度
Security | 安全性:
- Thread-safe: Yes (immutable record) - 线程安全: 是(不可变 record)
- Null-safe: No (roots must not be null) - 空值安全: 否(根节点不能为null)
- Since:
- JDK 25, opencode-base-tree V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionTreeStatistics(int nodeCount, int leafCount, int maxDepth, int maxWidth, double avgBranchingFactor, Map<Integer, Integer> widthByLevel) Compact constructor - defensive copy of widthByLevel 紧凑构造器 - widthByLevel 防御性拷贝 -
Method Summary
Modifier and TypeMethodDescriptiondoubleReturns the value of theavgBranchingFactorrecord component.final booleanIndicates whether some other object is "equal to" this one.final inthashCode()Returns a hash code value for this object.intGet internal (non-leaf) node count 获取内部(非叶子)节点数intReturns the value of theleafCountrecord component.doubleGet the ratio of leaf nodes to total nodes 获取叶子节点占总节点的比率intmaxDepth()Returns the value of themaxDepthrecord component.intmaxWidth()Returns the value of themaxWidthrecord component.intReturns the value of thenodeCountrecord component.static <T extends Treeable<T,ID>, ID>
TreeStatisticsCollect statistics from a tree forest in a single BFS pass 通过单次 BFS 遍历从树森林收集统计信息summary()Get a human-readable summary of the statistics 获取人类可读的统计摘要final StringtoString()Returns a string representation of this record class.Returns the value of thewidthByLevelrecord component.
-
Constructor Details
-
TreeStatistics
-
-
Method Details
-
of
Collect statistics from a tree forest in a single BFS pass 通过单次 BFS 遍历从树森林收集统计信息- Type Parameters:
T- the node type | 节点类型ID- the ID type | ID类型- Parameters:
roots- the root nodes | 根节点列表- Returns:
- the statistics | 统计信息
-
internalNodeCount
public int internalNodeCount()Get internal (non-leaf) node count 获取内部(非叶子)节点数- Returns:
- the internal node count | 内部节点数
-
leafRatio
public double leafRatio()Get the ratio of leaf nodes to total nodes 获取叶子节点占总节点的比率- Returns:
- the leaf ratio (0.0 for empty tree) | 叶子比率(空树为0.0)
-
summary
Get a human-readable summary of the statistics 获取人类可读的统计摘要- Returns:
- the summary string | 摘要字符串
-
toString
-
hashCode
-
equals
Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared withObjects::equals(Object,Object); primitive components are compared with thecomparemethod from their corresponding wrapper classes. -
nodeCount
public int nodeCount()Returns the value of thenodeCountrecord component.- Returns:
- the value of the
nodeCountrecord component
-
leafCount
public int leafCount()Returns the value of theleafCountrecord component.- Returns:
- the value of the
leafCountrecord component
-
maxDepth
public int maxDepth()Returns the value of themaxDepthrecord component.- Returns:
- the value of the
maxDepthrecord component
-
maxWidth
public int maxWidth()Returns the value of themaxWidthrecord component.- Returns:
- the value of the
maxWidthrecord component
-
avgBranchingFactor
public double avgBranchingFactor()Returns the value of theavgBranchingFactorrecord component.- Returns:
- the value of the
avgBranchingFactorrecord component
-
widthByLevel
-