|
|
|
|
|
|
|
|
* Constructor for pre-order iterator. |
|
|
* Constructor for pre-order iterator. |
|
|
*/ |
|
|
*/ |
|
|
public PreOrder() { |
|
|
public PreOrder() { |
|
|
hasNext(); // A call to set up the initial iterators |
|
|
|
|
|
|
|
|
hasNextNode(); // A call to set up the initial iterators |
|
|
// so that a call to next() without a preceding call to |
|
|
// so that a call to next() without a preceding call to |
|
|
// hasNext() will behave sanely |
|
|
// hasNext() will behave sanely |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public PreOrder(Object sync) { |
|
|
public PreOrder(Object sync) { |
|
|
synchronized (sync) { |
|
|
synchronized (sync) { |
|
|
hasNext(); // A call to set up the initial iterators |
|
|
|
|
|
|
|
|
hasNextNode(); // A call to set up the initial iterators |
|
|
// so that a call to next() without a preceding call to |
|
|
// so that a call to next() without a preceding call to |
|
|
// hasNext() will behave sanely |
|
|
// hasNext() will behave sanely |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public boolean hasNext() { |
|
|
public boolean hasNext() { |
|
|
|
|
|
return hasNextNode(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean hasNextNode() { |
|
|
if (selfNotReturned) { |
|
|
if (selfNotReturned) { |
|
|
return true; |
|
|
return true; |
|
|
} |
|
|
} |
|
|
// self has been returned - are there any children? |
|
|
// self has been returned - are there any children? |
|
|
// if so, we must always have an iterator available |
|
|
// if so, we must always have an iterator available |
|
|
// even unless it is exhausted. Assume it is set up this |
|
|
|
|
|
|
|
|
// even if it is exhausted. Assume it is set up this |
|
|
// way by next(). The iterator has a chance to do this |
|
|
// way by next(). The iterator has a chance to do this |
|
|
// because self will always be returned first. |
|
|
// because self will always be returned first. |
|
|
// The test of nextChildIndex must always be made because |
|
|
// The test of nextChildIndex must always be made because |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Object next() { |
|
|
public Object next() { |
|
|
if (! hasNext()) { |
|
|
|
|
|
|
|
|
if (! hasNextNode()) { |
|
|
throw new NoSuchElementException(); |
|
|
throw new NoSuchElementException(); |
|
|
} |
|
|
} |
|
|
if (selfNotReturned) { |
|
|
if (selfNotReturned) { |
|
|
|
|
|
|
|
|
* Constructor for post-order iterator. |
|
|
* Constructor for post-order iterator. |
|
|
*/ |
|
|
*/ |
|
|
public PostOrder() { |
|
|
public PostOrder() { |
|
|
hasNext(); // A call to set up the initial iterators |
|
|
|
|
|
|
|
|
hasNextNode(); // A call to set up the initial iterators |
|
|
// so that a call to next() without a preceding call to |
|
|
// so that a call to next() without a preceding call to |
|
|
// hasNext() will behave sanely |
|
|
// hasNext() will behave sanely |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
*/ |
|
|
*/ |
|
|
public PostOrder(Object sync) { |
|
|
public PostOrder(Object sync) { |
|
|
synchronized (sync) { |
|
|
synchronized (sync) { |
|
|
hasNext(); // A call to set up the initial iterators |
|
|
|
|
|
|
|
|
hasNextNode(); // A call to set up the initial iterators |
|
|
// so that a call to next() without a preceding call to |
|
|
// so that a call to next() without a preceding call to |
|
|
// hasNext() will behave sanely |
|
|
// hasNext() will behave sanely |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public boolean hasNext() { |
|
|
public boolean hasNext() { |
|
|
|
|
|
return hasNextNode(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private boolean hasNextNode() { |
|
|
// self is always the last to go |
|
|
// self is always the last to go |
|
|
if (selfReturned) { // nothing left |
|
|
if (selfReturned) { // nothing left |
|
|
return false; |
|
|
return false; |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
public Object next() throws NoSuchElementException { |
|
|
public Object next() throws NoSuchElementException { |
|
|
if (! hasNext()) { |
|
|
|
|
|
|
|
|
if (! hasNextNode()) { |
|
|
throw new NoSuchElementException(); |
|
|
throw new NoSuchElementException(); |
|
|
} |
|
|
} |
|
|
// Are there any children? |
|
|
// Are there any children? |