多级菜单 树结构 排序 前端 后端 java

目录

省流:

正文:

v1.0版

前端传的值:

后端代码:

v2.0版

v3.0版


省流:

前端提交过来整个树即可。

给整个树进行sort。代码如下:

java 复制代码
    public static void sort(List<Node> tree){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            if(o.getChild()!=null){
                sort(o.getChild());
            }
        }
    }

这个只是单纯排序,没有替换parentId。升级版本见正文。

排序完以后,结果如下:

一级节点的sort:1,2,3,4...

二级节点的sort:每个一级节点下的二级节点,都会从1开始排序:1,2,3,4...

bash 复制代码
|-- 一级节点1
    |--二级节点1
    |--二级节点2
    |--二级节点3
|-- 一级节点2
    |--二级节点1
    |--二级节点2
    |--二级节点3
|-- 一级节点3
    |--二级节点1
    |--二级节点2
    |--二级节点3

正文:

v1.0版

前端传的值:

bash 复制代码
{
    "id": "雪花id",
    "parentId": "雪花id",
	"name":"书籍",
	"sort":"1",
    "children": [
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "数学",
            "sort": 1,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "几何学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "代数学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "概率学",
                    "sort": 3,
                }
            ]

        },
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "物理",
            "sort": 2,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "光学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "力学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "量子学",
                    "sort": 3,
                }
            ]
        },
        {
            "id": "雪花id",
            "parentId": "雪花id",
            "name": "化学",
            "sort": 3,
            "children": [
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "有机学",
                    "sort": 1,

                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "无机学",
                    "sort": 2,
                },
                {
                    "children": [],
                    "id": "雪花id",
                    "parentId": "雪花id",
                    "name": "应用化学",
                    "sort": 3,
                }
            ]
        }
    ]
}

后端代码:

后端模拟前端传值单元测试:

java 复制代码
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SortT1 {
    public static void main(String[] args) {
        //一级节点
        Node firstNode1 = new Node();
        Node firstNode2 = new Node();
        Node firstNode3 = new Node();
        List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

        //二级节点
        Node sec1First1 = new Node();
        Node sec2First1 = new Node();
        Node sec3First1 = new Node();
        List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
        firstNode1.setChild(second1);

        Node sec1First2 = new Node();
        Node sec2First2 = new Node();
        Node sec3First2 = new Node();
        List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
        firstNode2.setChild(second2);

        Node sec1First3 = new Node();
        Node sec2First3 = new Node();
        Node sec3First3 = new Node();
        List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
        firstNode3.setChild(second3);

        sort(first);

        System.out.println(first);
    }

    public static void sort(List<Node> tree){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            if(o.getChild()!=null){
                sort(o.getChild());
            }
        }
    }
}
java 复制代码
import java.util.List;

public class Node {
    private Integer sort;
    private List<Node> child;

    public Integer getSort() {
        return sort;
    }

    public void setSort(Integer sort) {
        this.sort = sort;
    }

    public List<Node> getChild() {
        return child;
    }

    public void setChild(List<Node> child) {
        this.child = child;
    }
}

v2.0版

增加parentId

java 复制代码
    public static void main(String[] args) {
        //一级节点
        Node firstNode1 = new Node("0001");
        Node firstNode2 = new Node("0002");
        Node firstNode3 = new Node("0003");
        List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

        //二级节点
        Node sec1First1 = new Node("00010001");
        Node sec2First1 = new Node("00010002");
        Node sec3First1 = new Node("00010003");
        List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));
        firstNode1.setChild(second1);

        Node sec1First2 = new Node("00020001");
        Node sec2First2 = new Node("00020002");
        Node sec3First2 = new Node("00020003");
        List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));
        firstNode2.setChild(second2);

        Node sec1First3 = new Node("00030001");
        Node sec2First3 = new Node("00030002");
        Node sec3First3 = new Node("00030003");
        List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));
        firstNode3.setChild(second3);

        sort(first, "0");

        System.out.println(first);
    }
    public static void sort(List<Node> tree,String parentId){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            o.setParentId(parentId);
            if(o.getChild()!=null){
                sort(o.getChild(),o.getId());
            }
        }
    }
java 复制代码
public class Node {
    public Node(String id){
        this.id = id;
    }
    private Integer sort;
    private List<Node> child;
    private String id;
    private String parentId;
//省略了get set方法没写,自己测的时候记得加上
}

v3.0版

保存到数据库:将所有节点都放到一个list里。

