Home >Java >javaTutorial >Java recursively traverse tree structure

Java recursively traverse tree structure

高洛峰
高洛峰Original
2017-01-17 13:53:081633browse

No more nonsense, let me just post the code for you. The specific code is as follows:

//菜单树形结构
public JSONArray treeMenuList(JSONArray menuList, int parentId) {
JSONArray childMenu = new JSONArray();
for (Object object : menuList) {
JSONObject jsonMenu = JSONObject.fromObject(object);
int menuId = jsonMenu.getInt("id");
int pid = jsonMenu.getInt("parentId");
if (parentId == pid) {
JSONArray c_node = treeMenuList(menuList, menuId);
jsonMenu.put("childNode", c_node);
childMenu.add(jsonMenu);
}
}
return childMenu;
}
public static void main(String args[]) {
MenuCacheService menuCacheService = new MenuCacheService();
JSONArray jsonArray = new JSONArray();
Menu menu1 = new Menu();
menu1.setId(1l);
menu1.setParentId(0);
menu1.setLevel(0);
Menu menu2 = new Menu();
menu2.setId(2l);
menu2.setParentId(0);
menu2.setLevel(0);
Menu menu3 = new Menu();
menu3.setId(3l);
menu3.setParentId(2);
menu3.setLevel(1);
Menu menu4 = new Menu();
menu4.setId(4l);
menu4.setParentId(2);
menu4.setLevel(1);
Menu menu5 = new Menu();
menu5.setId(5l);
menu5.setParentId(4);
menu5.setLevel(2);
Menu menu6 = new Menu();
menu6.setId(6l);
menu6.setParentId(1);
menu6.setLevel(1);
jsonArray.add(menu1);
jsonArray.add(menu2);
jsonArray.add(menu3);
jsonArray.add(menu4);
jsonArray.add(menu5);
jsonArray.add(menu6);
System.out.print(menuCacheService.treeMenuList(jsonArray,0));
}

ps: java implements recursive tree traversal (used to implement folding menus)

1. Core algorithm

package com.zf.tag.dao;
import java.util.ArrayList;
import java.util.List;
import com.zf.tag.entity.dept.Dept;
public class DeptDao extends BaseDao{
public static List<Dept> searchAllDept() throws Exception{
List<Object[]> listAllObject=new ArrayList<Object[]>();
List<Object[]> tmp=new ArrayList<Object[]>();
List<Dept> listAllDept=new ArrayList<Dept>();
String sql="select id,dname,up_did from tbl_dept";
listAllObject=getResult(sql);
for (Object[] row : listAllObject) {
Object[] ro = new Object[3];
ro[0] = row[0];
ro[1] = row[1];
ro[2] = row[2];
tmp.add(row);
}
for (Object[] row : tmp) {
if (null == row[2]) {
Dept d = new Dept();
d.setId(Integer.parseInt(String.valueOf(row[0])));
d.setDname(String.valueOf(row[1]));
listAllDept.add(d);
fomateDept(tmp, d);
}
}
return listAllDept;
}
private static void fomateDept(List<Object[]> tmp, Dept dept) {
for (Object[] row : tmp) {
if ((null != row[2])
&& Integer.parseInt(String.valueOf(row[2])) == dept.getId()) {
Dept d = new Dept();
d.setId(Integer.parseInt(String.valueOf(row[0])));
d.setDname(String.valueOf(row[1]));
List<Dept> list = dept.getChildren();
if (list == null) {
list = new ArrayList<Dept>();
}
list.add(d);
dept.setChildren(list);
fomateDept(tmp, d);
}
}
}
}

2. Entity class (department)

package com.zf.tag.entity.dept;
import java.util.List;
public class Dept {
private Integer id;
private String dname;
private List<Dept> children;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public List<Dept> getChildren() {
return children;
}
public void setChildren(List<Dept> children) {
this.children = children;
}
public String toString(){
return this.getDname();
}
}

The above is the relevant content of Java recursive traversal of tree structure introduced by the editor. I hope it will be helpful to everyone!

For more articles related to Java recursive traversal of tree structures, please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn