java 生成树形结构_Java后台生成树状结构工具类

importjava.util.ArrayList;importjava.util.HashMap;importjava.util.List;importjava.util.Map;public classTreeUtil {//private static String PID = "000000";

/*** 将平行的树,转化为一颗有层级关系的树

*@paramlist

*@parampId

*@return

*/

public static List getTreeList(Listlist,String pId){if (list == null){return null;

}//获取所有头节点

List rootNode = new ArrayList<>();for(TreeData treeData: list ){if(treeData.getId().equals(pId)){

rootNode.add(treeData);

}

}//头节点不存在的情况

if (rootNode.size() == 0){returngetChild(pId,list);

}//头节点存在的情况

for(TreeData treeData : rootNode){

String id=treeData.getId();

treeData.setChild(getChild(id,list));

}returnrootNode;

}private static List getChild(String id,Listlist){//找到id节点子节点

List childList = new ArrayList<>();for(TreeData treeData: list){if(treeData.getpId().equals(id)){

childList.add(treeData);

}

}//给子节点设置子节点

for(TreeData treeData : childList){

id=treeData.getId();//递归

treeData.setChild(getChild(id,list));

}if (childList.size() == 0){return null;

}returnchildList;

}/*** 获取指定层级的树

*@paramtreeList

*@paramlevel

*@return

*/

public static List getTreeNodeOnLevel(List treeList,intlevel){if (treeList == null && treeList.size() == 0){return new ArrayList<>();

}

ArrayList treeListOnLevel = new ArrayList<>();//调用递归方法

treeList = TreeUtil.recursionQueryTree(treeList, treeListOnLevel, 0,level);returntreeList;

}/*** 递归查所有指定层级的树节点

*@paramtreeList

*@paramtreeListOnLevel

*@paramcurrentLevel

*@paramlevel

*@return

*/

private static List recursionQueryTree(List treeList,List treeListOnLevel,int currentLevel,intlevel){

currentLevel++;//递归循环查询指定层级树

for(TreeData treeData : treeList){if (currentLevel ==level){

treeListOnLevel.add(treeData);continue;

}//递归

if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}

recursionQueryTree(treeData.getChild(),treeListOnLevel,currentLevel,level);

}returntreeListOnLevel;

}/*** 统计一颗树的深度

*@paramtreeList

*@return

*/

public static int getTreeLevel(ListtreeList){if (treeList == null || treeList.size() == 0){return 0;

}//调用递归统计方法

return TreeUtil.recursionCount(treeList,0,0);

}private static int recursionCount(List treeList,int currentlevel,intmaxlevel){

currentlevel++;//当当前层级大于目前最大层级时覆盖

if (currentlevel >maxlevel){

maxlevel=currentlevel;

}//递归遍历节点

for(TreeData treeData : treeList){//如果子节点为空不进行递归

if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}//递归

maxlevel =recursionCount(treeData.getChild(), currentlevel, maxlevel);

}returnmaxlevel;

}/*** 获取指定id在此树中的层级位置

*

*@paramlist 要遍历的树

*@paramid 要寻找的节点Id

*@paramrankCount 层级统计器,传入0即可

*@return

*/

public static Map getNode(Listlist,String id,Integer rankCount){

TreeData node= null;//计数器加一

rankCount++;

Map map = new HashMap<>();

map.put("node",node);

map.put("rankCount",rankCount);//如果传入的树为0或返回层级0

if (list == null || list.size() == 0){

map.put("rankCount",0);returnmap;

}//遍历树

for(TreeData treeData : list){if(treeData.getId().equals(id)){

map.put("node",treeData);returnmap;

}if (treeData.getChild() == null || treeData.getChild().size() == 0){continue;

}//递归

Map map1 =getNode(treeData.getChild(),id,rankCount);if (map1.get("node") != null){returnmap1;

}

}//如果传入的节点是不存在的,返回层级0

map.put("rankCount",0);returnmap;

}

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值