Java实现树形图

实现树形图最主要的就是用到键值对,而java中可以实现键值对的可以用map集合来实现键值对,但是map并不是集合,它只是集合的分类但不是集合。

树形图实现效果

在这里插入图片描述
数据库设计
通过键值对来查询数据
在这里插入图片描述

封装好类
public class area {
private int AreaID;
private String AreaName;
private int AreaCityID;
public int getAreaID() {
return AreaID;
}
public void setAreaID(int areaID) {
AreaID = areaID;
}
public String getAreaName() {
return AreaName;
}
public void setAreaName(String areaName) {
AreaName = areaName;
}
public int getAreaCityID() {
return AreaCityID;
}
public void setAreaCityID(int areaCityID) {
AreaCityID = areaCityID;

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
$(function(){ $.fn.extend({ SimpleTree:function(options){ //初始化参数 var option = $.extend({ click:function(a){ } },options); option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */ option._init=function(){ /* * 初始化菜单展开状态,以及分叉节点的样式 */ this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */ this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */ this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */ this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */ }/* option._init() End */ /* 设置所有超链接不响应单击事件 */ this.find("a").click(function(){ $(this).parent("li").click(); return false; }); /* 菜单项 接受单击 */ this.find("li").click(function(){ /* * 当单击菜单项 * 1.触发用户自定义的单击事件,将该 标签中的第一个超链接做为参数传递过去 * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true) * 3.重新初始化菜单 */ option.click($(this).find("a")[0]); /* 触发单击 */ /* * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false * 否则修改其 show 属性为 true */ /* if($(this).next("ul").attr("show")=="true"){ $(this).next("ul").attr("show","false"); }else{ $(this).next("ul").attr("show","true"); }*/ /* 初始化菜单 */ option._init(); }); /* 设置所有父节点样式 */ this.find("ul").prev("li").addClass("folder"); /* 设置节点“是否包含子节点”属性 */ this.find("li").find("a").attr("hasChild",false); this.find("ul").prev("li").find("a").attr("hasChild",true); /* 初始化菜单 */ option._init(); }/* SimpleTree Function End */ }); });
实现树形结构图,需要先有一个树形结构的数据模型,然后通过查询数据库获取数据,最后通过递归生成树形结构。以下是一个简单的示例: 1. 数据模型 假设我们有一个表格叫做`category`,它有以下字段: - id:分类的唯一标识 - name:分类的名称 - parent_id:父分类的id(如果没有父分类,则为null) 我们可以用一个Java类来表示这个数据模型: ``` public class Category { private int id; private String name; private Integer parentId; // 注意这里用了Integer类型,因为parent_id可能为null // 省略getter和setter } ``` 2. 查询数据库 我们可以使用JDBC或ORM框架(如MyBatis、Hibernate等)来查询数据库,这里以MyBatis为例: ``` @Mapper public interface CategoryMapper { @Select("SELECT id, name, parent_id FROM category ORDER BY parent_id, id") List<Category> findAll(); } ``` 这里的`@Mapper`和`@Select`是MyBatis的注解,表示这是一个Mapper接口,以及查询所有的分类数据。 3. 生成树形结构 最后,我们可以使用递归来生成树形结构。首先,我们需要定义一个方法,它的输入是所有的分类数据,输出是一个根节点: ``` public CategoryNode buildTree(List<Category> categories) { Map<Integer, CategoryNode> nodes = new HashMap<>(); // 用Map来快速查找节点 // 遍历所有分类,创建节点 for (Category category : categories) { CategoryNode node = new CategoryNode(category.getId(), category.getName()); nodes.put(category.getId(), node); } // 遍历所有分类,连接父子节点 for (Category category : categories) { Integer parentId = category.getParentId(); if (parentId != null) { CategoryNode parent = nodes.get(parentId); CategoryNode child = nodes.get(category.getId()); parent.addChild(child); } } // 最后返回根节点 for (CategoryNode node : nodes.values()) { if (node.getParent() == null) { return node; } } return null; // 如果没有根节点,则返回null } ``` 这个方法的思路很简单:先遍历所有分类,创建对应的节点并存储到Map中;然后再遍历所有分类,连接父子节点;最后找到根节点并返回。这里的`CategoryNode`是一个自定义的类,它表示一个节点: ``` public class CategoryNode { private int id; private String name; private List<CategoryNode> children = new ArrayList<>(); private CategoryNode parent; public CategoryNode(int id, String name) { this.id = id; this.name = name; } public void addChild(CategoryNode child) { children.add(child); child.setParent(this); } // 省略getter和setter } ``` 这个类有四个属性:id、name、children、parent。其中,children表示它的子节点,parent表示它的父节点。addChild方法用于添加子节点时同时设置父子关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值