Class TreeSorter

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

public final class TreeSorter extends Object
Tree Sorter - Recursively sort children at every level 树排序器 - 递归排序每一层子节点

Recursively sorts children at every level of a tree in-place.

递归地对树的每一层子节点进行原地排序。

Features | 主要功能:

  • Sort by custom comparator - 按自定义比较器排序
  • Sort by extracted comparable key - 按提取的可比较键排序
  • Reversed sort order - 反向排序
  • Check if tree is sorted at all levels - 检查树在所有层级是否已排序
  • Max depth protection (1000) - 最大深度保护(1000)

Usage Examples | 使用示例:

// Sort by name - 按名称排序
TreeSorter.sort(roots, Comparator.comparing(Node::getName));

// Sort by extracted key - 按提取键排序
TreeSorter.sortBy(roots, Node::getOrder);

// Sort reversed - 反向排序
TreeSorter.sortReversed(roots, Comparator.comparing(Node::getName));

// Check if sorted - 检查是否已排序
boolean sorted = TreeSorter.isSorted(roots, Comparator.comparing(Node::getName));

Performance | 性能特性:

  • Time complexity: O(n log k) where k is average children count - 时间复杂度: O(n log k)
  • Space complexity: O(n) for iterative stack - 空间复杂度: O(n) 迭代栈

Security | 安全性:

  • Thread-safe: Yes (stateless utility class) - 是(无状态工具类)
  • Null-safe: No (roots and comparator must not be null) - 否(根节点和比较器不能为null)
  • Depth-safe: Max depth 1000 to prevent stack overflow - 最大深度1000防止栈溢出
Since:
JDK 25, opencode-base-tree V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • sort

      public static <T extends Treeable<T,ID>, ID> void sort(List<T> roots, Comparator<T> comparator)
      Sort children in-place recursively at every level using the given comparator. 使用给定的比较器递归地对每一层的子节点进行原地排序。

      Uses an iterative depth-tracking approach with a Deque to avoid stack overflow.

      使用基于Deque的迭代深度跟踪方式以避免栈溢出。

      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      comparator - the comparator for ordering | 排序比较器
      Throws:
      NullPointerException - if roots or comparator is null | 如果根节点或比较器为null
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000
    • sortBy

      public static <T extends Treeable<T,ID>, ID, U extends Comparable<? super U>> void sortBy(List<T> roots, Function<T,U> keyExtractor)
      Sort children in-place recursively by an extracted comparable key. 按提取的可比较键递归地对子节点进行原地排序。
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      U - the comparable key type | 可比较键类型
      Parameters:
      roots - the root nodes | 根节点列表
      keyExtractor - the function to extract the sort key | 提取排序键的函数
      Throws:
      NullPointerException - if roots or keyExtractor is null | 如果根节点或键提取器为null
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000
    • sortReversed

      public static <T extends Treeable<T,ID>, ID> void sortReversed(List<T> roots, Comparator<T> comparator)
      Sort children in-place recursively in reversed order. 按反向顺序递归地对子节点进行原地排序。
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      comparator - the comparator for ordering (will be reversed) | 排序比较器(将被反转)
      Throws:
      NullPointerException - if roots or comparator is null | 如果根节点或比较器为null
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000
    • isSorted

      public static <T extends Treeable<T,ID>, ID> boolean isSorted(List<T> roots, Comparator<T> comparator)
      Check if the tree is sorted at all levels according to the given comparator. 检查树在所有层级是否按给定的比较器排序。
      Type Parameters:
      T - the node type | 节点类型
      ID - the ID type | ID类型
      Parameters:
      roots - the root nodes | 根节点列表
      comparator - the comparator to check ordering against | 用于检查顺序的比较器
      Returns:
      true if all levels are sorted, false otherwise | 如果所有层级已排序返回true,否则返回false
      Throws:
      NullPointerException - if roots or comparator is null | 如果根节点或比较器为null
      TreeException - if tree depth exceeds 1000 | 如果树深度超过1000