Class TreeUtil

java.lang.Object
cloud.opencode.base.tree.operation.TreeUtil

public final class TreeUtil extends Object
Tree Util 树工具类

Utility methods for tree operations.

树操作的工具方法。

Features | 主要功能:

  • Find nodes by ID or predicate - 通过ID或谓词查找节点
  • Flatten tree to list - 将树扁平化为列表
  • Count nodes and get depth - 统计节点和获取深度
  • Get leaf nodes - 获取叶子节点

Usage Examples | 使用示例:

Optional<MyNode> node = TreeUtil.findById(roots, targetId);
List<MyNode> flat = TreeUtil.flatten(roots);
int count = TreeUtil.count(roots);
int depth = TreeUtil.getMaxDepth(roots);

Security | 安全性:

  • Thread-safe: No - 否
  • Null-safe: No (roots must not be null) - 否(根节点不能为null)

Performance | 性能特性:

  • Time complexity: O(n) - flatten/count/depth visit all nodes; find exits early on match - 时间复杂度: O(n) - flatten/count/depth 遍历全部节点;find 在匹配时提前退出
  • Space complexity: O(n) for flatten result; O(h) recursion stack where h is tree height - 空间复杂度: flatten 结果 O(n);递归栈 O(h),h 为树高
Since:
JDK 25, opencode-base-tree V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • findById

      public static <T extends Treeable<T,ID>, ID> Optional<T> findById(List<T> roots, ID id)
      Find node by ID 通过ID查找节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      id - the ID to find | 要查找的ID
      Returns:
      the found node | 找到的节点
    • find

      public static <T extends Treeable<T,ID>, ID> Optional<T> find(List<T> roots, Predicate<T> predicate)
      Find node by predicate 通过谓词查找节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the predicate | 谓词
      Returns:
      the found node | 找到的节点
    • findAll

      public static <T extends Treeable<T,ID>, ID> List<T> findAll(List<T> roots, Predicate<T> predicate)
      Find all nodes matching predicate 查找所有匹配谓词的节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      predicate - the predicate | 谓词
      Returns:
      the matching nodes | 匹配的节点
    • flatten

      public static <T extends Treeable<T,ID>, ID> List<T> flatten(List<T> roots)
      Flatten tree to list 将树扁平化为列表
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the flattened list | 扁平化的列表
    • count

      public static <T extends Treeable<T,ID>, ID> int count(List<T> roots)
      Count all nodes 统计所有节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the node count | 节点数量
    • getMaxDepth

      public static <T extends Treeable<T,ID>, ID> int getMaxDepth(List<T> roots)
      Get max depth 获取最大深度
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the max depth | 最大深度
    • getLeaves

      public static <T extends Treeable<T,ID>, ID> List<T> getLeaves(List<T> roots)
      Get all leaf nodes 获取所有叶子节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      Returns:
      the leaf nodes | 叶子节点
    • contains

      public static <T extends Treeable<T,ID>, ID> boolean contains(List<T> roots, ID id)
      Check if tree contains node with ID 检查树是否包含指定ID的节点
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      id - the ID to check | 要检查的ID
      Returns:
      true if contains | 如果包含返回true
    • extractSubtree

      public static <T extends Treeable<T,ID>, ID> Optional<T> extractSubtree(List<T> roots, ID id)
      Extract the subtree rooted at the node with the given ID 提取以给定ID节点为根的子树

      Returns the node and its entire subtree. This does NOT clone — it returns the original node reference with its existing children.

      返回该节点及其完整子树。不进行克隆——返回原始节点引用及其现有子节点。

      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      id - the target node ID | 目标节点ID
      Returns:
      the subtree rooted at the node, or empty if not found | 以该节点为根的子树,未找到返回空
      Since:
      V1.0.3
    • getSiblings

      public static <T extends Treeable<T,ID>, ID> List<T> getSiblings(List<T> roots, ID id)
      Get sibling nodes of the node with the given ID 获取给定ID节点的兄弟节点

      Returns other children of the same parent, excluding the target node itself. Returns empty list if the node is a root or not found.

      返回同一父节点的其他子节点,不包括目标节点本身。 如果节点是根节点或未找到则返回空列表。

      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      id - the target node ID | 目标节点ID
      Returns:
      the sibling nodes | 兄弟节点列表
      Since:
      V1.0.3