我正在进行FileSystem实施,而且我的二进制搜索树locate
方法出现问题。现在我的结构方式。它会添加其他文件或其他我无法弄清楚的内容。
举个例子,我有树:
/
/ \
a b
/ / \
c h q
/
x
如果用户输入locate h
,我的程序将输出/acxh
。
当用户输入locate h
时,输出应该为/bh
这是我的FileSystem
课程:
import java.io.IOException;
import java.util.ArrayList;
public class FileSystem {
private Directory root;
private Directory wDir;
private ArrayList<File> files = new ArrayList<File>();
// Constructor
public FileSystem() {
}
// Constructor with parameters
public FileSystem(Directory root) {
this.root = root;
wDir = root;
files.add(root);
}
// Returns the FileSystem's files
public ArrayList<File> getFiles() {
return files;
}
// Returns the working directory
public Directory getWDir() {
return wDir;
}
// Sets the working directory
public void setWDir(Directory d) {
wDir = d;
}
// Returns the root file. This will always be / in our program
public File getRoot() {
return root;
}
public File getFile(File f, String name) {
if (f.isDirectory()) {
for (File c : ((Directory) f).getChildren()) {
if (c.getName().equals(name))
return c;
}
}
return null;
}
// Currently only used in cat method, getFile is better
File findFile(File f, String name) {
if (f.getName().equals(name))
return f;
File file = null;
if (f.isDirectory()) {
for (File c : ((Directory) f).getChildren()) {
file = findFile(c, name);
if (file != null)
break;
}
}
return file;
}
// Returns true if file is found
boolean isFile(String name) {
File file = null;
file = getFile(wDir, name);
if (file != null) {
return true;
}
return false;
}
// Creates Directory
public void mkdir(String path) {
files.add(new Directory(path));
int size = files.size();
// Sets the parent
files.get(size - 1).setParent(wDir);
// Sets the child
wDir.addChild(files.get(size - 1));
}
// Changes working directory
public void cd(String s) {
if (s.equals("..")) {
if (wDir != root) {
wDir = wDir.getParent();
}
} else if (s.equals("/")) {
wDir = root;
} else {
wDir = (Directory) getFile(wDir, s);
}
}
// Provides absolute filename
public void pwd() {
if (wDir == root) {
System.out.println("/");
} else {
System.out.println(wDir.getPath());
}
}
// Lists children of current working directory
public void ls() {
ArrayList<File> children = wDir.getChildren();
if (children != null) {
for (int i = 0; i < children.size(); i++) {
String childName = children.get(i).getName();
System.out.print(childName + " ");
}
}
}
// Lists children of file(s) inputted by user
public void ls(File f) {
if (f instanceof TextFile) {
System.out.println(f.getPath());
} else {
ArrayList<File> children = ((Directory) f).getChildren();
if (children != null) {
for (int i = 0; i < children.size(); i++) {
String childName = children.get(i).getName();
System.out.print(childName + " ");
}
}
}
}
public void recLS(File f, String location) {
System.out.println(location + ":");
ls(f);
System.out.println("");
if (f.isDirectory()) {
ArrayList<File> children = ((Directory) f).getChildren();
for (File c : children) {
location += "/" + c.getName();
}
for (File c : children) {
recLS(c, location);
}
}
}
// Creates a TextFile or edit's TextFile's content if already exists in the
// tree
public void edit(String name, String content) {
files.add(new TextFile(name, content));
// Setting TextFile parent
files.get(files.size() - 1).setParent(wDir);
// Setting Parent's child
wDir.addChild(files.get(files.size() - 1));
}
// Prints the content of TextFile
public void cat(String name) {
File f = findFile(root, name);
System.out.println(((TextFile) f).getContent());
}
public void updatedb(Indexer i) throws IOException {
i.index(files);
}
public String locate(String s, Indexer i) {
return i.locate(s);
}
}
这是我的Indexer
课程:
import java.io.*;
import java.util.*;
class Entry implements Comparable<Entry> {
String word;
ArrayList<Integer> page = new ArrayList<Integer>();
Entry(String word) {
this.word = word;
}
public int compareTo(Entry e) {
return word.compareTo(e.word);
}
}
class Indexer {
private BinarySearchTree<Entry> bst;
public void index(ArrayList<File> files) throws IOException {
bst = new BinarySearchTree<Entry>();
int fileCount = 1;
for (int i = 0; i < files.size(); i++) {
String name = files.get(i).getName();
indexName(name, fileCount++);
}
}
private void indexName(String name, int fileCount) {
Entry e = new Entry(name);
Entry r = bst.find(e);
if (r != null) {
r.page.add(fileCount);
} else {
e.page.add(fileCount);
bst.add(e);
}
}
public String locate(String s) {
Entry e = new Entry(s);
ArrayList<String> path = new ArrayList<String>();
bst.locateHelper(e, bst.root, path);
String word = "";
for (int i = 0; i < path.size(); i++) {
word += path.get(i);
}
return word;
}
}
这是我的BinarySearchTree
课程:
import java.util.ArrayList;
public class BinarySearchTree<E extends Comparable> extends BinaryTree<E> {
private boolean addReturn;
private E deletedItem;
boolean add(E item) {
root = add(root, item);
return addReturn;
}
private Node<E> add(Node<E> n, E item) {
if (n == null) {
addReturn = true;
return new Node<E>(item);
} else if (item.compareTo(n.data) == 0) {
addReturn = false;
return n;
} else if (item.compareTo(n.data) < 0) {
n.leftChild = add(n.leftChild, item);
return n;
} else {
n.rightChild = add(n.rightChild, item);
return n;
}
}
public E find(E target) {
return find(target, root);
}
private E find(E target, Node<E> node) {
if (node == null) {
return null;
}
int result = target.compareTo(node.data);
if (result == 0) {
return node.data;
}
if (result < 0) {
return find(target, node.leftChild);
}
return find(target, node.rightChild);
}
public String locateHelper(Entry e, Node<Entry> node, ArrayList<String> path) {
if (node == null) {
return null;
}
int result = e.compareTo(node.data);
if (result == 0) {
path.add(node.data.word);
return node.data.word;
}
if (result < 0) {
path.add(node.data.word);
return locateHelper(e, node.leftChild, path);
}
path.add(node.data.word);
return locateHelper(e, node.rightChild, path);
}
}
我很抱歉在帖子中有很多代码,我知道我应该保持最低限度,但我相信你至少需要看看这三个类来提供一些帮助。如果需要其他任何内容,请告诉我,我会编辑此帖子以提供它。