package brs; /** * Stores data and represents a non-empty state. * @author Dung X. Nguyen - Copyright 2001 - All rights reserved. */ class DatNode extends ANode { /** * Data Invariant: != null. * @SBGen Variable (,left,,64) */ private BiTree _leftTree = new BiTree (); /** * the stored data element. */ private Object _dat; /** * Data Invariant: != null. * @SBGen Variable (,right,,64) */ private BiTree _rightTree = new BiTree (); /** * Initialize the data element to a given object. * @param dat a given data object. */ DatNode(Object dat) { _dat = dat; } /** * Gets the root data of the owner Tree. * @param owner the BiTree holding this DatNode. * @return the data element of this DatNode. */ Object getRootDat(BiTree owner) { return _dat; } /** * Sets the data element of this node to a given data object. * @param dat a given data object. * @param owner the BiTree holding this DatNode. */ void setRootDat(Object dat, BiTree owner) { _dat = dat; } /** * Gets the left subtree of the owner tree. * @param owner the BiTree holding this DatNode. * @return the left subtree of this DatNode. */ BiTree getLeftSubTree(BiTree owner) { return _leftTree; } /** * Gets the right subtree of the owner tree. * @param owner the BiTree holding this DatNode. * @return the right subtree of this DatNode. */ BiTree getRightSubTree(BiTree owner) { return _rightTree; } /** * Sets the left subtree of this Datnode to a given tree. * Allows for growing the owner tree. * @param biTree != null. Does not check for null! * @param owner the BiTree holding this DatNode. */ void setLeftSubTree(BiTree biTree, BiTree owner) { _leftTree = biTree; } /** * Sets the right subtree of this node to a given tree. * Allows for growing the owner tree. * @param biTree != null. Does not check for null! * @param owner the BiTree holding this DatNode. */ void setRightSubTree(BiTree biTree, BiTree owner) { _rightTree = biTree; } /** * Throws an IllegalStateException because the owner tree is not empty. * @exception IllegaStateException. */ void insertRoot(Object dat, BiTree owner) { throw new IllegalStateException ("DatNode.insertRoot()."); } /** * Removes and returns the root element from the owner tree by handing the * right subtree to the left subtree to do the job. * @param owner the BiTree holding this DatNode. * @exception IllegaStateException if the owner has a non-empty subtree. */ Object remRoot(BiTree owner) { return _leftTree.remParent (_rightTree, owner); } /** * Throws an IllegalStateException because the owner tree is not empty, * meaning the parent of the owner tree has a non-empty subtree. * @param oSib the sibling tree of the owner tree. * @param oDad the parent tree of the owner tree. * @param owner the BiTree holding this DatNode. */ Object remParent(BiTree oSib, BiTree oDad, BiTree owner) { throw new IllegalStateException ("Tree has more than one element."); } /** * Throws an IllegalStateException because the owner tree is not empty, * meaning the parent of the owner tree has a non-empty subtree. * @param ownerDad the parent tree of the owner tree. * @param owner the BiTree holding this DatNode. */ Object remParentNode (BiTree ownerDad, BiTree owner) { throw new IllegalStateException ("Tree has more than one element."); } /** * Calls algo's nonEmptyCase() method to execute the algorithm algo. * @param algo an algorithm on owner. * @param inp the input algo needs. * @param owner the BiTree holding this DatNode. * @return the output for the nonEmptyCase() of algo. */ Object execute(IVisitor algo, Object inp, BiTree owner) { return algo.nonEmptyCase (owner, inp); } }