如何处理树形结构问题

如何处理树形结构问题_第1张图片

1.mysql自连接
适用于对于叶子节点平整的数据,只需要将表自连接,连接次数等于 层数-1
如何处理树形结构问题_第2张图片
如果需要1 1-1 1-1-1就需要连接两次

SELECT
	one.id one_id,
	one.label one_label,
	two.id two_id,
	two.label two_label
FROM
	course_category AS one
	INNER JOIN course_category AS two ON one.id = two.parentid 
WHERE
	one.parentid = '1' 
	AND one.is_show = '1' 
	AND two.is_show = '1' 
ORDER BY
	one.orderby,
	two.orderby

如何处理树形结构问题_第3张图片

2.递归(通用)
1.

 public CourseCategoryTreeDto buildTree(List<CourseCategory> categories, String parentId) {
        CourseCategoryTreeDto parentNode = new CourseCategoryTreeDto();
        List<CourseCategoryTreeDto> childrenNodes = new ArrayList<>();
        for (CourseCategory category : categories) {
            if (category.getParentid().equals(parentId)) {
                CourseCategoryTreeDto childNode = new CourseCategoryTreeDto();
                childNode.setId(category.getId());
                childNode.setName(category.getName());
                childNode.setLabel(category.getLabel());
                childNode.setIsShow(category.getIsShow());
                childNode.setOrderby(category.getOrderby());
                childNode.setIsLeaf(category.getIsLeaf());
                childNode.setParentid(parentId);
                // 如果该节点是父节点,则递归处理其子节点
                if (category.getIsLeaf() == 0) {
                    CourseCategoryTreeDto childTree = buildTree(categories, category.getId());
                    childNode.setChildrenTreeNodes(childTree.getChildrenTreeNodes());
                } else {
                    childNode.setChildrenTreeNodes(null);
                }
                childrenNodes.add(childNode);
            }
        }
            parentNode.setChildrenTreeNodes(childrenNodes);
        return parentNode;
    }



2.使用stream流

public CourseCategoryTreeDto buildTree(List<CourseCategory> categories, String parentId) {
    List<CourseCategoryTreeDto> childrenNodes = categories.stream()
            .filter(category -> category.getParentid().equals(parentId))
            .map(category -> {
                CourseCategoryTreeDto childNode = new CourseCategoryTreeDto();
                childNode.setId(category.getId());
                childNode.setName(category.getName());
                childNode.setLabel(category.getLabel());
                childNode.setIsShow(category.getIsShow());
                childNode.setOrderby(category.getOrderby());
                childNode.setIsLeaf(category.getIsLeaf());
                childNode.setParentid(parentId);
                // 如果该节点是父节点,则递归处理其子节点
                if (category.getIsLeaf() == 0) {
                    CourseCategoryTreeDto childTree = buildTree(categories, category.getId());
                    childNode.setChildrenTreeNodes(childTree.getChildrenTreeNodes());
                } else {
                    childNode.setChildrenTreeNodes(null);
                }
                return childNode;
            })
            .collect(Collectors.toList());
    CourseCategoryTreeDto parentNode = new CourseCategoryTreeDto();
    parentNode.setChildrenTreeNodes(childrenNodes);
    return parentNode;
}

调用方法

    public List<CourseCategoryTreeDto> getCourseCategoryTreeDto() {
        //找出所有可以显示的节点
        List<CourseCategory> courseCategories = courseCategoryMapper.selectList(new QueryWrapper<CourseCategory>().eq("is_show",1));
        CourseCategoryTreeDto courseCategoryTreeDto = buildTree(courseCategories, "1");
        //找到根节点

        return courseCategoryTreeDto.getChildrenTreeNodes();
    }

结果
如何处理树形结构问题_第4张图片

你可能感兴趣的:(java,开发语言)