public class TreeNodeIterator implements Iterator { // Constants private static final int USEROOT = -1; // Fields /** The node the iterator is based on */ TreeNode root; /** The iterator of the current child. */ Iterator child_iterator; /** The number of the current child. */ int current; /** Create a new iterator. */ public TreeNodeIterator(TreeNode n) { root = n; current = USEROOT; } /** Peek at the next value. */ public Object peek() { if (current == USEROOT) { return root.value(); } else { return child_iterator.peek(); } } // peek() /** Get the next value. */ public Object nextElement() { Object next; // Special case if (current == USEROOT) { next = root.value(); current = 0; child_iterator = new TreeNodeIterator(root.getChild(current)); } // General case else { next = child_iterator.nextElement(); } // Keep going until we find a child iterator that has // elements remaining or we run out of iterators. while ( (current < root.arity()) && (!child_iterator.hasMoreElements()) ) { ++current; child_iterator = new TreeNodeIterator(root.getchild(current)); } // while } // nextElement(); /** Are there elements remaining? */ public Object hasMoreElements() { return ( (current == USEROOT) || ( (current < root.arity()) && child_iterator.hasMoreElements() ) ); } // hasMoreElements } // TreeNodeIterator
public class TreeNodeIterator implements Iterator { /** Have we returned the root? */ boolean returned_root; /** The root node. */ public TreeNode root; /** All the child iterators. */ public Iterator[] child_iterators; /** Create a new iterator. */ public TreeNodeIterator(TreeNode n) { root = n; returned_root = false; child_iterators = new Iterators[n.arity()]; for (int i = 0; i < n.arity(); ++i) { child_iterators[i] = new TreeNodeIterator(n.getChild(i)); } // for } // TreeNodeIterator /** Peek at the next element. */ public peek() { if (!returned_root) { return root.value(); } childnum = pick a child iterator with more elements; return child_iterators[childnum].peek(); } // peek /** Are there more elements? */ public hasMoreElements() { // If we haven't returned the root, we have more elements. if (!returned_root) return true; // If any child iterator has more elements, there are more elements. for (int i = 0; i < root.arity(); ++i) { if child_iterators[i].hasMoreElements() return true; } // for // Nope. return false; } // hasMoreElements } // TreeNodeIterator
public class TreeNodeIterator implements Iterator { // Fields /** The stack of node/edge pairs. */ Stack path; /** Build a new iterator */ public TreeNodeIterator(TreeNode n) { path = new Stack(); path.push(n); } /** Peek at the next element */ public Object peek() { return path.peek().value(); } /** Get the next value. */ public Object nextElement() { // Remember the top of the stack public Object oldtop = path.peek(); // Can we advance the path? if (path.peek().arity() > 0) { path.push(new Integer(0)); path.push(oldtop.getChild(0)); } else { /** Have we found a non-null element? */ boolean found = false; /** Keep going until wie find a non-null element */ while (!path.empty() && !found) { /** Get rid of the node that is no longer useful path.pop(); /** Get the edge we took to reach that element. */ int edge = ((Integer) path.pop())).intValue(); /** Can we push a new edge? */ ++edge; int tmp = (TreeNode) path.peek(); if (edge < tmp.arity()) { push(new Integer(edge)); push(tmp.getChild(edge)); found = true; } } // while } // else return oldtop.value(); } // nextElement() /** Are there elements remaining. */ public boolean hasMoreElements() { return (!path.empty()); } // hasMoreElements } // TreeNodeIterator
public class TreeNodeIterator implements Iterator { /** The list of nodes not yet returned. Nodes below these nodes also have not yet been returned. */ public Queue notyet; /** Create a new iterator */ public TreeNodeIterator(TreeNode n) { notyet = new Queue(); notyet.add(n); } /** Peek at the next element. */ public peek() { return notyet.peek().value(); } /** Grab the next element and advance. */ public nextElement() { TreeNode next = notyet.remove(); for (int i = 0; i < next.arity(); ++i) { notyet.add(next.getChild(i)); } return next.value(); } /** Are there elements left to return? */ public hasMoreElements() { return (!notyet.empty()); } // hasMoreElements } // TreeNodeIterator
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
Source text last modified Tue Dec 16 13:54:36 1997.
This page generated on Tue Dec 16 13:56:22 1997 by SiteWeaver.
Contact our webmaster at rebelsky@math.grin.edu