package binaryTree; /** * Stores data and represents a non-empty state. * @author Dung X. Nguyen - Copyright 1999 - All rights reserved. * @since 11/10/99 */ class DatNode extends ANode { /** * Data Invariant: != null. * @SBGen Variable (,left subtree,,64) */ private BiTree _leftTree = new BiTree (); /** * the stored data element. */ private Object _dat; /** * Data Invariant: != null. * @SBGen Variable (,right subtree,,64) */ private BiTree _rightTree = new BiTree (); /** * Initialize the data element to a given object. * @param dat */ DatNode(Object dat) { _dat = dat; } /** * Gets the root data of the parent Tree. * @param parent the context of the state. * @return the data element of this node. */ Object getRootDat(BiTree parent) { return _dat; } /** * Sets the data element of this node to a given data object. * @param dat * @param parent the context of this state. */ void setRootDat(Object dat, BiTree parent) { _dat = dat; } /** * Gets the left subtree of the parent tree. * @param parent the context of this state. * @return the left subtree of this node. */ BiTree getLeftSubTree(BiTree parent) { return _leftTree; } /** * Gets the right subtree of the parent tree. * @param parent the context of this state. * @return the right subtree of this node. */ BiTree getRightSubTree(BiTree parent) { return _rightTree; } /** * Sets the left subtree of this node to a given tree. * Allows for growing the parent tree. * @param biTree != null. * @param parent the context of this state. */ void setLeftSubTree(BiTree biTree, BiTree parent) { _leftTree = biTree; } /** * Sets the right subtree of this node to a given tree. * Allows for growing the parent tree. * @param biTree != null. * @param parent the context of this state. */ void setRightSubTree(BiTree biTree, BiTree parent) { _rightTree = biTree; } /** * Throws an IllegalStateException because the parent tree is not empty. * @exception IllegaStateException. */ void insertRoot(Object dat, BiTree parent) { throw new IllegalStateException ("DatNode.insertRoot - Tree is not empty."); } /** * Removes and returns the root element from the parent tree by handing the right * subtree to the left subtree to do the job. * @param dat * @param parent the context of this state. * @exception IllegaStateException if the parent has more than one element. */ Object remRoot(BiTree parent) { return _leftTree.remParent (_rightTree, parent); } /** * Throws an IllegalStateException because the parent tree is not empty. */ Object remParent(BiTree sis, BiTree grandPo, BiTree mom) { throw new IllegalStateException ("Tree has more than one element."); } /** * Throws an IllegalStateException because the parent tree is not empty. */ Object remOurParent(BiTree grandParent, BiTree parent) { throw new IllegalStateException ("Tree has more than one element."); } /** * Calls the visitor's nonNullCase () 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 nonNullCase() of the algorithm. */ Object execute(IVisitor algo, Object input, BiTree parent) { return algo.nonEmptyCase (parent, input); } }