package binaryTree; /** * Stores data and represents a non-empty state. * @author Alan L. Cox * @author Dung X. Nguyen - Copyright 1999 - All rights reserved. * @since 04/05/01 */ class NonEmptyNode extends ANode { /** * Data Invariant: != null. */ private BiTree _leftTree = new BiTree(); /** * The stored data object. */ private Object _data; /** * Data Invariant: != null. */ private BiTree _rightTree = new BiTree(); /** * Initialize the data object at this node to the given object. * * @param data */ NonEmptyNode(Object data) { _data = data; } /** * Get the data object at this node. * * @return the data object at this node. */ Object getRootData() { return _data; } /** * Set the data object at this node to the given data object. * * @param data the new data object for this node. */ void setRootData(Object data) { _data = data; } /** * Get the left subtree of this node. * * @return the left subtree of this node. */ BiTree getLeftSubTree() { return _leftTree; } /** * Set the left subtree of this node to the given tree. * * @param biTree != null. */ void setLeftSubTree(BiTree biTree) { _leftTree = biTree; } /** * Get the right subtree of this node. * * @return the right subtree of this node. */ BiTree getRightSubTree() { return _rightTree; } /** * Set the right subtree of this node to the given tree. * * @param biTree != null. */ void setRightSubTree(BiTree biTree) { _rightTree = biTree; } /** * Throws an IllegalStateException because the node is not empty. * * @param data the data object. * @param owner the context of this state. * @exception IllegaStateException. */ void insertRoot(Object data, BiTree owner) { throw new IllegalStateException("NonEmptyNode.insertRoot - Tree is not empty."); } /** * 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 context of this state. * @exception IllegaStateException if the owner has more than one element. */ Object removeRoot(BiTree owner) { return _leftTree.removeParent(_rightTree, owner); } /** * Helper method for removeRoot(). * * @param sibling the sibbling of this node. * @param parent the parent of this node. * @param owner the owner (i.e., context) of this node. * @return the root data of the parent of this node. */ Object removeParent(BiTree sibling, BiTree parent, BiTree owner) { return sibling.removeParentFromNonEmptyNode(owner, parent); } /** * Throws an IllegalStateException because the tree has two subtrees. */ Object removeParentFromNonEmptyNode(BiTree sibling, BiTree parent) { throw new IllegalStateException("Tree has two subtrees."); } /** * Calls IAlgo's method nonEmptyCase() to execute the visiting algorithm. * * @param algo the visiting algorithm * @param input the input needed by the visiting algorithm. * @param owner the context of this node. * @return the output for the nonEmptyCase() of the algorithm. */ Object execute(IAlgo algo, Object input, BiTree owner) { return algo.nonEmptyCase(owner, input); } }