I created a simple parameterized tree structure, based on a root Node. Each node holds a arraylist of its childnotes and a link to its parent. Thats simple.
Now a problem that I'm not able to solve (in near future):
I want to write a breadth-first search over this tree. I want to implement this feature with the help of a (implemented) iterator interface. But I'm really unable to bring this to work: Problem is the also iterable List-structure. I don't know how to implement the hasNext(), next() and remove() functions :/
Do you have any idea?
Greetings
Code:
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;
}
...
and
public class Node {
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<T>, it should implementIterable<T>. The iterator then should be a separate class, an instance of which is returned by theIterable.iterator()method.