/* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See end of file] */ package structure.avl; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; public class AvlIterator> implements Iterator { public static > Iterator iterator(AVL avl, R min, R max) { return new AvlIterator(avl, min, max) ; } boolean finished = false ; R record ; // Yield this before moving on AvlNode node ; R max ; AvlIterator(AVL avl, R min, R max) { if ( min != null && max != null ) { int x = min.compareTo(max) ; if ( x >= 0 ) { finished = true ; return ; } } node = avl.findNodeAbove(min) ; if ( node == null ) { record = null ; finished = true ; return ; } record = node.record ; this.max = max ; } @Override public boolean hasNext() { if ( finished ) return false ; if ( record != null ) return true ; // Record null, move to next node. if ( node == null ) { System.err.println("Happened!") ; // Does not happen? finished = true ; return false ; } // Have yielded the value for "node" (and hence all left subtree) // Move the left-est node of the right subtree. // If no right subtree // Go up to parent. // Check we were the left subtree of parent, not right. // If no parent (this is the root), and we were left of parent, // go down AvlNode nextNode = node.right ; if ( nextNode != null ) { // There is a right tree to do. // Find min (left chasing) while ( nextNode.left != null ) nextNode = nextNode.left ; } else { // No right subtree from here. // Walk up tree until we were not the right node of our parent. AvlNode n2 = node ; AvlNode n3 = n2.parent ; while( n3 != null ) { if ( n3.right != n2 ) { n2 = n3 ; break ; } n2 = n3 ; n3 = n2.parent ; } if ( n3 == null ) { finished = true ; return false ; } // Now at the first node upwards when we're the left // (i.e. less than the value) nextNode = n2 ; } node = nextNode ; return testAndSetRecord(nextNode) ; } private boolean testAndSetRecord(AvlNode node) { if ( node == null ) { record = null ; finished = true ; return false ; } if ( max != null ) { int x = node.record.compareTo(max) ; if ( x >= 0 ) { // End finished = true ; return false ; } } record = node.record ; return true ; } @Override public R next() { if ( ! hasNext()) throw new NoSuchElementException("AvlIterator") ; R r = record ; record = null ; return r ; } @Override public void remove() { throw new UnsupportedOperationException("AvlIterator") ; } // Calculate the iterator. For testing. static > List calcIter(AvlNode node, R min, R max) { List x = new ArrayList() ; if ( node == null ) return x ; node.records(x) ; // Sorted. if ( min != null ) while ( x.size() > 0 && x.get(0).compareTo(min) < 0 ) x.remove(0) ; if ( max != null ) while ( x.size() > 0 && x.get(x.size()-1).compareTo(max) >= 0 ) x.remove(x.size()-1) ; return x ; } } /* * (c) Copyright 2007, 2008, 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */