TreeList格式的递归循环

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

/**
 * 
 * @author zyen
 *
 */
public class TreeList {
	static List newList=new ArrayList();
	
	//获得子节点
	public static List<TreePojo> getChild(TreePojo treeNode,List treeList){
		System.out.println("----getChild");
		List<TreePojo> childList = new ArrayList<TreePojo>();
		for(int i = 0 ;i<treeList.size();i++){
			if(treeNode.treeid == ((TreePojo)treeList.get(i)).getPid()){
				childList.add((TreePojo)treeList.get(i));
			}
		}
		return childList;
	}

	//获得根节点
	public static List<TreePojo> getRootNode(List treeList){
		System.out.println("----getRootNode");
		List<TreePojo> rootList = new ArrayList<TreePojo>();
		for(int i = 0 ;i<treeList.size();i++){
			//等于0,表示父节点
			if(((TreePojo)treeList.get(i)).getPid()==0){
				rootList.add((TreePojo)treeList.get(i));
			}
		}
		return rootList;
	}

	//向新list中装入根节点并递归子节点
	public static List setRootNode(List treeList){
		System.out.println("----setRootNode");
		List<TreePojo> rootNode = getRootNode(treeList);
		for(int i = 0 ;i<rootNode.size();i++){
			newList.add(rootNode.get(i));
			sortNode(rootNode.get(i),treeList);
		}
		
		return newList;
	}


	//递归子节点
	public static List<TreePojo> sortNode(TreePojo rootNode,List treeList){
		System.out.println("----sortNode");
		List<TreePojo> childList = getChild(rootNode,treeList);
		if(childList!=null){
			for(int i = 0;i<childList.size();i++){
				newList.add(childList.get(i));
				sortNode(childList.get(i),treeList);
			}
		}
		return childList;
	}
	
	
	//main方法调用
	public static void main(String[] args) {
		List treeList2=new ArrayList();
		treeList2.add(new TreePojo(7,5,"一级栏目1__子级2__子级1__"));
		treeList2.add(new TreePojo(1,0,"一级栏目1__"));
		treeList2.add(new TreePojo(2,0,"一级栏目2__"));
		treeList2.add(new TreePojo(3,1,"一级栏目1__子级1__"));
		treeList2.add(new TreePojo(4,0,"一级栏目3__"));
		treeList2.add(new TreePojo(5,1,"一级栏目1__子级2__"));
		treeList2.add(new TreePojo(6,1,"一级栏目1__子级3__"));
		treeList2.add(new TreePojo(8,0,"一级栏目4__"));
		treeList2.add(new TreePojo(9,2,"一级栏目2__子级1__"));
		
		List newList_=setRootNode(treeList2);
		
		//System.out.println(newList.size());
		for(int i=0;i < newList_.size();i++){
			TreePojo t=(TreePojo) newList_.get(i);
			System.out.println(t.getClassname());
		}
	}
}


Oracle递归查询
select d.treename from 
 (select dd.*  from treetable dd where dd.treetag = -1 and dd.treeuserlogid = 99    )  d
start with d.parentid = 0   
CONNECT BY prior  d.treeid=d.parentid
order   siblings   by d.levelid

treeid//树id
parentid//父id
levelid//级别

你可能感兴趣的:(oracle)