java 实现 父子部门的人数 ,父部门人数 为各子部门人数总和 ,和输出各自节点人数
时间: 2023-11-28 08:50:56 浏览: 40
假设我们有一个部门树形结构,每个节点包含一个部门对象和一个子节点列表。部门对象中包含一个部门名称和一个人数。
首先我们需要定义一个递归函数来计算每个节点的人数:
```java
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
```
这个函数首先会获取当前节点的员工数,然后递归调用子节点的这个函数,并将子节点的返回值累加到总人数中。最后更新当前节点的员工数并返回总人数。
接下来我们可以遍历整个部门树,对每个节点调用上面的函数来计算员工数:
```java
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
```
这个函数会先调用 `calculateTotalEmployees` 函数来计算每个节点的员工数,然后遍历整个部门树,输出每个节点的员工数。注意输出时要输出节点名称和员工数。
完整代码如下:
```java
public class DepartmentNode {
private Department department;
private List<DepartmentNode> children;
public DepartmentNode(Department department) {
this.department = department;
children = new ArrayList<>();
}
public Department getDepartment() {
return department;
}
public void addChild(DepartmentNode child) {
children.add(child);
}
public List<DepartmentNode> getChildren() {
return children;
}
}
public class Department {
private String name;
private int employeeCount;
public Department(String name, int employeeCount) {
this.name = name;
this.employeeCount = employeeCount;
}
public String getName() {
return name;
}
public int getEmployeeCount() {
return employeeCount;
}
public void setEmployeeCount(int employeeCount) {
this.employeeCount = employeeCount;
}
}
public class Main {
public static void main(String[] args) {
DepartmentNode root = new DepartmentNode(new Department("总部", 0));
DepartmentNode child1 = new DepartmentNode(new Department("子部门1", 10));
DepartmentNode child2 = new DepartmentNode(new Department("子部门2", 20));
DepartmentNode child3 = new DepartmentNode(new Department("子部门3", 30));
root.addChild(child1);
root.addChild(child2);
root.addChild(child3);
DepartmentNode grandChild1 = new DepartmentNode(new Department("孙子部门1", 5));
DepartmentNode grandChild2 = new DepartmentNode(new Department("孙子部门2", 15));
DepartmentNode grandChild3 = new DepartmentNode(new Department("孙子部门3", 25));
child1.addChild(grandChild1);
child2.addChild(grandChild2);
child3.addChild(grandChild3);
Main main = new Main();
main.printDepartmentEmployeeCount(root);
}
public int calculateTotalEmployees(DepartmentNode node) {
int total = node.getDepartment().getEmployeeCount();
for (DepartmentNode child : node.getChildren()) {
total += calculateTotalEmployees(child);
}
node.getDepartment().setEmployeeCount(total); // 更新当前节点的员工数
return total;
}
public void printDepartmentEmployeeCount(DepartmentNode root) {
calculateTotalEmployees(root); // 先计算每个节点的员工数
printEmployeeCount(root); // 输出每个节点的员工数
}
private void printEmployeeCount(DepartmentNode node) {
System.out.println(node.getDepartment().getName() + ": " + node.getDepartment().getEmployeeCount());
for (DepartmentNode child : node.getChildren()) {
printEmployeeCount(child);
}
}
}
```