package binaryTree; /** * Represents the empty state of a BiTree. Uses the singleton pattern to model * the uniqueness of "emptiness". * @author Dung X. Nguyen - Copyright 1999 - All rights reserved. * @since 11/10/99 */ class EmptyNode extends ANode { /** * Singleton pattern. */ final static EmptyNode Singleton = new EmptyNode (); private EmptyNode () { } /** * Throws java.util.NoSuchElementException. */ Object getRootDat (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getRootDat ()"); } /** * Throws java.util.NoSuchElementException. */ void setRootDat (Object dat, BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.setRootDat ()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getLeftSubTree (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getRightSubTree(BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getRightSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ void setLeftSubTree(BiTree biTree, BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.setLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ void setRightSubTree(BiTree biTree, BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.setRightSubTree ()"); } /** * Asks the owner tree to set the root node to a new DatNode containing dat, * resulting in a state change from empty to non-empty. * @param dat * @param owner the context of this state. */ void insertRoot(Object dat, BiTree owner) { owner.setRootNode (new DatNode (dat)); } /** * Throws java.util.NoSuchElementException. */ Object remRoot (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.remRoot ()"); } /** * @param oSib * @param oDad */ Object remParent(BiTree oSib, BiTree oDad, BiTree owner) { return oSib.remParentNode (oDad); } /** * Removes and return the root element of the grandparent of this node, * knowing the sibbling of the owner tree is empty. */ Object remParentNode (BiTree ownerDad, BiTree owner) { Object dat = ownerDad.getRootDat(); ownerDad.setRootNode (EmptyNode.Singleton); return dat; } /** * Calls the visitor's nullCase () method to execute the visiting algorithm. * @param algo the visiting algorithm * @param input the input the algorithm needs. * @param owner the context of this node. * @return the output for the nullCase() of the algorithm. */ Object execute(IVisitor algo, Object input, BiTree owner) { return algo.emptyCase(owner, input); } }