一下是源代码


package com.arvon.json1.bean;

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

/**
 * 森林生成的算法
 *@author Huangwen
 *@time 2017-1-23
 */
public class TreeNode {
	
	private int id ;
	private int pid ;
	private String text;
	private List<TreeNode> children = new ArrayList<TreeNode>();
	/**最跟的父节点ID*/
	private static final int ROOT_PARENT_ID = 0;
	
	public void addChild(TreeNode node) {
		 if (children == null)
			 children = new ArrayList<TreeNode>();
		this.children.add(node);		
	}
	/**将treeNode插入到当前树合适的节点上*/
	public boolean insertChild(TreeNode treeNode) {
        if (this.id == treeNode.getPid()) {
        	addChild(treeNode);
            return true;
        } else {
            List<TreeNode> childList = this.getChildren();
            int childNumber = childList.size();
            boolean insertFlag;
            for (int i = 0; i < childNumber; i++) {
                TreeNode childNode = childList.get(i);
                insertFlag = childNode.insertChild(treeNode); //递归插入到当前树合适的节点上
                if (insertFlag == true)
                    return true;
            }
            return false;
        }
	}
	/**
	 * @param distList 目标树状的TreeNodeList
	 * @param srcList  源线性的TreeNodeList
	 */
	public static void flat2tree(List<TreeNode> distList,List<TreeNode> srcList) {
		for (int i = 0; i < srcList.size(); i++) {
			TreeNode node = srcList.get(i);
			if(node.getPid()==ROOT_PARENT_ID){
				distList.add(node);
				srcList.remove(i--);
			}else if(distList!=null && distList.size()>0){
				for (TreeNode pNode : distList) {
					boolean insertFlag = pNode.insertChild(node);
					if(insertFlag)
						srcList.remove(i--);
				}
			}
		}
		if(srcList.size()!=0){ //递归转化
			flat2tree(distList,srcList);
		}
	}
	
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getPid() {
		return pid;
	}
	public void setPid(int pid) {
		this.pid = pid;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public List<TreeNode> getChildren() {
		return children;
	}
	public void setChildren(List<TreeNode> children) {
		this.children = children;
	}
	public TreeNode(int pid, int id, String text) {
		super();
		this.pid = pid;
		this.id = id;
		this.text = text;
	}
	public TreeNode() {
		super();
	}
}



测试用例1

public static void main(String[] args) throws Exception {
		List<TreeNode> list = new ArrayList<TreeNode>();
		TreeNode t0 = new TreeNode(0,1,"A0_1");
		TreeNode t1 = new TreeNode(1,2,"A1_2");
		TreeNode t2 = new TreeNode(1,3,"A1_3");
		TreeNode t3 = new TreeNode(2,11,"A2_11");
		TreeNode t4 = new TreeNode(3,12,"A3_12");
		TreeNode t5 = new TreeNode(0,13,"A0_13");
		list.add(t0);
		list.add(t1);
		list.add(t2);
		list.add(t3);
		list.add(t4);
		list.add(t5);
		List<TreeNode> distList = new ArrayList<TreeNode>();
		TreeNode.flat2tree(distList,list);
		System.out.println(list2json(distList));
	}

打印的结果:

[{"children":[{"children":[{"children":[],"id":"11","pid":"2","text":"A2_11"}],"id":"2","pid":"1","text":"A1_2"},{"children":[{"children":[],"id":"12","pid":"3","text":"A3_12"}],"id":"3","pid":"1","text":"A1_3"}],"id":"1","pid":"0","text":"A0_1"},{"children":[],"id":"13","pid":"0","text":"A0_13"}]





Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