将List转成树的两种方式(递归、循环)

在做树结构搜索到一篇文章就拿来用了,作者写得比较通俗易懂,一看就知道算法,粘贴过来,分享分享!原文地址:http://blog.csdn.net/massivestars/article/details/53911620 在做目录树的时候通常是将一个目录存在数据库的List全部返回来,再根据节点id和parentId组装成一颗树。这里切忌使用递归查询数据库的方式实现,应把对应的目录数据全部查询回来再进行组装。List转成Tree有两种方式,一种是常用的递归,一种是双层循环。

TreeNode.java

/**
 *  佛曰:
 *       写字楼里写字间,写字间里程序员;
 *       程序人员写程序,又拿程序换酒钱。
 *       酒醒只在网上坐,酒醉还来网下眠;
 *       酒醉酒醒日复日,网上网下年复年。
 *       但愿老死电脑间,不愿鞠躬老板前;
 *       奔驰宝马贵者趣,公交自行程序员。
 *       别人笑我忒疯癫,我笑自己命太贱;
 *       不见满街漂亮妹,哪个归得程序员?
 * @author xiaobai  
 * mail:sky-xiaobai@qq.com   blog:itsayer.com
 * 2017年7月19日
 * 
 * TODO()
 */

/**
 * @author xiaobai
 * 2017年7月19日
 * @todo( )
 */

import java.util.ArrayList;  
import java.util.List; 
class TreeNode {
	private String id;
    private String parentId;  
  
    private String name;  
  
    private List<TreeNode> children;  
  
    public TreeNode(String id, String name, String parentId) {  
        this.id = id;  
        this.parentId = parentId;  
        this.name = name;  
    }  
    public TreeNode(String id, String name, TreeNode parent) {  
        this.id = id;  
        this.parentId = parent.getId();  
        this.name = name;  
    }  
  
  
    public String getParentId() {  
        return parentId;  
    }  
  
    public void setParentId(String parentId) {  
        this.parentId = parentId;  
    }  
  
    public String getName() {  
        return name;  
    }  
  
    public void setName(String name) {  
        this.name = name;  
    }  
  
    public String getId() {  
        return id;  
    }  
  
    public void setId(String id) {  
        this.id = id;  
    }  
    
    public List<TreeNode> getChildren() {  
        return children;  
    }  
  
    public void setChildren(List<TreeNode> children) {  
        this.children = children;  
    }  
  
    @Override  
    public String toString() {  
        return "TreeNode{" +  
                "id='" + id + '\'' +  
                ", parentId='" + parentId + '\'' +  
                ", name='" + name + '\'' +  
                ", children=" + children +  
                '}';  
    }  
  
}

TreeBuilder.java

import java.util.ArrayList;
import java.util.List;

/**
 *  佛曰:
 *       写字楼里写字间,写字间里程序员;
 *       程序人员写程序,又拿程序换酒钱。
 *       酒醒只在网上坐,酒醉还来网下眠;
 *       酒醉酒醒日复日,网上网下年复年。
 *       但愿老死电脑间,不愿鞠躬老板前;
 *       奔驰宝马贵者趣,公交自行程序员。
 *       别人笑我忒疯癫,我笑自己命太贱;
 *       不见满街漂亮妹,哪个归得程序员?
 * @author xiaobai  
 * mail:sky-xiaobai@qq.com   blog:itsayer.com
 * 2017年7月19日
 * 
 * TODO()
 */

/**
 * @author xiaobai
 * 2017年7月19日
 * @todo( )
 */
public class TreeBuilder {
	  
    /** 
     * 两层循环实现建树 
     * @param treeNodes 传入的树节点列表 
     * @return 
     */  
    public static List<TreeNode> bulid(List<TreeNode> treeNodes) {  
  
        List<TreeNode> trees = new ArrayList<TreeNode>();  
  
        for (TreeNode treeNode : treeNodes) {  
  
            if ("0".equals(treeNode.getParentId())) {  
                trees.add(treeNode);  
            }  
  
            for (TreeNode it : treeNodes) {  
                if (it.getParentId() == treeNode.getId()) {  
                    if (treeNode.getChildren() == null) {  
                        treeNode.setChildren(new ArrayList<TreeNode>());  
                    }  
                    treeNode.getChildren().add(it);  
                }  
            }  
        }  
        return trees;  
    }  
  
    /** 
     * 使用递归方法建树 
     * @param treeNodes 
     * @return 
     */  
    public static List<TreeNode> buildByRecursive(List<TreeNode> treeNodes) {  
        List<TreeNode> trees = new ArrayList<TreeNode>();  
        for (TreeNode treeNode : treeNodes) {  
            if ("0".equals(treeNode.getParentId())) {  
                trees.add(findChildren(treeNode,treeNodes));  
            }  
        }  
        return trees;  
    }  
  
    /** 
     * 递归查找子节点 
     * @param treeNodes 
     * @return 
     */  
    public static TreeNode findChildren(TreeNode treeNode,List<TreeNode> treeNodes) {  
        for (TreeNode it : treeNodes) {  
            if(treeNode.getId().equals(it.getParentId())) {  
                if (treeNode.getChildren() == null) {  
                    treeNode.setChildren(new ArrayList<TreeNode>());  
                }  
                treeNode.getChildren().add(findChildren(it,treeNodes));  
            }  
        }  
        return treeNode;  
    }  
  
  
  
    public static void main(String[] args) {  
  
        TreeNode treeNode1 = new TreeNode("1","广州","0");  
        TreeNode treeNode2 = new TreeNode("2","深圳","0");  
  
        TreeNode treeNode3 = new TreeNode("3","天河区",treeNode1);  
        TreeNode treeNode4 = new TreeNode("4","越秀区",treeNode1);  
        TreeNode treeNode5 = new TreeNode("5","黄埔区",treeNode1);  
        TreeNode treeNode6 = new TreeNode("6","石牌",treeNode3);  
        TreeNode treeNode7 = new TreeNode("7","百脑汇",treeNode6);  
  
  
        TreeNode treeNode8 = new TreeNode("8","南山区",treeNode2);  
        TreeNode treeNode9 = new TreeNode("9","宝安区",treeNode2);  
        TreeNode treeNode10 = new TreeNode("10","科技园",treeNode8);  
  
  
        List<TreeNode> list = new ArrayList<TreeNode>();  
  
        list.add(treeNode1);  
        list.add(treeNode2);  
        list.add(treeNode3);  
        list.add(treeNode4);  
        list.add(treeNode5);  
        list.add(treeNode6);  
        list.add(treeNode7);  
        list.add(treeNode8);  
        list.add(treeNode9);  
        list.add(treeNode10);  
  
        List<TreeNode> trees = TreeBuilder.bulid(list);  
  
        List<TreeNode> trees_ = TreeBuilder.buildByRecursive(list);  
  
        System.out.print( trees.toString() );
        
    }  
}