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 parent) { throw new java.util.NoSuchElementException ("EmptyNode.getRootDat ()"); } /** * Throws java.util.NoSuchElementException. */ void setRootDat(Object dat, BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.setRootDat ()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getLeftSubTree(BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.getLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ BiTree getRightSubTree(BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.getRightSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ void setLeftSubTree(BiTree biTree, BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.setLeftSubTree ()"); } /** * Throws java.util.NoSuchElementException. */ void setRightSubTree(BiTree biTree, BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.setRightSubTree ()"); } /** * Asks the parent 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 parent the context of this state. */ void insertRoot(Object dat, BiTree parent) { parent.setRootNode(new DatNode (dat)); } /** * Throws java.util.NoSuchElementException. */ Object remRoot(BiTree parent) { throw new java.util.NoSuchElementException ("EmptyNode.remRoot ()"); } /** * Removes and return the root element of the grandparent of this node, * asking the sibbling of the parent tree to do it if necessary. * @param aunt the sibbling of the parent of this node. * @param grandPo the grand parent of this node. * @param mom the parent (i.e. context) of this node. * @return the root data of the grandparent of this node. */ Object remParent(BiTree aunt, BiTree grandPo, BiTree mom) { return aunt.remOurParent (grandPo); } /** * Removes and return the root element of the grandparent of this node, * knowing the sibbling of the parent tree is empty. * @param grandParent the grand parent of this BiTree. * @param parent the context of this node. * @return the root data of the grandparent of this node. */ Object remOurParent(BiTree grandParent, BiTree parent) { Object dat = grandParent.getRootDat(); grandParent.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 parent the context of this node. * @return the output for the nullCase() of the algorithm. */ Object execute(IVisitor algo, Object input, BiTree parent) { return algo.emptyCase(parent, input); } }