Class TreeTraverser
java.lang.Object
cloud.opencode.base.tree.TreeTraverser
Tree Traverser - Advanced tree traversal utilities
树遍历器 - 高级树遍历工具
Provides comprehensive traversal operations including controlled traversal, transformation, reduction, and stream-based operations.
提供全面的遍历操作,包括受控遍历、转换、归约和基于流的操作。
Features | 主要功能:
- Controlled traversal (skip, stop) - 受控遍历(跳过、停止)
- Parallel traversal - 并行遍历
- Stream-based operations - 基于流的操作
- Transform/reduce operations - 转换/归约操作
- Iterator support - 迭代器支持
- Ancestor/descendant queries - 祖先/后代查询
Usage Examples | 使用示例:
// Stream all nodes - 流式处理所有节点
TreeTraverser.stream(root)
.filter(node -> node.getData() != null)
.forEach(System.out::println);
// Controlled traversal - 受控遍历
TreeTraverser.traverse(root, node -> {
if (shouldSkip(node)) return TraversalControl.SKIP_SUBTREE;
if (shouldStop(node)) return TraversalControl.STOP;
return TraversalControl.CONTINUE;
});
// Transform tree - 转换树
TreeNode<String> transformed = TreeTraverser.map(root, node -> node.toString());
// Reduce tree - 归约树
int sum = TreeTraverser.reduce(root, 0, (acc, node) -> acc + node.getValue());
Security | 安全性:
- Thread-safe: Yes (stateless utility class) - 线程安全: 是(无状态工具类)
- Null-safe: No (null root will cause NullPointerException) - 空值安全: 否(null根节点会导致空指针异常)
- Since:
- JDK 25, opencode-base-tree V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceControlled traversal visitor 受控遍历访问者static enumTraversal control enum 遍历控制枚举 -
Method Summary
Modifier and TypeMethodDescriptionbreadthFirstIterator(TreeNode<T> root) Creates a breadth-first iterator 创建广度优先迭代器breadthFirstStream(TreeNode<T> root) Creates a breadth-first stream 创建广度优先流static <T> TreeNode<T> findLowestCommonAncestor(TreeNode<T> node1, TreeNode<T> node2) Finds lowest common ancestor 查找最近公共祖先static <T,R> Stream <R> Flat maps tree to stream 扁平映射树到流static <T,R> R foldBottomUp(TreeNode<T> root, Function<T, R> leafMapper, BiFunction<T, List<R>, R> branchFolder) Folds tree bottom-up 自底向上折叠树getAncestors(TreeNode<T> node) Gets ancestors from parent to root 获取从父节点到根节点的祖先static <T> intGets the depth of a node 获取节点深度getDescendants(TreeNode<T> node) Gets all descendants 获取所有后代getSiblings(TreeNode<T> node) Gets all siblings 获取所有兄弟节点static <T,R> TreeNode <R> Maps tree to new data type 将树映射到新数据类型static <T,R> TreeNode <R> Maps tree with node context 带节点上下文的树映射parallelStream(TreeNode<T> root) Creates a parallel stream 创建并行流postOrderIterator(TreeNode<T> root) Creates a post-order iterator 创建后序迭代器postOrderStream(TreeNode<T> root) Creates a post-order stream 创建后序流preOrderIterator(TreeNode<T> root) Creates a pre-order iterator 创建前序迭代器preOrderIterator(List<T> roots) Creates a pre-order iterator for Treeable 创建Treeable的前序迭代器static <T,R> R reduce(TreeNode<T> root, R identity, BiFunction<R, T, R> accumulator) Reduces tree to single value 将树归约为单个值static <T,R> R reduce(TreeNode<T> root, R identity, BiFunction<R, T, R> accumulator, BinaryOperator<R> combiner) Reduces tree with combiner (for parallel) 带组合器的树归约(用于并行)Creates a pre-order stream of TreeNode 创建TreeNode的前序流Creates a pre-order stream of Treeable nodes 创建Treeable节点的前序流static <T> booleantraverse(TreeNode<T> root, TreeTraverser.ControlledVisitor<T> visitor) Traverses tree with control 带控制的树遍历static <T extends Treeable<T,ID>, ID>
booleantraverse(List<T> roots, Function<T, TreeTraverser.TraversalControl> visitor) Traverses Treeable nodes with control 带控制的Treeable节点遍历
-
Method Details
-
traverse
Traverses tree with control 带控制的树遍历- Type Parameters:
T- data type | 数据类型- Parameters:
root- root node | 根节点visitor- visitor with control | 带控制的访问者- Returns:
- true if completed without stop | 如果完成且未停止则为true
-
traverse
public static <T extends Treeable<T,ID>, ID> boolean traverse(List<T> roots, Function<T, TreeTraverser.TraversalControl> visitor) Traverses Treeable nodes with control 带控制的Treeable节点遍历- Type Parameters:
T- node type | 节点类型ID- ID type | ID类型- Parameters:
roots- root nodes | 根节点列表visitor- visitor with control | 带控制的访问者- Returns:
- true if completed without stop | 如果完成且未停止则为true
-
stream
-
stream
-
parallelStream
-
breadthFirstStream
-
postOrderStream
-
preOrderIterator
-
preOrderIterator
-
breadthFirstIterator
-
postOrderIterator
-
map
-
mapNode
-
flatMap
Flat maps tree to stream 扁平映射树到流- Type Parameters:
T- source type | 源类型R- result type | 结果类型- Parameters:
root- root node | 根节点mapper- node to stream mapper | 节点到流映射器- Returns:
- flattened stream | 扁平化流
-
reduce
Reduces tree to single value 将树归约为单个值- Type Parameters:
T- data type | 数据类型R- result type | 结果类型- Parameters:
root- root node | 根节点identity- initial value | 初始值accumulator- accumulator | 累加器- Returns:
- reduced value | 归约值
-
reduce
public static <T,R> R reduce(TreeNode<T> root, R identity, BiFunction<R, T, R> accumulator, BinaryOperator<R> combiner) Reduces tree with combiner (for parallel) 带组合器的树归约(用于并行)- Type Parameters:
T- data type | 数据类型R- result type | 结果类型- Parameters:
root- root node | 根节点identity- initial value | 初始值accumulator- accumulator | 累加器combiner- combiner | 组合器- Returns:
- reduced value | 归约值
-
foldBottomUp
public static <T,R> R foldBottomUp(TreeNode<T> root, Function<T, R> leafMapper, BiFunction<T, List<R>, R> branchFolder) Folds tree bottom-up 自底向上折叠树- Type Parameters:
T- data type | 数据类型R- result type | 结果类型- Parameters:
root- root node | 根节点leafMapper- leaf node mapper | 叶节点映射器branchFolder- branch folder | 分支折叠器- Returns:
- folded value | 折叠值
-
getDescendants
-
getSiblings
-
getAncestors
-
getDepth
Gets the depth of a node 获取节点深度- Type Parameters:
T- data type | 数据类型- Parameters:
node- node | 节点- Returns:
- depth (0 for root) | 深度(根节点为0)
-
findLowestCommonAncestor
-