JAVA: cree de forma recursiva una estructura de árbol con nodos secundarios anidados y devuelva parámetros al front-end&& Obtenga todos los nodos hoja bajo un determinado nodo&& Obtenga los nodos hoja bajo un determinado nodo (nodos sin nodos secundarios)


En el proyecto JAVA, se construye recursivamente una estructura de árbol basada en parentId, y sus subnodos secundarios se anidan y se devuelven al front-end. Obtenga todos los nodos hoja bajo un determinado nodo raíz; obtenga los nodos hoja (nodos sin nodos secundarios) bajo un determinado nodo raíz; solo obtenga la identificación del nodo hoja.

construir arbol

tagId es el ID del nodo; parentId es el ID del nodo principal; tagName es el nombre del nodo; Children es el tipo de lista del nodo secundario.

getRootNode () es para obtener todos los nodos raíz. Determinamos que el ID del nodo principal (parentId) es 0 como nodo raíz.

buildChildTree() obtiene el conjunto de todos los nodos y determina si el ID del nodo principal (parentId) del nodo actual es igual al ID (tagId) del nodo raíz, es decir, el nodo actual es su nodo secundario. Luego determine de forma recursiva la situación del nodo actual y llame a su propio método.

buildTree() construye una estructura de árbol basada en cada nodo raíz.

    /**
     *   获取需构建的所有根节点(顶级节点) "0"
     *   @return 所有根节点List集合
     */
    public List<TagRequestVO> getRootNode(){
    
    chre
        // 保存所有根节点(所有根节点的数据)
        List<TagRequestVO> rootNodeList = new ArrayList<>();
        // treeNode:查询出的每一条数据(节点)
        List<Tag> nodeList = tagMapper.selectList(null);
        for (Tag treeNode : nodeList){
    
    
            // 判断当前节点是否为根节点,
            if (treeNode.getParentId().equals("0")) {
    
    
                // 是,添加
                //tagId为节点id;parentId为其父节点id;tagName为节点名称
                TagRequestVO response = new TagRequestVO();
                response.setTagId(treeNode.getTagId());
                response.setTagName(treeNode.getTagName());
                response.setParentId(treeNode.getParentId());
                rootNodeList.add(response);
            }
        }
        return rootNodeList;
    }

    /**
     *  递归-----构建子树形结构
     *  @param  pNode 根节点(顶级节点)
     *  @return 整棵树
     */
    public TagRequestVO buildChildTree(TagRequestVO pNode){
    
    
        List<TagRequestVO> childTree = new ArrayList<>();
        // nodeList:所有节点集合(所有数据)
        List<Tag> nodeList = tagMapper.selectList(null);
        for (Tag treeNode : nodeList) {
    
    
            // 判断当前节点的父节点ID是否等于根节点的ID,即当前节点为其下的子节点
            //tagId为节点id;parentId为其父节点id;tagName为节点名称
            if (treeNode.getParentId().equals(pNode.getTagId())) {
    
    
                // 再递归进行判断当前节点的情况,调用自身方法
                TagRequestVO response = new TagRequestVO();
                response.setTagId(treeNode.getTagId());
                response.setTagName(treeNode.getTagName());
                response.setParentId(treeNode.getParentId());
                childTree.add(buildChildTree(response));
            }
        }
        // for循环结束,即节点下没有任何节点,树形构建结束,设置树结果
        pNode.setChildren(childTree);
        return pNode;
    }

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

Los niños devolverán una matriz vacía [] cuando no haya nodos secundarios; si no quieres arrepentirte cuando esté vacía, puedes agregar un comentario a VO y no volverá.

//为空不返回
@JsonInclude(JsonInclude.Include.NON_EMPTY)
//为null不返回
@JsonInclude(JsonInclude.Include.NON_NULL)

Obtener todos los nodos hoja debajo de un nodo

