Java 文件系统实现,通过二进制搜索树列出文件路径

Java 文件系统实现,通过二进制搜索树列出文件路径,java,Java,我正在处理一个文件系统实现,我的二进制搜索树locate方法遇到了问题。现在我的结构是这样的。它将添加额外的文件或其他我无法理解的东西 例如,我有一棵树: / / \ a b / / \ c h q / x 如果用户输入locate h,我的程序将输出/acxh。 当用户输入locate h时,输出应为/bh 这是我的文件系统类: import java.io.IOException; import java.util.ArrayLis

我正在处理一个文件系统实现,我的二进制搜索树
locate
方法遇到了问题。现在我的结构是这样的。它将添加额外的文件或其他我无法理解的东西

例如,我有一棵树:

      /
     / \
    a   b
   /   / \
  c   h   q
 /
x
如果用户输入
locate h
,我的程序将输出
/acxh
。 当用户输入
locate h
时,输出应为
/bh

这是我的
文件系统
类:

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);
}

}
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;
}

}
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);
}

}
这是我的
BinarySearchTree
类:

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);
}

}
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;
}

}
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);
}

}
import java.util.ArrayList;
公共类BinarySearchTree扩展了BinaryTree{
私有布尔返回;
私人电子商务;
布尔添加(E项){
根=添加(根,项);
返回addReturn;
}
专用节点添加(节点n、E项){
如果(n==null){
addReturn=true;
返回新节点(项目);
}else if(项比较(n.数据)==0){
addReturn=false;
返回n;
}否则如果(项目比较(n.数据)<0){
n、 leftChild=添加(n.leftChild,项);
返回n;
}否则{
n、 rightChild=添加(n.rightChild,项目);
返回n;
}
}
公共电子查找(电子目标){
返回find(目标、根);
}
专用E查找(E目标、节点){
if(node==null){
返回null;
}
int result=target.compareTo(node.data);
如果(结果==0){
返回node.data;
}
如果(结果<0){
返回find(target,node.leftChild);
}
返回find(target,node.rightChild);
}
公共字符串locateHelper(条目e、节点节点、ArrayList路径){
if(node==null){
返回null;
}
int result=e.compareTo(node.data);
如果(结果==0){
add(node.data.word);
返回node.data.word;
}
如果(结果<0){
add(node.data.word);
返回locateHelper(e,node.leftChild,path);
}
add(node.data.word);
返回locateHelper(e,node.rightChild,path);
}
}

我很抱歉在文章中有很多代码,我知道我应该尽量少用,但我相信你至少需要看看这3个类来提供一些帮助。如果还需要什么,请告诉我,我会编辑这篇文章来提供。

提示:独立于文件系统模拟器测试二进制搜索树,以确定是哪一个导致了问题。此外,您还应该告诉我们您期望的行为以及实际得到的结果。二元搜索树应该将具有“较低”值的节点作为左子节点,而具有“较高”值的节点作为右子节点,但您的示例似乎没有显示这一点。如何使输入具有较低的值?我应该按路径输入文件,而不是通过存储树中所有文件的
文件
数组列表执行
for
循环@GerardoFigueroa和@JeffreyBosboom如果用户输入
locate h
,我的程序将输出
/acxh
,而不是正确的输出,即
/bh
,我的文章顶部有一个树示例。提示:独立于文件系统模拟器测试二进制搜索树,以确定是哪个树导致了问题。此外,您还应该告诉我们您期望的行为以及实际得到的结果。二元搜索树应该将具有“较低”值的节点作为左子节点,而具有“较高”值的节点作为右子节点,但您的示例似乎没有显示这一点。如何使输入具有较低的值?我应该按路径输入文件,而不是通过存储树中所有文件的
文件
数组列表执行
for
循环@GerardoFigueroa和@JeffreyBosboom如果用户输入
locate h
,我的程序将输出
/acxh
,而不是正确的输出,即
/bh
,我的文章顶部有一个树示例。