递归树,从叶子节点找到父节点的的各种参数包括路径

这几天有个新需求,无聊的报表,通过各种维度组合成一个树,点击数的节点,组合各种条件去查询数据,由于在树的不同层级,需要向上查找父节点,直到根节点的各种组合条件。所以一个基本的想法是从叶子节点向上递归得到各种条件.
给Team的人写了个简单的测试,包括一个树的VO, 条件的类.还有一个主类,测试用的

1. 定义 TreeVO
程序代码 程序代码

public class TreeVO {
    
    private int pid;
    private int id;
    private String name;
    private String type;
    private String code;
    private List<TreeVO> child = new ArrayList<TreeVO>();
        //...省去各种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) {
        Map<String, TreeVO> treeMap = new HashMap<String, TreeVO>();
        
        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<TreeVO> leafList = new ArrayList<TreeVO>();
        for(Entry<String, TreeVO> 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<String, TreeVO> 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 人员参考而已。


除非申明,文章均为一号门原创,转载请注明本文地址,谢谢!
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: Java递归树
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.