java 复制代码
    public static void main(String[] args) {
        //省略前面的代码
        //...
        List<Node> result = new ArrayList<>();
        sort(first, "0", result);
        //将结果集保存到数据库
        xxxMapper.saveOrUpdate(result);
    }

    public static void sort(List<Node> tree,String parentId,List<Node> result){
        int i = 0;
        for (Node o : tree) {
            o.setSort(i++);
            o.setParentId(parentId);
            if(o.getChild()!=null){
                sort(o.getChild(),o.getId(),result);
            }
            o.setChild(null);//递归走出来后就不需要child了
            result.add(o);//将当前节点存到结果集里
        }
    }

注意:为了避免意外发生,生产上记得加上深度,以防万一出现死循环导致栈溢出stackoverflow。

===================分割线===================

文章到此已经结束,以下是紫薯布丁

|-- 一级节点1

|--二级节点1

|--二级节点2

|--二级节点3

|-- 一级节点2

|--二级节点1

|--二级节点2

|--二级节点3

|-- 一级节点3

|--二级节点1

|--二级节点2

|--二级节点3

{

"id": "雪花id",

"parentId": "雪花id",

"name":"书籍",

"sort":"1",

"children": [

{

"id": "雪花id",

"parentId": "雪花id",

"name": "数学",

"sort": 1,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "几何学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "代数学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "概率学",

"sort": 3,

}

]

},

{

"id": "雪花id",

"parentId": "雪花id",

"name": "物理",

"sort": 2,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "光学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "力学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "量子学",

"sort": 3,

}

]

},

{

"id": "雪花id",

"parentId": "雪花id",

"name": "化学",

"sort": 3,

"children": [

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "有机学",

"sort": 1,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "无机学",

"sort": 2,

},

{

"children": [],

"id": "雪花id",

"parentId": "雪花id",

"name": "应用化学",

"sort": 3,

}

]

}

]

}

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

public class SortT1 {

public static void main(String[] args) {

//一级节点

Node firstNode1 = new Node();

Node firstNode2 = new Node();

Node firstNode3 = new Node();

List<Node> first = new ArrayList<>(Arrays.asList(firstNode1,firstNode2,firstNode3));

//二级节点

Node sec1First1 = new Node();

Node sec2First1 = new Node();

Node sec3First1 = new Node();

List<Node> second1 = new ArrayList<>(Arrays.asList(sec1First1,sec2First1,sec3First1));

firstNode1.setChild(second1);

Node sec1First2 = new Node();

Node sec2First2 = new Node();

Node sec3First2 = new Node();

List<Node> second2 = new ArrayList<>(Arrays.asList(sec1First2,sec2First2,sec3First2));

firstNode2.setChild(second2);

Node sec1First3 = new Node();

Node sec2First3 = new Node();

Node sec3First3 = new Node();

List<Node> second3 = new ArrayList<>(Arrays.asList(sec1First3,sec2First3,sec3First3));

firstNode3.setChild(second3);

sort(first);

System.out.println(first);

}

public static void sort(List<Node> tree){

int i = 0;

for (Node o : tree) {

o.setSort(i++);

if(o.getChild()!=null){

sort(o.getChild());

}

}

}

}

public static void main(String[] args) {

List<Node> result = new ArrayList<>();

sort(first, "0", result);

//将结果集保存到数据库

xxxMapper.saveOrUpdate(result);

}

public static void sort(List<Node> tree,String parentId,List<Node> result){

int i = 0;

for (Node o : tree) {

o.setSort(i++);

o.setParentId(parentId);

if(o.getChild()!=null){

sort(o.getChild(),o.getId(),result);

}

o.setChild(null);//递归走出来后就不需要child了

result.add(o);//将当前节点存到结果集里

}

}

public class Node {

public Node(String id){

this.id = id;

}

private Integer sort;

private List<Node> child;

private String id;

private String parentId;

//省略了get set方法没写,自己测的时候记得加上

}

相关推荐
2401_8543910812 分钟前
城镇住房保障:SpringBoot系统功能概览
java·spring boot·后端
hummhumm14 分钟前
Oracle 第29章:Oracle数据库未来展望
java·开发语言·数据库·python·sql·oracle·database
wainyz23 分钟前
Java NIO操作
java·开发语言·nio
工业3D_大熊28 分钟前
【虚拟仿真】CEETRON SDK在船舶流体与结构仿真中的应用解读
java·python·科技·信息可视化·c#·制造·虚拟现实
lzb_kkk37 分钟前
【JavaEE】JUC的常见类
java·开发语言·java-ee
爬山算法1 小时前
Maven(28)如何使用Maven进行依赖解析?
java·maven
2401_857439691 小时前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧6661 小时前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
李老头探索1 小时前
Java面试之Java中实现多线程有几种方法
java·开发语言·面试
芒果披萨2 小时前
Filter和Listener
java·filter