where the amazing happens

算法1 : 递归

一个简单的递归程序,它读取给定目录下所有文件,然后以树型的方式在屏幕上打印出来.昨天断断续续得花了1个多小时写出来

读取文件

package  test;
import  java.io. * ;
import  java.util. * ;

/**
 *   Put files into a tree
 * 
*/

public   class  ReadFilesAndBuiltTree  {
    
    
static   class  Node {
        
private  Node father;
        
private  List children;
        
private  File data;
        
public  Node() {
            children 
=   new  Vector();
        }

        
public  File getData()  {
            
return  data;
        }

        
public   void  addChild(Node c) {
            
this .getChildren().add(c);
        }

        
public   void  setData(File data)  {
            
this .data  =  data;
        }

        
public  List getChildren()  {
            
return  children;
        }

        
public   void  setChildren(List children)  {
            
this .children  =  children;
        }

        
public  Node getFather()  {
            
return  father;
        }

        
public   void  setFather(Node father)  {
            
this .father  =  father;
        }

        
public   boolean  hasChildren() {
            
return  children.size() > 0 ;
        }

        
public  Node getChile( int  index) {
            
return  (Node)children.get(index);
        }

        
public  Node getChild(String fname) {
            Iterator i 
=  getChildren().iterator();
            
while (i.hasNext()) {
                Node bn 
=  (Node)i.next();
                String n 
=  
                    bn.getData().getName();
                
if (n.equals(fname)) return  bn;
            }

            
return   null ;
        }

        
public  List getDirChildren() {
            List c 
=   new  Vector();
            
for ( int  i  =   0  ; i  < children.size();i ++ ) {
                Node n 
=  (Node)children.get(i);
                
if (
                  n.getData().isDirectory()        
                )c.add(n);
            }

            
return  c;
        }

        
public  List getFileChildren() {
            List c 
=   new  Vector();
            
for ( int  i  =   0  ; i  < children.size();i ++ ) {
                Node n 
=  (Node)children.get(i);
                
if (
                  
! n.getData().isDirectory()        
                )c.add(n);
            }

            
return  c;
        }

    }

    
    
private  ReadFilesAndBuiltTree() {}
    
    
public   static  Node getTree(File rootDir) {
        Node r 
=   new  Node();
        r.setData(rootDir);
        
return   new  ReadFilesAndBuiltTree().builtTree(r);
    }

    
    
private  Node builtTree(Node root) {
        
if ( ! root.getData().isDirectory()) return  root;
        File f 
=  root.getData();
        File[] fs 
=  f.listFiles();
        
for ( int  i  =   0  ; i  <  fs.length ; i ++  ) {
            File ff 
=  fs[i];
            Node n 
=   new  Node();
            n.setData(ff);
            n.setFather(root);
            root.addChild(builtTree(n));
        }

        
return  root;
    }

    
    
public   static   void  main(String[] args) throws  Exception {
        Node root 
=  getTree( new  File(ShowFileDir.ROOT_DIR));
        Node cn 
=  root.getChild( " maps " );
        System.out.println(cn.getChildren().size());
    }

    
}


输出文件
package test;
import java.io.File;
import java.util.Iterator;
import java.util.List;

import test.ReadFilesAndBuiltTree.Node;

/**
 *  Print the tree to screen
 * 
*/

public class ShowFileDir {
    
    
public static String ROOT_DIR = "E:/y音乐";
    
    
public ShowFileDir(){
    }

    
    
public void printNodes(){
       Node root 
= ReadFilesAndBuiltTree.getTree(new File(ROOT_DIR));
       printNode(root,
0);
    }

    
    
private void printNode(Node root,int depth){
        
if(!root.hasChildren())return;
        pd(root.getData().getName(),depth);
        pindent(
++depth);
        List l 
= root.getFileChildren();
        pc(l,depth);
        l 
= root.getDirChildren();
        
for(int i = 0 ; i < l.size() ; i++){
            Node c 
= (Node)l.get(i);
            printNode(c,depth);
        }

    }

    
// Screen methods    
    private void pc(List l,int count){
        Iterator i 
= l.iterator();
        
while(i.hasNext()){
            Node n 
= (Node)i.next();
            pp(count);
            p(
"+"+n.getData().getName()+"\n");
        }

    }

    
    
private void pd(String t,int depth){
        String spc 
= "";
        
for(int i = 0; i< depth; i++)spc+="-";
        p(spc
+"+"+t+"\n");
    }

    
    
private void pindent(int depth){
        String spc 
= "";
        
for(int i = 0; i< depth; i++)spc+=" ";
        System.out.println(spc
+"|");
    }

    
    
private void pp(int count){
        
for(int i = 0 ; i < count ; i++)p(" ");
    }

    
    
private void p(String s){System.out.print(s);}
    
    
public static void main(String[] args)throws Exception{
        ShowFileDir sfd 
= new ShowFileDir();
        sfd.printNodes();
    }

}



2个类,一个负责读.一个负责写.
输出结果:

+y音乐
 |
-+欧美
  |
  +WeAreTheWorld.txt
  +WeAreTheWorld.wmv
  +WhereAreYou!.txt
  +jeanny_3.mp3
  +blood-upon-the-risers.mp3
  +Broken Down Angel.txt
  +Love Hurts.txt
  +Broken Down Angle.mp3
  +Love Hurts.mp3
  +Celien Dion - God Bless America.mp3
  +SS闪电部队在前进.mp3
  +The_Sun_Is_Burning_Simon_and_Garfunkel.mp3
  +vitas.-.opera02.rm
  +chivas we could be together.wma
  +绿色椅子.mp3
  +come away with me-norah jones.wma
  +the sound of silence.mp3
--+live from new york city,1967-simon&garfunkel
   |
   +a church is burning.mp3
   +a hazy shade of winter.mp3
   +a most peculiar.mp3
   +a poem on the underground wall.mp3
   +anji.mp3
   +benedictus.mp3
   +blessed.mp3
   +for emily,whenever I may find her.mp3
   +he was my brother.mp3
   +howeward bound.mp3
   +I am a rock.mp3
   +leaves that are green.mp3
   +richard cory.mp3
   +sparrow.mp3
   +the 59th street bridge song.mp3
   +the dangling conversation.mp3
   +the sound of silence.mp3
   +wednesday morning,3A.M.mp3
   +you don't know where your interest lies.mp3
-+其他
  |
  +StarCraft - Techno Mix.mp3
  +牵手.mp3
  +萤火虫 .mp3
  +njsw.mp3
  +zeldaN64.rar


这是算法系列的第一篇,下一个准备写动态规划的.

posted on 2006-04-23 05:00 where the amazing happens 阅读(879) 评论(1)  编辑  收藏 所属分类: 算法&数据结构

评论

# re: 算法1 : 递归[未登录] 2008-01-20 13:10 无名

private ReadFilesAndBuiltTree() {}
你这里为什么要用private来定义构造函数呢  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 

公告

点击这里给我发消息

导航

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

统计

常用链接

留言簿(3)

随笔分类(18)

随笔档案(17)

文章分类

相册

其他我的blog

技术Blog

最新随笔

搜索

最新评论

阅读排行榜

评论排行榜