• java构建树形结构


    树节点父类:

    package com.yw.framework.tree;
    
    import lombok.Data;
    
    import java.util.ArrayList;
    import java.util.LinkedList;
    import java.util.List;
    
    
    @Data
    public class Node {
    
        protected byte isCategory;
        protected String id;
        protected String elementId;
        protected String parentId;
        protected String name;
        protected List<Node> children = new ArrayList<>();
        protected String description;
        protected int rank = 0;
    
        public Node addChild(Node node) {
            if (children == null) {
                this.children = new LinkedList<Node>();
            }
            children.add(node);
            return this;
        }
    }

    工具类:

    package com.yw.framework.tree;
    
    import com.yw.module.core.element.base.BaseElement;
    import com.yw.module.synthesize.type.ElementTypeEnum;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Objects;
    import java.util.Optional;
    import java.util.stream.Collectors;
    
    /**
     * @Author zl
     * @Description 树形构造处理工具类
     *
     * @Date 2021/6/1 9:12
     */
    public class BuildTree {
    
        /**
         * 功能描述: <生成树结构>
         * 〈〉
         *
         * @Param: [T]
         * @Author: zl
         * @Date: 2021/6/1 9:10
         */
        public static List<Node> buildTree(List<? extends Node> nodeList) {
            List<Node> nodes = new ArrayList<>();
            if (nodeList!=null){
                for (Node node : nodeList) {
                    String parentId = node.getParentId();
                    if (parentId == null || "".equals(parentId)) {
                        nodes.add(node);
                        continue;
                    }
                    for (Node parent : nodeList) {
                        if (parent.getId().equals(parentId)) {
                            List<Node> parentList = parent.getChildren();
                            if (parentList == null) {
                                parentList = new ArrayList<>();
                                parentList.add(node);
                                parent.setChildren(parentList);
                            } else {
                                parentList.add(node);
                            }
                        }
                    }
                }
            }
    
            return nodes;
        }
    
    
        /**
         * 给树节点设置层级rank标识
         *
         * @param nodes
         * @Author zl
         */
        public static void setLevel(List<Node> nodes) {
            if (nodes!=null){
                for (Node node : nodes) {
                    String parentId = node.getParentId();
                    if (parentId == null || "".equals(parentId)) {
                        node.setRank(1);
                        dfsLevel(node);
                    }
                }
            }
    
    
        }
    
        /**
         * 递归遍历节点数
         * @param node
         */
        public static void dfsLevel(Node node) {
            List<Node> children = node.getChildren();
            if (children == null || children.isEmpty()) {
                return;
            }
            for (Node child : children) {
                child.setRank(node.getRank() + 1);
                dfsLevel(child);
            }
    
        }
    
        /**
         * 获取树的最大层级
         *
         * @param nodes
         * @return
         */
        public static int maxLevel(List<Node> nodes,int maxlevel) {
            if (nodes==null){
                return 0;
            }
            for (Node node : nodes) {
                maxlevel = Math.max(node.getRank(), maxlevel);
                if (node.getChildren()!=null) {
                    return maxLevel(node.getChildren(), maxlevel);
                }
            }
            return maxlevel;
        }
    
        /**
         * 获取树的某一层级所有节点
         *
         * @param nodes
         * @param level
         * @return
         */
        public static List<Node> getLevelNode(List<Node> nodes, int level, List<Node> nodeList) {
            if (nodes==null){
                return nodeList;
            }
            for (Node node : nodes) {
                if (node.getRank() == level) {
                    nodeList.add(node);
                }
                if (node.getChildren()!=null) {
                    getLevelNode(node.getChildren(), level, nodeList);
                }
            }
            return nodeList;
        }
    
        /**
         * 获取树的所有叶子节点
         *
         * @param nodes
         * @return
         */
        public static List<Node> getLeafNode(List<? extends Node> nodes, List<Node> leafNode) {
            if (nodes != null)
            {
                for (Node node : nodes) {
                    if (node.getChildren() == null || node.getChildren().size() == 0) {
                        leafNode.add(node);
                    } else {
                        getLeafNode(node.getChildren(), leafNode);
                    }
                }
            }
    
            return leafNode;
        }
    
        /**
         * 删除给定的叶子节点
         * @param nodes
         * @param ids
         * @param deleteSize
         * @Author zl
         * @return
         */
        public static int deleteLeafNode(List<Node> nodes, String[] ids, int deleteSize) {
    
            boolean b = nodes.removeIf(node -> {
                if ((node.getParentId() == null || "".equals(node.getParentId())) && (node.getChildren() == null || node.getChildren().size() == 0)) {
                    boolean isDelete = true;
                    for (int i = 0; i < ids.length; i++) {
                        if (ids[i].equals(node.getId())) {
                            isDelete = false;
                            break;
                        }
                    }
                    return isDelete;
                }
                return false;
    
            });
            if (b) {
                deleteSize++;
            }
    
            for (Node node : nodes) {
                if (node.getChildren() == null || node.getChildren().size() == 0) {
                    boolean isDelete = true;
                    for (int i = 0; i < ids.length; i++) {
                        if (ids[i].equals(node.getId())) {
                            isDelete = false;
                        }
                    }
                    if (isDelete) {
                        deleteSize++;
                        for (Node parent : nodes) {
                            if (parent.getId().equals(node.getParentId())) {
                                parent.getChildren().remove(node);
                                break;
                            }
                        }
                    }
                } else {
                    return deleteLeafNode(node.getChildren(), ids, deleteSize);
                }
            }
            return deleteSize;
        }
    
    
    
    
    
        /**
         * 获取某个父节点下面的所有子节点id
         * 并过滤要素 (isCategory为0)
         * @param baseElements
         * @param id
         * @Author zl
         * @return
         */
        public static List<String> getTreeElementSonEntity(List<? extends BaseElement> baseElements, String id, List<String> ids) {
            for (BaseElement baseElement : baseElements) {
                //遍历出父id等于参数的id,add进子节点集合
                if (Objects.equals(id, baseElement.getParentId())) {
                    //符合的具体要素
                    if (baseElement.getIsCategory() == 0){
                        ids.add(baseElement.getId());
                    }
                    //递归遍历下一级
                    getTreeElementSonEntity(baseElements, baseElement.getId(), ids);
                }
            }
            return ids;
        }
    
    
        /**
         * 获取某个要素节点所有父节点(分类)
         *
         * @param elements
         * @param id
         * @Author zl
         * @return
         */
        public static<T extends BaseElement>  List<T> getTreeElementParentEntity(List<T> elements, String id, List<T> newList) {
    
            //找到父节点
            Optional<T> first = elements.stream().filter(
                    e -> Objects.equals(e.getId(), id)
            ).findFirst();
    
            //已经找到最顶层
            if (!first.isPresent()){
                return newList;
            }
    
            T t = first.get();
            newList.add(t);
            //递归遍历父级
            return getTreeElementParentEntity(elements, t.getParentId(), newList);
        }
    
    
    
    }
    心有所想,必有回响
  • 相关阅读:
    24节气冬至
    最佳人体舒适温度是多少?
    常用正则
    点击按钮后的Loading处理
    支付时过渡动画
    npm ci 和 npm install
    Vue批量上传文件及实时进度
    HTML DOM classList 属性的使用
    Electron Uncaught ReferenceError: require is not defined
    javascript 深拷贝的问题
  • 原文地址:https://www.cnblogs.com/zhulei2/p/15689555.html
Copyright © 2020-2023  润新知