学习笔记(八):树形结构

文章介绍了如何使用Java编程构建树形结构,关键在于根节点和子节点的概念。通过实体类`BusinessForm`定义了业务数据,包含根节点和子节点的字段。然后利用递归思路,首先获取所有typeCode为-1的根节点,接着遍历所有节点,判断并构建子树。最后,通过`TreeBuild`类提供构建树形结构的方法,并在测试中应用到实际数据上,成功构建了两级树形菜单。
摘要由CSDN通过智能技术生成

树形结构主要是根节点和子节点,后台利用递归思路进行构建。

一、树节点数据类

这里,我是直接在实体类里增加了children字段,根节点字段是typeCode,子节点字段是businessCode。

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Data
@JsonInclude(value = JsonInclude.Include.ALWAYS)
@ApiModel(description = "业务类")
public class BusinessForm extends Model<BusinessForm> implements Serializable {
    private static final long serialVersionUID = 305168670107337418L;

    @ApiModelProperty(value = "主键id")
    private int id;
    @ApiModelProperty(value = "业务编码")
    private String businessCode;
    @ApiModelProperty(value = "业务名称")
    private String businessName;
    @ApiModelProperty(value = "业务类型编码")
    private String typeCode;
    @ApiModelProperty(value = "材料名称")
    private String materialName;
    @ApiModelProperty(value = "是否为空材料")
    private int isEmptyMaterial;
    @ApiModelProperty(value = "是否有回执")
    private int isEmptyReceipt;
    @ApiModelProperty(value = "市/区县级")
    private String businessLevel;
    @ApiModelProperty(value = "地区编码")
    private String areaCode;
    @ApiModelProperty(value = "备注")
    private String remark;
    @ApiModelProperty(value = "删除标志")
    private String isDelete;
    @ApiModelProperty(value = "创建时间")
    private Date createTime;
    @ApiModelProperty(value = "更新时间")
    private Date updateTime;

    @TableField(exist = false)
    private List<BusinessForm> children;

    public BusinessForm(String businessCode,String businessName,String typeCode){
        this.businessCode = businessCode;
        this.businessName = businessName;
        this.typeCode = typeCode;
    }

}

二、构建树形结构

思路:

1、首先获取所有的根节点,根节点标志是typeCode=-1;

2、根据每一个根节点,与所有节点集合进行判断,当前节点是否为其根节点下的子节点;

3、是,则递归调用构建树形;若不是,则表明该节点不是其根节点下的子节点;

4、继续循环判断节点的父子关系,直到所有的节点与根节点都判断完毕。

import com.example.videoprocessing.entity.BusinessForm;

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

public class TreeBuild {

    //保存参与构造树形的所有数据
    public List<BusinessForm> nodeList = new ArrayList<>();

    /**
     * 构造方法
     * 将数据集合赋值给nodeList 即所有数据作为所有节点
     */
    public TreeBuild(List<BusinessForm> nodeList){
        this.nodeList = nodeList;
    }

    /**
     * 获取需要构建的所有根节点(顶级节点)
     * @return 所有根节点List集合
     */
    public List<BusinessForm> getRootNode(){
        //保存所有根节点(所有根节点的数据)
        List<BusinessForm> rootNodeList = new ArrayList<>();
        //treeNode:查询出每一条数据(节点)
        for (BusinessForm treeNode : nodeList) {
            //判断当前节点是否为根节点,注意。若parentId类型时String,则要采用equals()方法判断
            if("-1".equals(treeNode.getTypeCode())){
                rootNodeList.add(treeNode);
            }
        }
        return rootNodeList;
    }

    /**
     * 根据每一个顶级节点(根节点)进行构建树形结构
     */
    public List<BusinessForm> buildTree(){
        //treeNodes:保存一个根节点所构建出来的完整树形
        List<BusinessForm> treeNodes = new ArrayList<>();
        //getRootNode():获取所有根节点
        for (BusinessForm treeRootNode : getRootNode()){
            //将根节点进行构建子树
            treeRootNode = buildChildTree(treeRootNode);
            //完成一个根节点所构建的树形,增加进来
            treeNodes.add(treeRootNode);
        }
        return treeNodes;
    }

    /**
     * 递归  ---构建子树形结构
     * @param pNode 根节点
     */
    public BusinessForm buildChildTree(BusinessForm pNode){
        List<BusinessForm> childTree = new ArrayList<>();
        //nodeList:所有节点集合(所有数据)
        for (BusinessForm treeNode : nodeList){
            //判断当前节点的父节点id是否等于根节点的id,即当前节点为其下的子节点
            if (treeNode.getTypeCode().equals(pNode.getBusinessCode())){
                //再递归进行判断当前节点的情况,调用自身方法
                childTree.add(buildChildTree(treeNode));
            }
        }
        //for循环结束,即节点下没有任何节点,树形构建结束,设置数结果
        pNode.setChildren(childTree);
        return pNode;
    }
}

三、测试运用

定义businessTreeList,查询表中数据,进行树形结构测试。

public List<BusinessForm> getBusinessTree() {
        List<BusinessForm> businessTreeList = businessMapper.getBusinessTree();
        //创建树形结构(数据集合作为参数)
        TreeBuild treeBuild = new TreeBuild(businessTreeList);
        //原查询结果转换树形结构
        businessTreeList = treeBuild.buildTree();
        return businessTreeList;
    }

补充:

1、测试的数据是两级树形菜单,结果是正确的,这里就不贴图展示了。

2、学习过程中参考了java构建树形结构_java树结构_十年(Sugar)的博客-CSDN博客,表示感谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值