我有这个问题需要解决。我必须在Java中使用树数据结构制作Windows资源管理器。
问题本身并不难,但我得到一个错误,我不知道它是什么以及如何解决它。在“删除”部分,我收到NoSuchElementException
错误,我发现getElement()
没有返回任何内容-_-。我不知道为什么会有这样的帮助。在此先感谢,这是我的代码:
import java.util.Iterator;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Iterator;
import java.util.NoSuchElementException;
public interface Tree<E> {
// //////////Accessors ////////////
public Tree.Node<E> root();
public Tree.Node<E> parent(Tree.Node<E> node);
public int childCount(Tree.Node<E> node);
// //////////Transformers ////////////
public void makeRoot(E elem);
public Tree.Node<E> addChild(Tree.Node<E> node, E elem);
public void remove(Tree.Node<E> node);
// //////////Iterator ////////////
public Iterator<E> children(Tree.Node<E> node);
// //////Inner interface for tree nodes ////////
public interface Node<E> {
public E getElement();
public void setElement(E elem);
}
}
public class SLLTree<E> implements Tree<E> {
// SLLNode is the implementation of the Node interface
class SLLNode<P> implements Node<P> {
// Holds the links to the needed nodes
SLLNode<P> parent, sibling, firstChild;
// Hold the data
P element;
public SLLNode(P o) {
element = o;
parent = sibling = firstChild = null;
}
public P getElement() {
return element;
}
public void setElement(P o) {
element = o;
}
}
protected SLLNode<E> root;
public SLLTree() {
root = null;
}
public Node<E> root() {
return root;
}
public Tree.Node<E> parent(Tree.Node<E> node) {
return ((SLLNode<E>) node).parent;
}
public int childCount(Tree.Node<E> node) {
SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
int num = 0;
while (tmp != null) {
tmp = tmp.sibling;
num++;
}
return num;
}
public void makeRoot(E elem) {
root = new SLLNode<E>(elem);
}
public Node<E> addChild(Node<E> node, E elem) {
SLLNode<E> tmp = new SLLNode<E>(elem);
SLLNode<E> curr = (SLLNode<E>) node;
tmp.sibling = curr.firstChild;
curr.firstChild = tmp;
tmp.parent = curr;
return tmp;
}
public void remove(Tree.Node<E> node) {
SLLNode<E> curr = (SLLNode<E>) node;
if (curr.parent != null) {
if (curr.parent.firstChild == curr) {
// The node is the first child of its parent
// Reconnect the parent to the next sibling
curr.parent.firstChild = curr.sibling;
} else {
// The node is not the first child of its parent
// Start from the first and search the node in the sibling list
// and remove it
SLLNode<E> tmp = curr.parent.firstChild;
while (tmp.sibling != curr) {
tmp = tmp.sibling;
}
tmp.sibling = curr.sibling;
}
} else {
root = null;
}
}
class SLLTreeIterator<T> implements Iterator<T> {
SLLNode<T> start, current;
public SLLTreeIterator(SLLNode<T> node) {
start = node;
current = node;
}
public boolean hasNext() {
return (current != null);
}
public T next() throws NoSuchElementException {
if (current != null) {
SLLNode<T> tmp = current;
current = current.sibling;
return tmp.getElement();
} else {
throw new NoSuchElementException();
}
}
public void remove() {
if (current != null) {
current = current.sibling;
}
}
}
public Iterator<E> children(Tree.Node<E> node) {
return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
}
void printTreeRecursive(Node<E> node, int level) {
if (node == null)
return;
int i;
SLLNode<E> tmp;
for (i = 0; i < level; i++)
System.out.print(" ");
System.out.println(node.getElement().toString());
tmp = ((SLLNode<E>) node).firstChild;
while (tmp != null) {
printTreeRecursive(tmp, level + 1);
tmp = tmp.sibling;
}
}
public void printTree() {
printTreeRecursive(root, 0);
}
public int countMaxChildren() {
return countMaxChildrenRecursive(root);
}
int countMaxChildrenRecursive(SLLNode<E> node) {
int t = childCount(node);
SLLNode<E> tmp = node.firstChild;
while (tmp != null) {
t = Math.max(t, countMaxChildrenRecursive(tmp));
tmp = tmp.sibling;
}
return t;
}
}
public class WindowsExplorer {
public static void main(String[] args) throws IOException {
// TODO code application logic here
int i;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String commands[] = new String[N];
for (i=0;i<N;i++)
commands[i] = br.readLine();
br.close();
SLLTree<String> tree = new SLLTree<String>();
tree.makeRoot("c:");
// vasiot kod ovde...
int res = 0;
int brJazli = 0;
int stuff = 0;
Tree.Node<String> tekoven = null;
Tree.Node<String>[] jazli = new Tree.Node[100];
Tree.Node<String> proverka = null;
Tree.Node<String> proverkaa = null;
String[] path = new String[100];
for(i=0; i<N; i++){
String[] pom = commands[i].split(" ");
if(pom[0].equals("CREATE")){
if(res == 0){
Tree.Node<String> a = null;
tekoven = tree.root;
brJazli++;
a = tree.addChild(tekoven, pom[1]);
jazli[brJazli] = a;
} else if(res != 0){
Tree.Node<String> a = null;
a = tree.addChild(tekoven, pom[1]);
brJazli++;
}
}
else if(pom[0].equals("OPEN")){ // Open
res++;
for(int j=0; j < brJazli; j++){
if(jazli[j].getElement().toString().equals(pom[1])){
tekoven = jazli[j];
}
}
}
else if(pom[0].equals("DELETE")){ // Delete
for(int j=0; j < brJazli; j++){
if(jazli[j].getElement().toString().equals(pom[1]))
tree.remove(jazli[j]);
}
}
else if(pom[0].equals("BACK")){ // Back
for(int j=0; j < brJazli; j++){
if(jazli[j].toString().equals(pom[1]))
tekoven = tree.parent(jazli[j]);
}
}
else if(pom[0].equals("PATH")){
for(int j=0; j < 15; j++){
proverkaa = tekoven;
proverka = tree.parent(proverkaa);
if(proverka.toString().equals("c:")){
System.out.print("c:");
if(stuff == 0) break;
else {
for(int k=1; k < stuff; k++)
System.out.print("\"" + path[stuff] + "\"");
}
}
else {
path[j+1] = proverka.toString();
proverkaa = proverka;
stuff++;
}
}
}
else if(pom[0].equals("PRINT")){
tree.printTree();
}
}
}
}