id       node      path                      pid
------------------------------------------------------------
1 四川 四川                   0
2 济南 济南                   0
3 山东省 济南.山东省          2
4 绵阳 济南.山东省.绵阳          3
5 绵阳555 济南.山东省.绵阳.绵阳555 4
6 绵阳666 济南.山东省.绵阳.绵阳666 4
7 青岛 济南.山东省.青岛          3
8 青岛333 济南.山东省.青岛.青岛333 7
9 青岛444 济南.山东省.青岛.青岛444 7
======================================================================
求 该表的 树形展示  代码实现..    树上就挂node 就行了. 

解决方案 »

  1.   

    只会Web版的
    ExtJs的Treepanel可以tee.expandAll();
    控制台的估计得自己写 可能还有回溯算法
      

  2.   

    效率有问题,不行再改public class Test{
    public static void main(String args[]){
    //init data
    TreeDto treeData[] = new TreeDto[9] ;
    treeData[0] = new TreeDto("1","四川","四川","0");
    treeData[1] = new TreeDto("2","济南","济南","0");
    treeData[2] = new TreeDto("3","山东省","济南.山东省","2");
    treeData[3] = new TreeDto("4","绵阳","济南.山东省.绵阳","3");
    treeData[4] = new TreeDto("5","绵阳555","济南.山东省.绵阳.绵阳555","4");
    treeData[5] = new TreeDto("6","绵阳666","济南.山东省.绵阳.绵阳666","4");
    treeData[6] = new TreeDto("7","青岛","济南.山东省.青岛","3");
    treeData[7] = new TreeDto("8","青岛333","济南.山东省.青岛.青岛333","7");
    treeData[8] = new TreeDto("9","青岛444","济南.山东省.青岛.青岛444","7");

    //add data
    TreeNode tRoot = new TreeNode(new TreeDto("0","root","name","-1")) ;
    int size = treeData.length ;
    for(int i = 0 ; i < size ; i ++){
    tRoot.AddChildren(new TreeNode(treeData[i])) ;
    }

    //show data
    showTree(tRoot , 0) ;
    }

    public static void showTree(TreeNode root , int tabCount){
    for(int i = 0 ; i <= tabCount ; i++){
    System.out.print("    ");
    }
    System.out.println(root.getName());

    for(int i = 0 ; i <root.getChildren().size() ; i ++){
    showTree(root.getChildren().get(i) , tabCount+1) ;
    }
    }
    }class TreeDto{
    private String name ;
    private String path ;
    private String id ;
    private String pId ;

    public TreeDto(String id , String name , String path , String pId){
    this.setId(id) ;
    this.setName(name) ;
    this.setPath(path) ;
    this.setpId(pId) ;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public String getPath() {
    return path;
    }
    public void setPath(String path) {
    this.path = path;
    }
    public String getId() {
    return id;
    }
    public void setId(String id) {
    this.id = id;
    }
    public String getpId() {
    return pId;
    }
    public void setpId(String pId) {
    this.pId = pId;
    }
    }class TreeNode{
    private ArrayList<TreeNode> childrenNodes = new ArrayList<TreeNode>() ;
    private TreeDto thisNode = null ; 

    public ArrayList<TreeNode> getChildren(){
    return this.childrenNodes ;
    }
    public String getId(){
    return this.thisNode.getId() ;
    } public String getPId(){
    return this.thisNode.getpId() ;
    }

    public String getName(){
    return this.thisNode.getName() ;
    }

    public TreeNode(TreeDto t){
    this.thisNode = t ;
    }

    /**
     * 添加子结点,如果本结点下没有添加结点的父结点,则不添加
     * @param t
     */
    public void AddChildren(TreeNode t){
    if(this.getId().equals(t.getPId())){
    this.childrenNodes.add(t) ;
    return ;
    }

    int size = this.childrenNodes.size();
    for(int i = 0 ; i < size ; i++){
    childrenNodes.get(i).AddChildren(t);
    }
    }
    }