Java Tree 递归实现(项目中实际应用)

Java Tree 递归实现(项目中实际应用)


Java Tree 递归实现(项目中实际应用)

  • 部门Bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.ArrayList;
import java.util.List;

public class Department {
    private int id;
    private String name;
    private int parentId;
    private List<Department> children = new ArrayList<Department>();

    public Department(int id, String name, int parentId) {
        this.id = id;
        this.name = name;
        this.parentId = parentId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getParentId() {
        return parentId;
    }

    public void setParentId(int parentId) {
        this.parentId = parentId;
    }

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

    public void setChildren(List<Department> children) {
        this.children = children;
    }

    @Override
    public String toString() {
        return "Department{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", parentId=" + parentId +
                ", children=" + children +
                '}';
    }
}
  • 测试类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

import java.util.ArrayList;
import java.util.List;

public class DempartmentThree {
    public static void main(String[] args) {
        List<Department> departmentList = new ArrayList<>();
        departmentList.add(new Department(1, "研发部门", 0));
        departmentList.add(new Department(2, "研发团队1", 1));
        departmentList.add(new Department(3, "研发团队2", 1));
        departmentList.add(new Department(4, "财务部门", 0));
        departmentList.add(new Department(5, "财务A部门", 4));
        departmentList.add(new Department(6, "财务B部门", 4));
        departmentList.add(new Department(7, "财务A部门团队1", 5));
        departmentList.add(new Department(8, "财务A部门团队2", 5));
        departmentList.add(new Department(9, "财务B部门团队1", 6));
        departmentList.add(new Department(10, "财务B部门团队2", 6));

        List<Department> listTree = getThree(departmentList,0);
        //Tree
        System.out.println(listTree);

        System.out.println("--------------");
        
        //由Tree某个节点及其子节点组成的姓名集合
        //System.out.println(getTreeName(listTree.get(0)));
    }

    private static List<Department> getThree(List<Department> list,int parentId){
        //获取所有子节点
        List<Department> childTreeList = getChildTree(list,parentId);
        for (Department dept:childTreeList) {
            dept.setChildren(getThree(list,dept.getId()));
        }
        return childTreeList;
    }

    private static List<Department> getChildTree(List<Department> list,int id){
        List<Department> childTree = new ArrayList<>();
        for (Department dept:list) {
            if(dept.getParentId() == id){
                childTree.add(dept);
            }
        }
        return childTree;
    }
}
  • 使用队列将树结构转换为list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//取出上述部门Tree中的某个节点及其所有子节点的name值
//利用队列的特点(先进先出)
private static List<String> getTreeName(Department department) {
    //部门名称集合
    List<String> names = new ArrayList<String>();
    //部门队列
    Queue<Department> queue = new LinkedList<Department>();
    //先将树顶压入队列
    queue.add(department);

    while (!queue.isEmpty()) {
        //从队列取出
        Department d = queue.remove();
        names.add(d.getName());
        queue.addAll(d.getChildren());
    }
    return names;
}