1. 一课树的结构
(1), 要有一个根节点
(2), 有0个或者多个子数
2. 树状结构显示
package com.niepengfei.domain;
import java.io.Serializable;
import java.util.Set;
public class Department implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String description;
private Department parent;
private Set<Department> children;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Department getParent() {
return parent;
}
public void setParent(Department parent) {
this.parent = parent;
}
public Set<Department> getChildren() {
return children;
}
public void setChildren(Set<Department> children) {
this.children = children;
}
}
package com.niepengfei.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.junit.Test;
import com.niepengfei.domain.Department;
public class TreeTest {
/**
* 结构类似于:
*
* 党政部门
* 学校办公室
* 党委办公室
* 教学机构
* 交通工程学院
* 计算机工程学院
* 计1
* 计2
* @return
*/
public static List<Department> findTopLevelDepartmentList(){
//------------------------------
Department dep1_1 = new Department();
dep1_1.setName("党政部门");
Department dep1_1_1 = new Department();
dep1_1_1.setName("学校办公室");
dep1_1_1.setParent(dep1_1);
Department dep1_1_2 = new Department();
dep1_1_2.setName("党委办公室");
dep1_1_2.setParent(dep1_1);
Set<Department> children1_1 = new LinkedHashSet<Department>();
children1_1.add(dep1_1_1);
children1_1.add(dep1_1_2);
dep1_1.setChildren(children1_1);
//------------------------------
//------------------------------
Department dep1_2 = new Department();
dep1_2.setName("教学机构");
Department dep1_2_1 = new Department();
dep1_2_1.setName("交通工程学院");
dep1_2_1.setParent(dep1_2);
Department dep1_2_2 = new Department();
dep1_2_2.setName("计算机工程学院");
dep1_2_2.setParent(dep1_2);
Set<Department> children1_2 = new LinkedHashSet<Department>();
children1_2.add(dep1_2_1);
children1_2.add(dep1_2_2);
dep1_2.setChildren(children1_2);
//------------------------------
//------------------------------
Department dep1_2_2_1 = new Department();
dep1_2_2_1.setName("计1");
dep1_2_2_1.setParent(dep1_2_2);
Department dep1_2_2_2 = new Department();
dep1_2_2_2.setName("计2");
dep1_2_2_2.setParent(dep1_2_2);
Set<Department> children1_2_2 = new LinkedHashSet<Department>();
children1_2_2.add(dep1_2_2_1);
children1_2_2.add(dep1_2_2_2);
dep1_2_2.setChildren(children1_2_2);
//------------------------------
List<Department> topLevelDepartmentList = new ArrayList<Department> ();
topLevelDepartmentList.add(dep1_1);
topLevelDepartmentList.add(dep1_2);
return topLevelDepartmentList;
}
/**
* 方法一:
* print all departments,as below:
* 党政部门
* 学校办公室
* 党委办公室
* 教学机构
* 交通工程学院
* 计算机工程学院
* 计1
* 计2
*/
@Test
public void printAllDepartments(){
List<Department> depts = findTopLevelDepartmentList();
for(Department top : depts){
showTree(top);
}
}
public void showTree(Department top){
System.out.println(top.getName());
if(top.getChildren()!=null){
for(Department child : top.getChildren()){
showTree(child);
}
}
}
///
/**
* 方法二:
* print all departments,as below:
* 党政部门
* 学校办公室
* 党委办公室
* 教学机构
* 交通工程学院
* 计算机工程学院
* 计1
* 计2
*/
@Test
public void printAllDepartments2(){
List<Department> depts = findTopLevelDepartmentList();
showTreeList(depts);
}
public void showTreeList(Collection<Department> topList){
if(topList!=null){
for(Department top : topList){
System.out.println(top.getName());
showTreeList(top.getChildren());
}
}
}
/
/**
* 结构类似于:
*
* 党政部门
* 学校办公室
* 党委办公室
* 教学机构
* 交通工程学院
* 计算机工程学院
* 计1
* 计2
*/
@Test
public void printAllDepartments3(){
List<Department> depts = findTopLevelDepartmentList();
showTreeList3(depts,"");
}
public void showTreeList3(Collection<Department> topList, String prefix){
if(topList!=null){
for(Department top : topList){
System.out.println(prefix+top.getName());
showTreeList3(top.getChildren()," "+prefix);
}
}
}
}
3. 运行结果
(1), 测试printAllDepartments()方法
(2),测试printAllDepartments2()方法
(3),测试printAllDepartments3()方法