La construcción del árbol no es muy diferente de la anterior, excepto que la identificación del nodo (tagId) requerida para el filtrado específico debe agregarse a la función de obtención del nodo raíz (getRootNodeByTag()). La estructura del subárbol (buildChildTreeByTag()) es en realidad exactamente la misma que la anterior. Después de construir el árbol, haga coincidir los nodos de hoja debajo de él.

getAllChildren() obtiene todos sus nodos hoja.

getTreeTagId() obtiene todos los ID de sus nodos hoja (tagId).

	/**
     *   获取需构建的根节点,根据根节点tagId匹配
     *   @return 所有根节点List集合
     */
    public List<TagRequestVO> getRootNodeByTag(String tagId){
    
    
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.and(w -> w.eq("tag_id", tagId).or().eq("parent_id", tagId));
        List<Tag> nodeList = tagMapper.selectList(wrapper);
        List<TagRequestVO> rootNodeList = new ArrayList<>();
        for (Tag treeNode : nodeList){
    
    
            TagRequestVO response = new TagRequestVO();
            if (treeNode.getParentId().equals("0")) {
    
    
                response.setTagId(treeNode.getTagId());
                rootNodeList.add(response);
            }
        }
        return rootNodeList;
    }

	/**
     *  递归-----构建子树形结构
     *  @param  pNode 根节点(顶级节点)
     *  @return 整棵树
     */
    public TagRequestVO buildChildTreeByTag(TagRequestVO pNode){
    
    
        List<TagRequestVO> childTree = new ArrayList<>();
        List<Tag> nodeList = tagMapper.selectList(null);
        for (Tag treeNode : nodeList) {
    
    
            if (treeNode.getParentId().equals(pNode.getTagId())) {
    
    
                TagRequestVO response = new TagRequestVO();
                response.setTagId(treeNode.getTagId());
                response.setParentId(treeNode.getParentId());
                childTree.add(buildChildTreeByTag(response));
            }
        }
        pNode.setChildren(childTree);
        return pNode;
    }

	/**
     *  根据每一个顶级节点(根节点)进行构建树形结构
     *  @return  构建整棵树
     */
    public List<TagRequestVO> buildTreeByTag(String tagId){
    
    
        List<TagRequestVO> treeNodes = new ArrayList<>();
        for (TagRequestVO treeRootNode : getRootNodeByTag(tagId)) {
    
    
            treeRootNode = buildChildTreeByTag(treeRootNode);
            treeNodes.add(treeRootNode);
        }
        return treeNodes;
    }

	//获取该节点下的所有叶子节点
    private List<TagRequestVO> getAllChildren(TagRequestVO tagRequestVO,List returnList){
    
    
    	//获取叶子节点children
        List<TagRequestVO> childrenList = tagRequestVO.getChildren();
        if(childrenList!=null && childrenList.size()>0){
    
    
            for(TagRequestVO children : childrenList){
    
    
            	//递归
                getAllChildren(children,returnList);
                returnList.add(children);
            }
        }
        return returnList;
    }

	//获取该节点下的所有叶子节点tagId
	public List<String> getTreeTagId(String tagId){
    
    
        List returnList = new ArrayList();
        List<TagRequestVO> tag = getAllChildren(buildTreeByTag(tagId).get(0), returnList);
        List<String> tagIdList = new ArrayList<>();
        for(TagRequestVO tagChildren : tag){
    
    
            tagIdList.add(tagChildren.getTagId());
        }
        return tagIdList;
    }

Obtenga los nodos hoja que no tienen nodos secundarios en un determinado nodo (sin hijos)

La construcción del árbol no es muy diferente de la anterior, excepto que la identificación del nodo (tagId) requerida para el filtrado específico debe agregarse a la función de obtención del nodo raíz (getRootNodeByTag()). La estructura del subárbol (buildChildTreeByTag()) es en realidad exactamente la misma que la anterior. Después de construir el árbol, haga coincidir los nodos de hoja debajo de él.

