package binaryTree; /** * Represents the state of the parent binary tree structure. * * This is an instance of the Union pattern. * * @author Alan L. Cox * @author Dung X. Nguyen - Copyright 1999 - All rights reserved. * @since 04/05/01 */ abstract class ANode { /** * Get the data object at this node if it exists. * * @return the data object at this node if it exists. * @exception NoSuchElementException if the node is empty. */ abstract Object getRootData(); /** * Set the data object at this node to the given data object. * * @param data the new data object for this node. * @exception NoSuchElementException if the node is empty. */ abstract void setRootData(Object data); /** * Get the left subtree of this node. * * @return the left subtree of this node if it exists. * @exception NoSuchElementException if the owner is empty. */ abstract BiTree getLeftSubTree(); /** * Set the left subtree of this node to the given tree. * * @param biTree != null. * @exception NoSuchElementException if the owner is empty. */ abstract void setLeftSubTree(BiTree biTree); /** * Get the right subtree of this node. * * @return the right subtree of this node if it exists. * @exception NoSuchElementException if the owner is empty. */ abstract BiTree getRightSubTree(); /** * Set the right subtree of this node to the given tree. * * @param biTree != null. * @exception NoSuchElementException if the owner is empty. */ abstract void setRightSubTree(BiTree biTree); /** * Changes an empty BiTree into a non-empty BiTree * with the given data at the root of the BiTree. * * @param data the data object. * @param owner the context of this state. * @exception IllegaStateException if the owner is not empty. */ abstract void insertRoot(Object data, BiTree owner); /** * Removes and returns the data object at this node if at least * one of the subtrees is empty. The node may change from a * non-empty to an empty state. * * @param owner the context of this state. * @exception NoSuchElementException if this node is empty. * @exception IllegalStateException if this node has two non-empty * subtrees. */ abstract Object removeRoot(BiTree owner); /** * Helper method for removeRoot(). * * @param sibling the sibbling of this node. * @param parent the parent of this node. * @param owner the owner of this node. * @return the root data of the parent of this node. */ abstract Object removeParent(BiTree sibling, BiTree parent, BiTree owner); /** * Helper method for removeParent(). * * @param parent the parent of this node. * @return the data object at the parent of this node. */ abstract Object removeParentFromNonEmptyNode(BiTree sibling, BiTree parent); /** * Calls IAlgo's appropriate method 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 algorithm's output. */ abstract Object execute(IAlgo algo, Object input, BiTree owner); }