树状结构显示之递归练习并说明写递归代码的技巧
来源:互联网 发布:windows live安装包 编辑:程序博客网 时间:2024/06/10 14:46
package cn.itcast.oa.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 cn.itcast.oa.domain.Department;/** * 说明:不能使用多层循环的方式,因为需要能支持任意层。 */@SuppressWarnings("unused")public class TreeViewPractice {/** * 练习一:打印所有顶层部门及其子孙部门的信息(名称) 提示:假设有一个 打印部门树 的信息 的方法 * * 要求打印如下效果: * * <pre> * 市场部 * 宣传部 * 业务部 * 业务一部 * 业务二部 * 开发部 * 开发一部 * 开发二部 * </pre> */@Testpublic void printAllDepts_1() {List<Department> topList = findTopLevelDepartmentList();// 方式一// for (Department top : topList) {// showTree(top);// }// 方式二showTreeList(topList);}/** * 显示一颗部门树中所有节点的信息 * * @param top * 树的顶点(根节点) */public void showTree(Department top) {// 顶点System.out.println(top.getName());// 子树for (Department child : top.getChildren()) {showTree(child);}}/** * 显示多颗树的所有节点的信息 * * @param topList */public void showTreeList(Collection<Department> topList) {for (Department top : topList) {// 顶点System.out.println(top.getName());// 子树showTreeList(top.getChildren());}}/** * 练习二:打印所有顶层部门及其子孙部门的信息(名称),用不同的缩进表示层次(使用全角空格)。<br> * 子部门的名称前比上级部门多一个空格,最顶层部门的名字前没有空格。 提示:假设有一个打印部门集合中所有部门信息的方法 * * 要求打印如下效果: * * <pre> * ┣市场部 * ┣宣传部 * ┣业务部 * ┣业务一部 * ┣业务二部 * ┣开发部 * ┣开发一部 * ┣开发二部 * </pre> */@Testpublic void printAllDepts_2() {List<Department> topList = findTopLevelDepartmentList();showTreeList_2(topList, "┣");}// 显示树public void showTreeList_2(Collection<Department> topList, String prefix) {for (Department top : topList) {// 顶点System.out.println(prefix + top.getName());// 子树showTreeList_2(top.getChildren(), " " + prefix);}}/** * 结构如下: * * <pre> * ┣市场部 * ┣宣传部 * ┣业务部 * ┣业务一部 * ┣业务二部 * ┣开发部 * ┣开发一部 * ┣开发二部 * </pre> * * @return 所有最顶层的部门的列表 */public static List<Department> findTopLevelDepartmentList() {Department dept_1_1 = new Department();dept_1_1.setId(new Long(11));dept_1_1.setName("宣传部");Department dept_1_2 = new Department();dept_1_2.setId(new Long(12));dept_1_2.setName("业务部");Department dept_1_2_1 = new Department();dept_1_2_1.setId(new Long(121));dept_1_2_1.setName("业务一部");Department dept_1_2_2 = new Department();dept_1_2_2.setId(new Long(122));dept_1_2_2.setName("业务二部");dept_1_2_1.setParent(dept_1_2);dept_1_2_2.setParent(dept_1_2);Set<Department> children_0 = new LinkedHashSet<Department>();children_0.add(dept_1_2_1);children_0.add(dept_1_2_2);dept_1_2.setChildren(children_0);// ================================Department dept_1 = new Department();dept_1.setId(new Long(1));dept_1.setName("市场部");dept_1_1.setParent(dept_1);dept_1_2.setParent(dept_1);Set<Department> children_1 = new LinkedHashSet<Department>();children_1.add(dept_1_1);children_1.add(dept_1_2);dept_1.setChildren(children_1);// ---Department dept_2_1 = new Department();dept_2_1.setId(new Long(21));dept_2_1.setName("开发一部");Department dept_2_2 = new Department();dept_2_2.setId((new Long(22)));dept_2_2.setName("开发二部");Department dept_2 = new Department();dept_2.setId(new Long(2));dept_2.setName("开发部");dept_2_1.setParent(dept_2);dept_2_2.setParent(dept_2);Set<Department> children_2 = new LinkedHashSet<Department>();children_2.add(dept_2_1);children_2.add(dept_2_2);dept_2.setChildren(children_2);// ---List<Department> depts = new ArrayList<Department>();depts.add(dept_1);depts.add(dept_2);return depts;}}
阅读全文
0 0
- 树状结构显示之递归练习并说明写递归代码的技巧
- 树状结构显示之递归算法
- 递归在Dropdownlist中显示树状结构
- 简单说明什么是递归,什么情况下会使用递归,并写一个简单的递归程序。
- 递归打印目录的树状结构
- 用递归在Dropdownlist中显示树状结构
- 用递归在Dropdownlist中显示树状结构
- 用递归在Dropdownlist中显示树状结构
- 用递归在Dropdownlist中显示树状结构(连数据库)
- 用树状结构显示BBS的帖子及其回帖,使用递归方法.
- mysql中递归树状结构
- Java递归实现树状结构
- angularjs 递归 展示树状结构
- ASP 递归显示无穷树状结构, 下拉框中显示树状效果
- 递归显示目录结构
- oracle之树状结构的存储与展示(递归查询)
- 使用递归的方式显示树形结构
- 在jsp页面中实现递归显示树状结构(树状bbs)
- Python学习笔记(3) -- enumerate函数
- Android handler讲解与实践
- Centos 安装amd显卡
- 1211: 中文系素素的困惑
- android点击button后的页面跳转
- 树状结构显示之递归练习并说明写递归代码的技巧
- 自定义标签配置详解
- 《算法分析与设计》Week 19
- [leetcode]124. Binary Tree Maximum Path Sum
- leetcode371题解
- 矩阵(matrix)应用大总结(二)HDU1588+HDU 5950+HDU 5015
- JSP简介——9大隐式对象
- RabbitMQ 三 角色权限
- Linux中制作压缩文件