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 统计所有节点extractSubtree(List<T> roots, ID id) Extract the subtree rooted at the node with the given ID 提取以给定ID节点为根的子树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 获取最大深度getSiblings(List<T> roots, ID id) Get sibling nodes of the node with the given ID 获取给定ID节点的兄弟节点
-
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
-
extractSubtree
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
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
-