/* * BSTEnumerator.java - Enumerate A BST * * Copyright (c) 1996 Chuck McManis, All Rights Reserved. * * Permission to use, copy, modify, and distribute this software * and its documentation for NON-COMMERCIAL purposes and without * fee is hereby granted provided that this copyright notice * appears in all copies. * * CHUCK MCMANIS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE * SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. CHUCK MCMANIS * SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT * OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. */ import java.util.Enumeration; import java.util.NoSuchElementException; /** * This class is used to enumerate a BST from the first to the last node. */ class BSTEnumerator extends BSTNode implements Enumeration { private BSTNode currentNode; private boolean keys; BSTEnumerator(BSTNode start, boolean doKeys) { super(); currentNode = (start != null) ? start.min() : null; keys = doKeys; } public boolean hasMoreElements() { return (currentNode != null); } public Object nextElement() { if (currentNode == null) throw new NoSuchElementException(); BSTNode n = currentNode; currentNode = n.successor(); return (keys ? n.key : n.payload); } }