码迷,mamicode.com
首页 > 编程语言 > 详细

java 递归

时间:2017-09-09 14:36:06      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:tpi   tree   getname   list   str   put   size   ++   lis   

public class TreeTest {

private Integer id;
private Integer pId;
private String name;
private List<TreeTest>children;
public TreeTest() {

}
public TreeTest(Integer id, Integer pId, String name) {
super();
this.id = id;
this.pId = pId;
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getpId() {
return pId;
}
public void setpId(Integer pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}


public List<TreeTest> getChildren1() {
return children;
}
public void setChildren(List<TreeTest> children) {
this.children = children;
}

@Override
public String toString() {
return "TreeTest [id=" + id + ", pId=" + pId + ", name=" + name
+ ", children=" + children + "]";
}


public static void main(String[] args) {
Map<Integer,TreeTest> map = new HashMap<Integer,TreeTest>();

TreeTest terr1 = new TreeTest(1,0,"一级父节点");
TreeTest terr2 = new TreeTest(2,1,"一级1子节点");
TreeTest terr3 = new TreeTest(3,2,"一级2子节点");
TreeTest terr4 = new TreeTest(4,0,"二级父节点");
TreeTest terr5 = new TreeTest(5,4,"二级1子节点");
TreeTest terr6 = new TreeTest(6,4,"二级1子节点2");
TreeTest terr7 = new TreeTest(7,3,"一级3子节点");
TreeTest terr8 = new TreeTest(8,5,"二级2子节点");
map.put(terr1.getId(), terr1);
map.put(terr2.getId(), terr2);
map.put(terr3.getId(), terr3);
map.put(terr4.getId(), terr4);
map.put(terr5.getId(), terr5);
map.put(terr6.getId(), terr6);
map.put(terr7.getId(), terr7);
map.put(terr8.getId(), terr8);
List<TreeTest> li = getChildren(map,0,1);
for (int i = 0; i < li.size(); i++) {
System.out.println(li.get(i));
}
// System.out.println(JSON.toJSON(li));
}

//递归树
public static List<TreeTest> getChildren(Map<Integer,TreeTest> trees,Integer id,Integer leve){
List<TreeTest> list = new ArrayList<TreeTest>();

for (TreeTest tree : trees.values()) {
if(id == tree.getpId()){
System.out.println("-递归" + tree.toString());
List<TreeTest> chidren = getChildren(trees,tree.getId(),++leve);
list.add(tree);//本身
tree.setChildren(chidren);//子节点
leve--;
}

}
return list;
}

java 递归

标签:tpi   tree   getname   list   str   put   size   ++   lis   

原文地址:http://www.cnblogs.com/yangpeng-jingjing/p/7498049.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!