zoukankan      html  css  js  c++  java
  • 递归树

    import java.util.*;
     
    public class ReadConsole1 {
         public static void main(String[] args) 
         {
             Tree t1=new Tree("01","name1","");
             Tree t2=new Tree("02","name2","01");
             Tree t3=new Tree("03","name3","01");
             Tree t4=new Tree("04","name4","02");
             Tree t5=new Tree("05","name5","02");
              
             List<Tree> list=new ArrayList<Tree>();
             list.add(t1);
             list.add(t2);
             list.add(t3);
             list.add(t4);
             list.add(t5);
             showTree(list,t1,0);
         }
         //判断字母还是数字
         public static void showTree(List<Tree> list,Tree tree,int deep)
         {
             String str="";
             for(int i=0;i<deep;i++)
             {
                 str+=" ";
             }
             System.out.println(str+""+tree.getName());
             for(int i=0;i<list.size();i++)
             {
                 if(list.get(i).getPid().equals(tree.getId()))
                 {
                     deep++;
                     showTree(list,list.get(i),deep);
                     deep--;
                 }
                  
             }
         }
    }
     
    class Tree
    {
        private String id;
        private String name;
        private String pid;
        public Tree(String id,String name,String pid)
        {
            this.id=id;
            this.name=name;
            this.pid=pid;
        }
        public String getId() {
            return id;
        }
        public String getName() {
            return name;
        }
        public String getPid() {
            return pid;
        }
         
    }
     
     
    来自:http://bbs.csdn.net/topics/320094100
     
     
     

    package tree.t1;

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

    public class ReadConsole1 {
    public static void main(String[] args) {
    Tree t1=new Tree(1, "name1", 0);
    Tree t2=new Tree(2, "name2", 1);
    Tree t3=new Tree(3, "name3", 1);
    Tree t4=new Tree(4, "name4", 2);
    Tree t5=new Tree(5, "name5", 2);
    Tree t6=new Tree(6, "name6", 0);
    Tree t7=new Tree(7, "name7", 6);
    Tree t8=new Tree(8, "name8", 6);
    Tree t9=new Tree(9, "name9", 7);
    List<Tree> list=new ArrayList<Tree>();
    list.add(t1);
    list.add(t2);
    list.add(t3);
    list.add(t4);
    list.add(t5);
    list.add(t6);
    list.add(t7);
    list.add(t8);
    list.add(t9);
    List<Tree>lists=showTree(list,t1,0);
    for (int i = 0; i <lists.size(); i++) {
    System.out.println(lists.get(i).getId()+"----"+lists.get(i).getName());
    }
    }
    static List<Tree> lists=new ArrayList<Tree>();
    public static List<Tree> showTree(List<Tree> list,Tree tree,int deep){

    String str="";
    for (int i = 0; i < deep; i++) {
    str+=" ";
    }
    lists.add(tree);
    System.out.println(str+""+tree.getName());
    for (int i = 0; i < list.size(); i++) {
    if(list.get(i).getPid().equals(tree.getId())){
    deep++;
    showTree(list,list.get(i),deep);
    deep--;
    }
    }
    return lists;
    }
    }
    class Tree{
    private Integer id;
    private String name;
    private Integer pid;

    public Tree(Integer id, String name, Integer pid) {
    super();
    this.id = id;
    this.name = name;
    this.pid = pid;
    }
    public Tree() {
    super();
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Integer getPid() {
    return pid;
    }
    public void setPid(Integer pid) {
    this.pid = pid;
    }

    }

     
     
     
     
     
     
     
     
     
  • 相关阅读:
    如何让一个图片宽度为百分比时居中,且宽度等于高度
    React创建组件的三种方式及其区别
    给电脑换源 npm 国内镜像 cnpm
    ps使用图层合并切图
    textarea去掉右下三角号
    使用定时器通过改变图片的src来切换图片
    如何使一个元素垂直居中
    如何让背景图固定,不随着滚动条移动而且在不同分辨率的屏幕下铺满
    java 中MAP的按照进入顺序遍历与无序遍历
    java树形目录展示
  • 原文地址:https://www.cnblogs.com/lanliying/p/5743467.html
Copyright © 2011-2022 走看看