package binaryTree; /** * Represents the empty state of a BiTree. Uses the singleton pattern * to model the uniqueness of "emptiness". * * @author Alan L. Cox * @author Dung X. Nguyen - Copyright 1999 - All rights reserved. * @since 4/5/01 */ class EmptyNode extends ANode { /** * Singleton pattern. */ public final static EmptyNode Singleton = new EmptyNode(); private EmptyNode() { } /** * Throws java.util.NoSuchElementException. */ Object getRootData() { throw new java.util.NoSuchElementException("EmptyNode.getRootData()"); } /** * Throws java.util.NoSuchElementException. */ void setRootData(Object data) { throw new java.util.NoSuchElementException("EmptyNode.setRootData()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getLeftSubTree() { throw new java.util.NoSuchElementException("EmptyNode.getLeftSubTree()"); } /** * Throws java.util.NoSuchElementException. */ void setLeftSubTree(BiTree biTree) { throw new java.util.NoSuchElementException("EmptyNode.setLeftSubTree()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getRightSubTree() { throw new java.util.NoSuchElementException("EmptyNode.getRightSubTree()"); } /** * Throws java.util.NoSuchElementException. */ void setRightSubTree(BiTree biTree) { throw new java.util.NoSuchElementException("EmptyNode.setRightSubTree()"); } /** * Asks the owner tree to set the root node to a new NonEmptyNode * containing data, resulting in a state change from empty to * non-empty. * * @param data * @param owner the context of this state. */ void insertRoot(Object data, BiTree owner) { owner.setRootNode(new NonEmptyNode(data)); } /** * Throws java.util.NoSuchElementException. */ Object removeRoot(BiTree owner) { throw new java.util.NoSuchElementException("EmptyNode.removeRoot()"); } /** * Helper method for removeRoot(). Removes the parent by * elevating its sibling's node to the root of the BiTree. * It doesn't matter whether the sibling is an empty or non- * empty BiTree. * * @param sibling the sibbling of the parent of this node. * @param parent the parent of this node. * @param owner the owner (i.e., context) of this node. * @return the data object at the parent of this node. */ Object removeParent(BiTree sibling, BiTree parent, BiTree owner) { Object data = parent.getRootData(); parent.setRootNode(sibling.getRootNode()); return data; } /** * Helper method for removeParent(). This method is called only * if our left sibling was a non-empty BiTree. Removes the parent * by elevating its sibling's node to the root of the BiTree. * * @param sibling the sibbling of this node. * @param parent the parent of this node. * @return the root data of the parent of this node. */ Object removeParentFromNonEmptyNode(BiTree sibling, BiTree parent) { Object data = parent.getRootData(); parent.setRootNode(sibling.getRootNode()); return data; } /** * Calls IAlgo's method emptyCase() to execute the visiting algorithm. * * @param algo the visiting algorithm * @param input the input needed by the visiting algorithm. * @param owner the context of this node. * @return the output for the emptyCase() of the algorithm. */ Object execute(IAlgo algo, Object input, BiTree owner) { return algo.emptyCase(owner, input); } }