我正在尝试使用迭代器来完成对链接的二叉树的有序遍历。但是,当我从树上的迭代器上调用iterator()。next()时,它总是抛出NoSuchElementException异常。为简单起见,我将使用一棵只有一个单节点的树,该树称为ir singleNodeTree。我不知道为什么迭代器没有按预期方式遍历树,而我在哪里做错了?
BinaryTree.java
/**
* Returns an iterator over the elements in this tree using the
* iteratorInOrder method
*
* @return an in order iterator over this binary tree
*/
public Iterator<T> iterator()
{
return iteratorInOrder();
}
/**
* Performs an inorder traversal on this binary tree by calling an
* overloaded, recursive inorder method that starts with
* the root.
*
* @return an in order iterator over this binary tree
*/
public Iterator<T> iteratorInOrder()
{
ArrayUnorderedList<T> tempList = new ArrayUnorderedList<T>();
inOrder(root, tempList);
return new TreeIterator(tempList.iterator());
}
/**
* Performs a recursive inorder traversal.
*
* @param node the node to be used as the root for this traversal
* @param tempList the temporary list for use in this traversal
*/
protected void inOrder(BinaryTreeNode<T> node,
ArrayUnorderedList<T> tempList)
{
if (node != null)
{
inOrder(node.getLeft(), tempList);
tempList.addToRear(node.getElement());
inOrder(node.getRight(), tempList);
}
}
/**
* Inner class to represent an iterator over the elements of this tree
*/
private class TreeIterator implements Iterator<T>
{
private int expectedModCount;
private Iterator<T> iter;
/**
* Sets up this iterator using the specified iterator.
*
* @param iter the list iterator created by a tree traversal
*/
public TreeIterator(Iterator<T> iter)
{
this.iter = iter;
expectedModCount = modCount;
}
/**
* Returns true if this iterator has at least one more element
* to deliver in the iteration.
*
* @return true if this iterator has at least one more element to deliver
* in the iteration
* @throws ConcurrentModificationException if the collection has changed
* while the iterator is in use
*/
public boolean hasNext() throws ConcurrentModificationException
{
if (!(modCount == expectedModCount))
throw new ConcurrentModificationException();
return (iter.hasNext());
}
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws NoSuchElementException
{
if (hasNext())
return (iter.next());
else
throw new NoSuchElementException();
}
/**
* The remove operation is not supported.
*
* @throws UnsupportedOperationException if the remove operation is called
*/
public void remove()
{
throw new UnsupportedOperationException();
}
}
驱动程序。 java
public class Driver
{
public static void main(String[] args)
{
//singleNodeTree is a tree with a single node element "a".
//It has some basic method:
//1. getRootElement() return current element at the root
//2. size() return size of the tree
//3. contains(String a) reutrn true if string element a is in the tree
BinaryTree singleNodeTree = new BinaryTree("a");
System.out.println(singleNodeTree.getTree().getRootElement()); //return "a"
System.out.println(singleNodeTree.getTree().size()); // return 1
System.out.println(singleNodeTree.getTree().contains("a")); //return true
System.out.println(singleNodeTree.getTree().iterator().hasNext());//return false
if (singleNodeTree.getTree().iterator().next()!= null)// throws NoSuchElementException
System.out.println("It is not empty.");
/* what I intend to do is the following:
while (singleNodeTree.getTree().iterator().hasNext())
System.out.println(singleNodeTree.getTree().iterator().next()+ " "); */
}
}
异常从这里抛出:
/**
* Returns the next element in the iteration. If there are no
* more elements in this iteration, a NoSuchElementException is
* thrown.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iterator is empty
*/
public T next() throws NoSuchElementException
{
if (hasNext())
return (iter.next());
else
throw new NoSuchElementException(); <-- This is where the Exception is thrown.
}
您需要将元素添加到列表中才能获得下一个元素。