Class TreeUtil
java.lang.Object
cloud.opencode.base.tree.operation.TreeUtil
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 Summary
Modifier and TypeMethodDescriptionstatic <T extends Treeable<T,ID>, ID>
booleanCheck if tree contains node with ID 检查树是否包含指定ID的节点static <T extends Treeable<T,ID>, ID>
intCount all nodes 统计所有节点Find node by predicate 通过谓词查找节点Find all nodes matching predicate 查找所有匹配谓词的节点Find node by ID 通过ID查找节点Flatten tree to list 将树扁平化为列表Get all leaf nodes 获取所有叶子节点static <T extends Treeable<T,ID>, ID>
intgetMaxDepth(List<T> roots) Get max depth 获取最大深度
-
Method Details
-
findById
-
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
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
-
count
-
getMaxDepth
-
getLeaves
-
contains
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
-