Kotlin 树状结构的遍历 & 递归构建一棵树源代码实例

2019-07-04 12:20:33 浏览数 (1)

代码语言:javascript复制
package com.ak47.cms.cms.tree

import com.ak47.cms.cms.vo.TreeVO

object TreeUtil {

    /**
     * 递归遍历树
     * @param node 当前树节点
     * @param visitor 游历函数
     */
    fun visitTree(node: TreeVO, visitor: (TreeVO) -> Unit) {
        // 调用游历函数
        visitor(node)

        node.children.forEach {
            visitTree(it, visitor)
        }
    }


    /**
     * 根据节点列表,递归构建一棵树
     * @param nodes 节点列表
     * @param category 树的类型
     */
    fun buildTree(nodes: List<TreeVO>, category: TreeCategory): TreeVO {
        val treeNodes = mutableListOf<TreeVO>()

        val rootNodes = nodes.filter { it.parentCode == "-1" && it.category == category.name }
        rootNodes.forEach {
            buildChildren(it, nodes)
            treeNodes.add(it)
        }
        val treeVO = TreeVO()
        treeVO.children = treeNodes
        treeVO.category = TreeCategory.COUNTRY.name
        treeVO.name = TreeCategory.COUNTRY.name
        return treeVO
    }

    /**
     * 递归构建当前节点的的孩子列表
     * @param node 当前节点
     * @param nodes 节点列表
     */
    private fun buildChildren(node: TreeVO, nodes: List<TreeVO>) {
        val nodeCode = node.code
        val children = nodes.filter { it.parentCode == nodeCode }
        children.forEach {
            buildChildren(it, nodes)
        }
        node.children = children
    }
}

其中, TreeVO 代码如下

代码语言:javascript复制
package com.ak47.cms.cms.vo

class TreeVO {
    var code: String = ""
    var parentCode: String = ""
    var name: String = ""
    var category: String = ""
    var children: List<TreeVO> = mutableListOf()
}

前端的效果采用 d3.tree.js

源代码工程

https://github.com/AK-47-D/cms-spider 分支: i9102_20190407

0 人点赞