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

    Constructors
    Constructor
    Description
    TreeStatistics(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 Type
    Method
    Description
    double
    Returns the value of the avgBranchingFactor record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    int
    Get internal (non-leaf) node count 获取内部(非叶子)节点数
    int
    Returns the value of the leafCount record component.
    double
    Get the ratio of leaf nodes to total nodes 获取叶子节点占总节点的比率
    int
    Returns the value of the maxDepth record component.
    int
    Returns the value of the maxWidth record component.
    int
    Returns the value of the nodeCount record component.
    static <T extends Treeable<T,ID>, ID>
    TreeStatistics
    of(List<T> roots)
    Collect statistics from a tree forest in a single BFS pass 通过单次 BFS 遍历从树森林收集统计信息
    Get a human-readable summary of the statistics 获取人类可读的统计摘要
    final String
    Returns a string representation of this record class.
    Returns the value of the widthByLevel record component.

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • TreeStatistics

      public TreeStatistics(int nodeCount, int leafCount, int maxDepth, int maxWidth, double avgBranchingFactor, Map<Integer,Integer> widthByLevel)
      Compact constructor - defensive copy of widthByLevel 紧凑构造器 - widthByLevel 防御性拷贝
  • Method Details

    • of

      public static <T extends Treeable<T,ID>, ID> TreeStatistics of(List<T> roots)
      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

      public String summary()
      Get a human-readable summary of the statistics 获取人类可读的统计摘要
      Returns:
      the summary string | 摘要字符串
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      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 with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • nodeCount

      public int nodeCount()
      Returns the value of the nodeCount record component.
      Returns:
      the value of the nodeCount record component
    • leafCount

      public int leafCount()
      Returns the value of the leafCount record component.
      Returns:
      the value of the leafCount record component
    • maxDepth

      public int maxDepth()
      Returns the value of the maxDepth record component.
      Returns:
      the value of the maxDepth record component
    • maxWidth

      public int maxWidth()
      Returns the value of the maxWidth record component.
      Returns:
      the value of the maxWidth record component
    • avgBranchingFactor

      public double avgBranchingFactor()
      Returns the value of the avgBranchingFactor record component.
      Returns:
      the value of the avgBranchingFactor record component
    • widthByLevel

      public Map<Integer,Integer> widthByLevel()
      Returns the value of the widthByLevel record component.
      Returns:
      the value of the widthByLevel record component