JAVA用树结构实现目录系统

来源:互联网 发布:windows打印服务器 编辑:程序博客网 时间:2024/06/06 02:11

1使用 第一个儿子/下一兄弟表示法 来表示树

树节点定义如下:

private class TreeNode    {        String data;        TreeNode firstChild;        TreeNode nextSibling;        public TreeNode(String data, TreeNode  firstChild,                TreeNode nextSibling) {            super();            this.data = data;            this.firstChild = firstChild;            this.nextSibling = nextSibling;        }    }

如下图所示:
这是一棵树
这是一棵树
这是 上图所表示的树的第一儿子/下一兄弟表示法
这是 上图所表示的树的第一儿子/下一兄弟表示法

如何读取指定文件夹的所有文件名,可以参考这篇文章中的方法
http://blog.csdn.net/aa8568849/article/details/52670133
在此基础上加入文件名存储进树,和输出树中的内容
程序效果如下:
这里写图片描述
代码如下:

import java.io.File;/* *  *  */public class P78_tree//存储指定文件夹所有文件名的 树类{    private TreeNode root=new TreeNode(null, null, null);//树根(相当于链表的头指针)    public TreeNode getRoot()//获取树根    {        return root;    }    /*     * 函数名:getFile     * 作用:实现将指定文件夹的所有文件存入树中     */    public void getFile(String path,TreeNode treeNode)    {        File file=new File(path);        File[] array=file.listFiles();        TreeNode newNode=null;        for(int i=0;i<array.length;i++)        {            newNode=new TreeNode(array[i].getName(),null, null);            //判断当前节点有没有firstChild,没有的话为其添加一个            if (treeNode.firstChild==null)             {                if (array[i].isFile())                    treeNode.firstChild=newNode;                if (array[i].isDirectory())                 {                    treeNode.firstChild=newNode;                    getFile(array[i].getPath(), newNode);                }            }            //当前节点已经存在firstChild,所以后面的都是firstChild节点的兄弟            else            {                TreeNode p=treeNode.firstChild;                while(p.nextSibling!=null)                    p=p.nextSibling;                if (array[i].isFile())                    p.nextSibling=newNode;                if (array[i].isDirectory())                 {                    p.nextSibling=newNode;                    getFile(array[i].getPath(), newNode);                }            }           }    }    /*     * 函数名:printTree     * 作用:输出树中的内容     */    public void printTree(TreeNode root,int deep)    {        if (root.data!=null) //这个if是为了区分root节点,因为root节点data=null        {            for (int i = 0; i < deep; i++)//输出前置空格                System.out.print("  ");            System.out.println(root.data);        }        //画一下图,就能理解这个两个if        if (root.firstChild!=null)             printTree(root.firstChild, deep+1);        if (root.nextSibling!=null)            printTree(root.nextSibling, deep);    }    //树节点两个指针,一个指向第一个儿子 一个指向兄弟    private class TreeNode//树节点类    {        String data;        TreeNode firstChild;//第一儿子链        TreeNode nextSibling;//下一兄弟链        //构造器        public TreeNode(String data, TreeNode  firstChild,TreeNode nextSibling)        {            super();            this.data = data;            this.firstChild = firstChild;            this.nextSibling = nextSibling;        }    }    public static void main(String[] args)     {        P78_tree test=new P78_tree();        test.getFile("/Users/XXY/Desktop/test",test.getRoot());        test.printTree(test.root, 0);    }}
0 0
原创粉丝点击