getChildren() obtiene todos sus nodos hoja. Tenga en cuenta que no hay hijos (nodos hoja) en la condición de juicio.

getTreeChildrenTagId obtiene todos los ID de sus nodos hoja (tagId).

	/**
     *   获取需构建的根节点,根据根节点tagId匹配
     *   @return 所有根节点List集合
     */
    public List<TagRequestVO> getRootNodeByTag(String tagId){
    
    
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.and(w -> w.eq("tag_id", tagId).or().eq("parent_id", tagId));
        List<Tag> nodeList = tagMapper.selectList(wrapper);
        List<TagRequestVO> rootNodeList = new ArrayList<>();
        for (Tag treeNode : nodeList){
    
    
            TagRequestVO response = new TagRequestVO();
            if (treeNode.getParentId().equals("0")) {
    
    
                response.setTagId(treeNode.getTagId());
                rootNodeList.add(response);
            }
        }
        return rootNodeList;
    }

	/**
     *  根据每一个顶级节点(根节点)进行构建树形结构
     *  @return  构建整棵树
     */
    public List<TagRequestVO> buildTreeByTag(String tagId){
    
    
        List<TagRequestVO> treeNodes = new ArrayList<>();
        for (TagRequestVO treeRootNode : getRootNodeByTag(tagId)) {
    
    
            treeRootNode = buildChildTreeByTag(treeRootNode);
            treeNodes.add(treeRootNode);
        }
        return treeNodes;
    }


	/**
     *  递归-----构建子树形结构
     *  @param  pNode 根节点(顶级节点)
     *  @return 整棵树
     */
    public TagRequestVO buildChildTreeByTag(TagRequestVO pNode){
    
    
        List<TagRequestVO> childTree = new ArrayList<>();
        List<Tag> nodeList = tagMapper.selectList(null);
        for (Tag treeNode : nodeList) {
    
    
            if (treeNode.getParentId().equals(pNode.getTagId())) {
    
    
                TagRequestVO response = new TagRequestVO();
                response.setTagId(treeNode.getTagId());
                response.setParentId(treeNode.getParentId());
                childTree.add(buildChildTreeByTag(response));
            }
        }
        pNode.setChildren(childTree);
        return pNode;
    }

	//获取该节点下 没有子节点的(没有children) 叶子节点
    private List<TagRequestVO> getChildren(TagRequestVO tagRequestVO,List returnList){
    
    
        List<TagRequestVO> childrenList = tagRequestVO.getChildren();
		// 退出递归的条件 只有一层维度结构
        if(childrenList==null || childrenList.size()<=0){
    
    
            returnList.add(tagRequestVO);
        }else{
    
    
			// 有多层维度结果
            for(TagRequestVO children : childrenList){
    
    
                getChildren(children,returnList);
            }
        }
        return returnList;
    }
    
	//获取该节点下 没有子节点的(没有children) 叶子节点的tagId
    public List<String> getTreeChildrenTagId(String tagId){
    
    
        List returnList = new ArrayList();
        List<TagRequestVO> tag = getChildren(buildTreeByTag(tagId).get(0), returnList);
        List<String> childrenTagIdList = new ArrayList<>();
        for(TagRequestVO tagChildren : tag){
    
    
            childrenTagIdList.add(tagChildren.getTagId());
        }
        return childrenTagIdList;
    }

Referencia: https://blog.csdn.net/a18505947362/article/details/122458089

https://blog.csdn.net/weixin_36368404/article/details/115783785?spm=1001.2101.3001.6650.13&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-13-1157837 85-blog- 124102788.pc_relevant_multi_platform_whitelistv4& Depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7ERate-13-115783785-blog-124102788.pc_relevant_multi_platform_whitelistv4&utm _relevant_index=14

Supongo que te gusta

Origin blog.csdn.net/weixin_44436677/article/details/127589077
Recomendado
Clasificación