递归树,从叶子节点找到父节点的的各种参数包括路径
By:Roy.LiuLast updated:2016-10-08
这几天有个新需求,无聊的报表,通过各种维度组合成一个树,点击数的节点,组合各种条件去查询数据,由于在树的不同层级,需要向上查找父节点,直到根节点的各种组合条件。所以一个基本的想法是从叶子节点向上递归得到各种条件.
给Team的人写了个简单的测试,包括一个树的VO, 条件的类.还有一个主类,测试用的
1. 定义 TreeVO
2. 得到各种查询条件的类Condiction类,包含了各种维度
3. 主要测试类:
构造了一个树,并遍历,得到各种维度的条件。
仅仅测试给team 人员参考而已。
给Team的人写了个简单的测试,包括一个树的VO, 条件的类.还有一个主类,测试用的
1. 定义 TreeVO
public class TreeVO { private int pid; private int id; private String name; private String type; private String code; private Listchild = new ArrayList (); //...省去各种get,set. }
2. 得到各种查询条件的类Condiction类,包含了各种维度
public class Condiction { private String project; private String region; private String subcon; //...省去set,get }
3. 主要测试类:
public class MainClass { public static void main(String[] args) { MaptreeMap = new HashMap (); TreeVO tree = new TreeVO(); tree.setId(1); tree.setPid(-1); tree.setCode("123code"); tree.setType("project"); tree.setName("rootNmae"); treeMap.put("1", tree); int count = 2; for(int i=0; i<5; i++) { TreeVO treeRegion = new TreeVO(); treeRegion.setId(count++); treeRegion.setPid(1); treeRegion.setCode("region" + i); treeRegion.setType("region"); treeRegion.setName("region" + i); tree.getChild().add(treeRegion); treeMap.put(treeRegion.getId() + "", treeRegion); for(int j=0; j<5; j++) { TreeVO treeSubcon = new TreeVO(); treeSubcon.setId(count++); treeSubcon.setPid(treeRegion.getId()); treeSubcon.setCode("subcon" +i+j); treeSubcon.setType("subcon"); treeSubcon.setName("subcon" +i+j); treeMap.put(treeSubcon.getId() + "", treeSubcon); treeRegion.getChild().add(treeSubcon); } } // 得到叶子节点 List leafList = new ArrayList (); for(Entry entry:treeMap.entrySet()) { if (entry.getValue().getChild().size()==0) { leafList.add(entry.getValue()); } } for(TreeVO treevo : leafList) { Condiction c = new Condiction(); c = recNodes(c,treevo, treeMap); System.out.println(c.getProject()); System.out.println(c.getRegion()); System.out.println(c.getSubcon()); System.out.println("======================"); } System.out.println(tree); } public static Condiction recNodes(Condiction c,TreeVO currentTreeVO, Map treeMap) { if (currentTreeVO.getType().equals("subcon")) { c.setSubcon(currentTreeVO.getCode() + "-" + currentTreeVO.getType()); } if (currentTreeVO.getType().equals("region")) { c.setRegion(currentTreeVO.getCode() + "-" + currentTreeVO.getType()); } if (currentTreeVO.getType().equals("project")) { c.setProject(currentTreeVO.getCode() + "-" + currentTreeVO.getType()); } if (currentTreeVO.getPid() == -1) { return c; } TreeVO pTreevo = treeMap.get(currentTreeVO.getPid() + ""); return recNodes(c, pTreevo, treeMap); }
构造了一个树,并遍历,得到各种维度的条件。
仅仅测试给team 人员参考而已。
From:一号门
Previous:开通了百度打赏, 新工作也慢慢步入正轨
COMMENTS