我创建了一个基于根节点的简单参数化树结构。每个节点都拥有其子注释的数组列表以及指向其父代的链接。这很简单。Java树结构和广度优先搜索
现在,我没有能够解决(在不久的将来)一个问题:
我想写在这个树广度优先搜索。我想在(实现的)迭代器接口的帮助下实现此功能。但我真的无法把这个工作: 问题是也迭代列表结构。我不知道如何实现hasNext(),next()和remove()函数:/
你有什么想法吗?
问候
代码:
public class Tree<T> implements Iterator<T>{
private Node<T> root;
/**
* Default constructor.
*/
public Tree() {
super();
}
/**
* Return the root Node of the tree.
* @return the root element.
*/
public Node<T> getRoot() {
return this.root;
}
/**
* Set the root Element for the tree.
* @param Root the root element to set.
*/
public void setRoot(Node<T> Root) {
this.root = Root;
}
...
和
公共类节点{
public List<Node<T>> children;
public Node<T> parent;
public T data;
/**
* Default constructor.
*/
public Node() {
super();
}
/**
* Create a Node<T> with an instance of T.
* @param data an instance of T.
*/
public Node(T nodeData) {
this();
setData(nodeData);
}
/**
* Create a Node<T> with an instance of T.
* @param data an instance of T.
*/
public Node(T nodeData, Node<T> parentNode) {
this();
setData(nodeData);
setParent(parentNode);
}
...
树不应该执行' Iterator',它应该实现'Iterable '。迭代器应该是一个单独的类,它的一个实例由Iterable返回。iterator()'方法。 –
2012-02-08 14:17:35