Java递归实现树结构

递归实现树结构数据

一、递归概念:自己的理解,自己调用自己,何为自己调用自己,比如
下面的**chilMenus.add(buildChilTree(menuNode))**就是在自己调用自己,查询到下一级的下一级,有几级我就调用几次我自己,从而实现树状结构中的父节点下的所有子级。

//递归,建立子树形结构
	public Menu buildChilTree(Menu pNode){
		List chilMenus =new  ArrayList();
		for(Menu menuNode : menuList) {
			if(menuNode.getParentId().equals(pNode.getId())) {
				chilMenus.add(buildChilTree(menuNode));
			}
		}
		pNode.setChildren(chilMenus);
		return pNode;
	}

那么,上代码:
①实体类

package recursion;

import java.util.List;
public class Menu {
	private String id;
	private String parentId;
	private String text;
	private String url;
	private String yxbz;
	private List children;

	public Menu(String id,String parentId,String text,String url,String yxbz) {
		this.id=id;
		this.parentId=parentId;
		this.text=text;
		this.url=url;
		this.yxbz=yxbz;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getParentId() {
		return parentId;
	}

	public void setParentId(String parentId) {
		this.parentId = parentId;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getYxbz() {
		return yxbz;
	}

	public void setYxbz(String yxbz) {
		this.yxbz = yxbz;
	}

	public List getChildren() {
		return children;
	}

	public void setChildren(List children) {
		this.children = children;
	}

	@Override
	public String toString() {
		return "Menu{" +
				"id='" + id + '\'' +
				", parentId='" + parentId + '\'' +
				", text='" + text + '\'' +
				", url='" + url + '\'' +
				", yxbz='" + yxbz + '\'' +
				", children=" + children +
				'}';
	}
}

② getRootNode()获取根节点(父节点),buildChilTree(获取所有子节点),builTree整合子节点和父节点的数据,实现树结构

package recursion;

import java.util.ArrayList;
import java.util.List;
public class MenuTree{
	private List menuList = new ArrayList();
	public MenuTree(List menuList) {
		this.menuList=menuList;
	}

	//建立树形结构
	public List builTree(){
		List treeMenus =new  ArrayList();
		for(Menu menuNode : getRootNode()) {
			menuNode=buildChilTree(menuNode);
			treeMenus.add(menuNode);
		}
		return treeMenus;
	}

	//递归,建立子树形结构
	public Menu buildChilTree(Menu pNode){
		List chilMenus =new  ArrayList();
		for(Menu menuNode : menuList) {
			if(menuNode.getParentId().equals(pNode.getId())) {
				chilMenus.add(buildChilTree(menuNode));
			}
		}
		pNode.setChildren(chilMenus);
		return pNode;
	}

	//获取根节点(获取所有的父节点)
	public List getRootNode() {
		List rootMenuLists =new  ArrayList();
		for(Menu menuNode : menuList) {
			if(menuNode.getParentId().equals("0")) {
				rootMenuLists.add(menuNode);
			}
		}
		return rootMenuLists;
	}
}

③ 测试类

package recursion;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.List;
public class Recursion {
	public static void main(String[] args) {
		List menuList= new ArrayList();
		/*插入一些数据*/
		menuList.add(new Menu("GN001D000","0","系统管理","/admin","Y"));
		menuList.add(new Menu("GN001D100","GN001D000","权限管理","/admin","Y"));
		menuList.add(new Menu("GN001D110","GN001D100","密码修改","/admin","Y"));
		menuList.add(new Menu("GN001D120","GN001D100","新加用户","/admin","Y"));
		menuList.add(new Menu("GN001D200","GN001D000","系统监控","/admin","Y"));
		menuList.add(new Menu("GN001D210","GN001D200","在线用户","/admin","Y"));

		//让我们创建树
		MenuTree menuTree =new MenuTree(menuList);
		menuList=menuTree.builTree();
		//转为json看看效果
		String jsonOutput= JSON.toJSONString(menuList);
		System.out.println(jsonOutput);
	}
}

总结:要仔细观察,和琢磨,不能走马观花,看获取子节点时,仔细些,思考为什么要这么写,什么是递归,真正的理解递归的思想,就没有啥难度了。

感谢老哥的文章,受益匪浅。

原文链接:Java递归生成树状结构菜单

你可能感兴趣的:(数据结构与算法,java,数据结构)