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

时间:2025-05-12 09:32:00

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

给Team的人写了个简单的测试,包括一个树的VO, 条件的类.还有一个主类,测试用的

1. 定义 TreeVOpublic 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();

(1);

(-1);

("123code");

("project");

("rootNmae");

("1", tree);

int count = 2;

for(int i=0; i<5; i++) {

TreeVO treeRegion = new TreeVO();

(count++);

(1);

("region" + i);

("region");

("region" + i);

().add(treeRegion);

(() + "", treeRegion);

for(int j=0; j<5; j++) {

TreeVO treeSubcon = new TreeVO();

(count++);

(());

("subcon" +i+j);

("subcon");

("subcon" +i+j);

(() + "", treeSubcon);

().add(treeSubcon);

}

}

// 得到叶子节点

ListleafList = new ArrayList();

for(Entryentry:()) {

if (().getChild().size()==0) {

(());

}

}

for(TreeVO treevo : leafList) {

Condiction c = new Condiction();

c = recNodes(c,treevo, treeMap);

(());

(());

(());

("======================");

}

(tree);

}

public static Condiction recNodes(Condiction c,TreeVO currentTreeVO, MaptreeMap) {

if (().equals("subcon")) {

(() + "-" + ());

}

if (().equals("region")) {

(() + "-" + ());

}

if (().equals("project")) {

(() + "-" + ());

}

if (() == -1) {

return c;

}

TreeVO pTreevo = (() + "");

return recNodes(c, pTreevo, treeMap);

}

构造了一个树,并遍历,得到各种维度的条件。

仅仅测试给team 人员参考而已。