java 从树中获取每个路径列表

fquxozlt  于 2023-04-04  发布在  Java
关注(0)|答案(1)|浏览(177)

你好我有个东西

public class Child {
    private String id;
    private List<Child> children;
}

下面是一个JSON格式的例子:

{
    "id": "1",
    "children": [
        {
            "id": "1_1",
            "children": [
                {
                    "id": "1_1_1"
                },{
                    "id": "1_1_2",
                    "children": [
                        {
                            "id": "1_1_2_1"
                        }
                    ]
                }
            ]
        },{
            "id": "1_2",
            "children": [
                {
                    "id": "1_2_1",
                    "children": [
                        {
                            "id": "1_2_1_1"
                        }
                    ]
                }
            ]
        }
    ]
}

我试图得到每个路径的列表,但找不到方法
我想获取CustomChild列表
CustomChild为:

public class CustomChild {
    private String id;
    private CustomChild child;
}

最后,列表应该看起来像这样:每个元素都是树的路径

[
    {
        "id":"1",
        "child": {
            "id": "1_1",
            "child": {
                "id": "1_1_1"
            }
        }
    },
    {
        "id":"1",
        "child": {
            "id": "1_1",
            "child": {
                "id": "1_1_2",
                "child": {
                    "id": "1_1_2_1"
                }
            }
        }
    },
    {
        "id":"1",
        "child": {
            "id": "1_2",
            "child": {
                "id": "1_2_1",
                "child": {
                    "id": "1_2_1_1"
                }
            }
        }
    }
]

我使用递归尝试了不同的选项,但没有找到实现这一点的方法
非常感谢你抽出时间😄

vm0i2vca

vm0i2vca1#

这里有一个可能的解决方案,我给你的两个类和print方法添加了构造函数,以便快速了解嵌套结构包含什么。

public class Main {
    static private class Child {
        private String id;
        private List<Child> children;

        Child(String id, Child... children) {
            this.id = id;
            this.children = new ArrayList<Child>();
            for (Child child : children)
                this.children.add(child);
        }
        
        void print() {
            print("");
        }
        
        void print(String tab) {
            System.out.println(tab + id);
            for (Child child : children) child.print(tab + "  ");
        }
    }

    private static class CustomChild {
        private String id;
        private CustomChild child;

        CustomChild(String id, CustomChild child) {
            this.id = id;
            this.child = child;
        }

        void print() {
            print("");
        }
        
        void print(String tab) {
            System.out.println(tab + id);
            if (child != null) child.print(tab + "  ");
        }
    }

    private static List<CustomChild> convert(Child node) {
        List<CustomChild> result = new ArrayList<>();
        if (node.children.isEmpty()) {
            result.add(new CustomChild(node.id, null));
        }
        for (Child child : node.children) {
            for (CustomChild newChild : convert(child)) {
                result.add(new CustomChild(node.id, newChild));
            }
        }
        return result;
    }
    
    public static void main(String[] args) {
        Child tree = 
            new Child("1",
                new Child("1_1",
                    new Child("1_1_1"),
                    new Child("1_1_2",
                        new Child("1_1_2_1")
                    )
                ),
                new Child("1_2",
                    new Child("1_2_1",
                        new Child("1_2_1_1")
                    )
                )
            );
        System.out.println("original tree:");
        tree.print();
        System.out.println("converted forest:");
        List<CustomChild> paths = convert(tree);
        for (CustomChild node : paths) {
            node.print();
        } 
    }
}

main函数创建您作为示例给出的输入结构,并调用convert从该结构生成目标路径列表。
这是上面生成的输出:

original tree:
1
  1_1
    1_1_1
    1_1_2
      1_1_2_1
  1_2
    1_2_1
      1_2_1_1
converted forest:
1
  1_1
    1_1_1
1
  1_1
    1_1_2
      1_1_2_1
1
  1_2
    1_2_1
      1_2_1_1

相关问题