Home > Java > javaTutorial > body text

Java code example to implement tree structure

王林
Release: 2023-04-23 17:31:17
forward
1667 people have browsed it

Database table structure

Java code example to implement tree structure

Implementation ideas

1. Get the collection data with parent and child nodes

2. Traverse the collection data , get all the root nodes

3. Traverse the root node and get all the child nodes

4. Recurse the child nodes, connect the recursive child nodes to their parent nodes, until the child The node is empty and the recursion is completed

5. After the recursion is completed, it is returned in the form of a collection. When returning to the front end, it is converted in JSON format and returned.

Specific code

1. Create data. Consistent with database table data

package com.lyq.generateTree;
 
import com.alibaba.fastjson.JSON;
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author : [LiuYanQiang]
 * @version : [v1.0]
 * @className : TreeMain
 * @description : [描述说明该类的功能]
 * @createTime : [2022/5/3 0:05]
 * @updateUser : [LiuYanQiang]
 * @updateTime : [2022/5/3 0:05]
 * @updateRemark : [描述说明本次修改内容]
 */
public class TreeMain {
    public static void main(String[] args) {
        List<Tree> list = new ArrayList<>();
 
        //造数据
        list.add(new Tree("22650", "0", "第一周"));
        list.add(new Tree("22651", "22650", "1.1  随堂练习:《你认为以下属于人工智能的是什么呢?》,时间:0.5min"));
        list.add(new Tree("22652", "22650", "1.2  主题讨论:《使用循环代码块绘制正五角星》,时间:5min"));
        list.add(new Tree("22653", "22650", "1.3  主题讨论:《延时测试》,时间:0.5min"));
        list.add(new Tree("22654", "22650", "1.4  调查问卷:《测试延时时间》, 时间:0.5min"));
        list.add(new Tree("22655", "22650", "1.5  研究与挑战:翟文彪老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22656", "22650", "1.6  研究与挑战:米新江教授开播啦,时间:1min,[N]"));
        list.add(new Tree("22657", "22656", "1.6.1  研究与挑战:丁中文老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22658", "22656", "1.6.2  研究与挑战:郝晓军老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22659", "22656", "1.6.3  研究与挑战:张娟老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22660", "22656", "1.6.4  研究与挑战:王怀军老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22661", "22656", "1.6.5  研究与挑战:何操老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22662", "22656", "1.6.6  研究与挑战:武新丽老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22663", "22656", "1.6.7  一键签到:人脸识别,时间:2min"));
        list.add(new Tree("22664", "22656", "1.6.8  手势签到:人脸识别22,时间:2min"));
        list.add(new Tree("22665", "22656", "1.6.9  研究与挑战:吴欣明老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22666", "22656", "1.6.10  研究与挑战:程艳艳老师开播啦,时间:1min,[N]"));
        list.add(new Tree("22667", "22656", "1.6.11  研究与挑战:本学期课程内容,时间:0.5min"));
        list.add(new Tree("22668", "22656", "1.6.12  研究与挑战:米老师的教学团队介绍,时间:0.3min"));
        list.add(new Tree("22669", "22650", "1.7  研究与挑战:米老师的教学团队介绍,时间:0.3min"));
        list.add(new Tree("22670", "22650", "1.8  研究与挑战:米老师的教学团队介绍,时间:0.3min"));
        list.add(new Tree("22671", "22650", "1.9  研究与挑战:助教机器人提醒您参与活动,时间:2min"));
        list.add(new Tree("22672", "22650", "1.10  研究与挑战:AR系统使用教程-学生端,时间:5min"));
        list.add(new Tree("22673", "22672", "1.10.1  研究与挑战:不一样的课堂修改版,时间:0.5min"));
        list.add(new Tree("22674", "22673", "1.10.1.1  研究与挑战:下载AR APP,时间:8min"));
        list.add(new Tree("22675", "22674", "1.10.1.1.1  研究与挑战:修改版手机端AR助教机器人的登录,时间:5min"));
        list.add(new Tree("22676", "22674", "1.10.1.1.2  研究与挑战:app页面展示,时间:2min"));
        list.add(new Tree("22677", "22674", "1.10.1.1.3  研究与挑战:使用网页版方式, 时间:3min"));
        list.add(new Tree("22678", "22674", "1.10.1.1.4  研究与挑战:《使用学习通账号密码登录AR网页版》,时间:5min"));
        list.add(new Tree("22679", "22674", "1.10.1.1.5  主题讨论:修改时间2022年4月4日《上传:进入AR网页版后的界面》,时间:3min"));
        list.add(new Tree("22680", "22673", "1.10.1.2  研究与挑战:AR学生端教程视频,时间:2min"));
        list.add(new Tree("22681", "22672", "1.10.2  研究与挑战:修改时间2022年4月4日温馨提示-钉钉,时间:0.3min"));
 
 
        list = new ReplacementTree().builTree(list);
 
        String jsonString = JSON.toJSONString(list);
        System.out.println(jsonString);
    }
}
Copy after login

2. Tree structure entity class

package com.lyq.generateTree;
 
 
import java.util.ArrayList;
import java.util.List;
 
/**
 * @author : [LiuYanQiang]
 * @version : [v1.0]
 * @className : ReplacementTree
 * @description : [树型结构装换]
 * @createTime : [2022/5/2 23:37]
 * @updateUser : [LiuYanQiang]
 * @updateTime : [2022/5/2 23:37]
 * @updateRemark : [描述说明本次修改内容]
 */
public class ReplacementTree {
 
    /*
     * @version V1.0
     * Title: builTree
     * @author LiuYanQiang
     * @description 始树形结构创建
     * @createTime  2022/5/3 0:18
     * @param [list]
     * @return java.util.List<com.lyq.generateTree.Tree>*/
    public List<Tree> builTree(List<Tree> list) {
        List<Tree> treeList = new ArrayList<>();
        for (Tree tree : this.getRootNode(list)) {
            //建立子树节点
            tree = this.buildChilTree(tree,list);
            //为根节点设置子树节点
            treeList.add(tree);
        }
        return treeList;
    }
 
    /*
     * @version V1.0
     * Title: buildChilTree
     * @author LiuYanQiang
     * @description 通过递归来创建子树形结构
     * @createTime  2022/5/3 0:18
     * @param [tree, list]
     * @return com.lyq.generateTree.Tree*/
    private Tree buildChilTree(Tree tree,List<Tree> list) {
        List<Tree> treeList = new ArrayList<>();
        for (Tree t : list) {
            //判断当前父节点是否存在子节点
            if (t.getP_id().equals(tree.getId())) {
                treeList.add(this.buildChilTree(t,list));
            }
        }
        tree.setChildren(treeList);
        return tree;
    }
 
    /*
     * @version V1.0
     * Title: getRootNode
     * @author LiuYanQiang
     * @description 获取全部根节点
     * @createTime  2022/5/3 0:18
     * @param [list]
     * @return java.util.List<com.lyq.generateTree.Tree>*/
    private List<Tree> getRootNode(List<Tree> list) {
        List<Tree> rootList = new ArrayList<>();
        for (Tree tree : list) {
            if (tree.getP_id().equals("0")) {
                rootList.add(tree);
            }
        }
        return rootList;
    }
}
Copy after login

The final return situation is as follows

Java code example to implement tree structure

The above is the detailed content of Java code example to implement tree structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template