I'm building a Binary Search Tree class that has an inorder iterator and a pre-order iterator. I've coded up an attempt for the inorder iterator but I don't think I have this code right. I read some guides on coding the iterators and I took what I interpreted and tried to implement it in my design. The hasNext that is returning the tree is questionable. Currently the tree.isEmpty() returns whether the root node is null for the tree. I'm not sure if that's correct to check during an iteration of the tree.
I'm not entirely confident in my understanding of how Inorder iteration works. Currently, I'm starting at root.left with the next() method than attempting to iterator from there.
Some Notes for code clarification E = element / data for the nodes K = key
public class BSTIterator<E, K> implements StructureIterator<E> {
private final BST<E, K> tree;
BSTNode<E> root =null;
// Comparator<E> sortFct;
// BiFunction<K, E, Integer> searchFct;
public BSTIterator( BSTNode<E> root,Comparator<E> sortFct,
BiFunction<K, E, Integer> searchFct) {
this.tree = new BST<E, K>(sortFct, searchFct);
this.root = root;
}
@Override
public E next() {
BSTNode<E> node = tree.root.getLeft();
E result = node.getData();
if(node.getRight() != null){
while(node != null){
tree.root.getRight();
node = node.getLeft();
}
}
return result;
}
@Override
public boolean hasNext() {
return !tree.isEmpty();
}
}