0
Follow
0
View

How to sort a field data with tree structure in java

edithnith 注册会员
2023-02-28 13:08

Monster Group and GPt-based tuning:
depth-first traversal algorithm that recursively traverses each node and sorts as needed. Here is an example of a recursive depth-first traversal:

public class TreeNode {
    private String id;
    private List<TreeNode> children;

    public TreeNode(String id) {
        this.id = id;
        this.children = new ArrayList<>();
    }

    public String getId() {
        return id;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void addChild(TreeNode child) {
        children.add(child);
    }

    public void sortChildren() {
        Collections.sort(children, Comparator.comparing(TreeNode::getId));
        for (TreeNode child : children) {
            child.sortChildren();
        }
    }
}

public class TreeSorter {
    public static void sort(TreeNode root) {
        root.sortChildren();
    }
}

About the Author

Question Info

Publish Time
2023-02-28 13:08
Update Time
2023-02-28 13:08

Related Question

Powershell - ConvertTo-Json挂起脚本

为什么std::vector在c++中不给出任何输出

求,auto.js报错解决方案

编译选项.注解processorgeneratedsourcesdirectory属性已弃用.这将在Gradle 8.0中被移除

如何仅获取那些记录有catToken=x从火基地颤?

NextJS localStorage.getItem()方法在组件上不工作?

Failed to write core dump. Minidumps are not enabled by default on client versions of Windows

不支持的操作:无穷大或NaN toInt颤振

更改文本日期与VBA TextToColumns

ajax请求头token更新