我需要从 linux 目录结构创建一棵树。例如:
. 0
./file1.txt 1
./file2.txt 2
./site 3
./site/file3.txt 4
./site/ru 5
./site/ru/file4.txt 6
每个节点都是一个目录或文件(如果它没有子节点)并且具有 ID(>=0)。根 ID 为 0。父 ID 始终低于子 ID。我创建了 2 个类:
class Node<T1, T2> {
T1 key;
T2 value;
List<Node<T1, T2>> children;
Node(T1 key, T2 value) {
this.key = key;
this.value = value;
children = new ArrayList<Node<T1, T2>>();
}
public boolean isFile()
{
return children.size() == 0;
}
}
和
public class DirectoryTree<T1 extends Comparable<T1>, T2>
{
private Node<T1, T2> root = null;
public DirectoryTree(T1 key, T2 value)
{
root = new Node<T1, T2>(key, value);
}
}
我怎样才能填满树?