I as trying to implement the Iterator interface for tree traversal. I am getting the following error."Incompatible types at for(Integer node : tr )" and "treeIterator.java uses unchecked or unsafe operations." I am unable to fix this error. Can someone point out the problem.
//class to implement the Iterator interace.
class InorderItr implements Iterator {
public InorderItr(Node root) {
st = new Stack<Node>();
this.root = root;
}
@Override
public boolean hasNext() {
//has Next
}
@Override
public Integer next(){
//next node
}
@Override
public void remove(){
throw new java.lang.UnsupportedOperationException("Remove not supported.");
}
}
//This class just makes sure that we use the foreach loop.
class InorderTreeIterator implements Iterable {
Node root = null;
public InorderTreeIterator(Node root){
this.root = root;
}
@Override
public Iterator<Integer> iterator(){
try{
return new InorderItr(this.root);
} catch(UnsupportedOperationException e){
System.out.println(e.getMessage());
return null;
}
}
}
class treeIterator {
public static void main(String arg[]){
treeIterator obj = new treeIterator();
//create tree.
InorderTreeIterator tr = new InorderTreeIterator(obj.root);
for(Integer node : tr ){
System.out.println(node);
}
}
}
PS: This is my first try at implementing iterator interface. If there are any standard practices that I am not following please point out.
Thank You