/* * 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; private boolean firstNode; BSTEnumerator(BSTNode start, boolean doKeys) { super(); currentNode = start; keys = doKeys; firstNode = true; } public boolean hasMoreElements() { if (firstNode) return true; if (currentNode != null) return (currentNode.successor() != null); return false; } public Object nextElement() { if (firstNode) { firstNode = false; } else if (currentNode != null) { currentNode = currentNode.successor(); } if (currentNode == null) throw new NoSuchElementException(); return (keys ? currentNode.key : currentNode.payload); } }