package brs; /** * Represents the empty state of a BiTree. Uses the singleton pattern to model * the uniqueness of "emptiness". * @author Dung X. Nguyen - Copyright 2001 - All rights reserved. * @since 11/04/01 */ class EmptyNode extends ANode { /** * Singleton pattern. * @SBGen Variable (singleton,,,0) */ final static EmptyNode Singleton = new EmptyNode (); private EmptyNode (){ } /** * Throws java.util.NoSuchElementException. * @param owner the BiTree holding this EmptyNode. */ Object getRootDat (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getRootDat ()"); } /** * Throws java.util.NoSuchElementException. * @param dat a data Object. * @param owner the BiTree holding this EmptyNode. */ void setRootDat (Object dat, BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.setRootDat ()"); } /** * Throws java.util.NoSuchElementException. * @param owner the BiTree holding this EmptyNode. */ BiTree getLeftSubTree (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. * @param owner the BiTree holding this EmptyNode. */ BiTree getRightSubTree(BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.getRightSubTree ()"); } /** * Throws java.util.NoSuchElementException. * @param biTree a given BiTree. * @param owner the BiTree holding this EmptyNode. */ void setLeftSubTree(BiTree biTree, BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.setLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. * @param biTree a given BiTree. * @param owner the BiTree holding this EmptyNode. */ 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 a given data Object. * @param owner the context of this state. */ void insertRoot(Object dat, BiTree owner) { owner.setRootNode(new DatNode(dat)); } /** * Throws java.util.NoSuchElementException. * @param owner the BiTree holding this EmptyNode. */ Object remRoot (BiTree owner) { throw new java.util.NoSuchElementException ("EmptyNode.remRoot ()"); } /** * Asks oSib to help remove the root node of the parent of owner. * Because this node is the EmptyNode, it's up to the sibling of the owner * tree to decide whether or not it can help remove the root node of the * owner's parent. * @param oSib the sibling BiTree owner. * @param oDad the parent tree of owner. * @param owner the BiTree holding this EmptyNode. */ Object remParent(BiTree oSib, BiTree oDad, BiTree owner) { return oSib.remParentNode (oDad); } /** * Removes and returns the root element of the grandparent of this node: * because this method is called by an empty sibling of owner, we * know at this point that the parent of owner has two empty subtrees. * @param owner the BiTree holding this EmptyNode. */ Object remParentNode (BiTree ownerDad, BiTree owner) { Object dat = ownerDad.getRootDat(); ownerDad.setRootNode (EmptyNode.Singleton); return dat; } /** * Calls algo's emptyCase () method to execute the algorithm algo. * @param algo the visiting algorithm * @param inp the input algo needs. * @param owner the BiTree holding this EmptyNode. * @return the output for the emptyCase() of algo. */ Object execute(IVisitor algo, Object inp, BiTree owner) { return algo.emptyCase(owner, inp); } }