brs
Class BiTree<T>

java.lang.Object
  extended by brs.BiTree<T>

public class BiTree<T>
extends java.lang.Object

Models the binary tree structure using the state pattern and the visitor pattern. Provides only structural behaviors and a hook to execute any visitor algorithm. Exercise: Override the toString() method anonymous inner visitor classes.


Field Summary
private  ANode<T> _rootNode
          the state of this BiTree.
private  ToString<T> _toString
          Visitor to generate string representation.
 
Constructor Summary
BiTree()
          Initializes this BiTree to the empty state.
 
Method Summary
<R,P> R
execute(IVisitor<T,R,P> algo, P... inp)
          Hook to execute any algorithm that presents itself as a visitor to this BiTree.
 BiTree<T> getLeftSubTree()
          Gets the left subtree of this BiTree if it exists.
 BiTree<T> getRightSubTree()
          Gets the right subtree of this BiTree if it exsists.
 T getRootDat()
          Gets the root data of this BiTree if it exists.
(package private)  ANode<T> getRootNode()
          Returns the current node (i.e.
 void insertRoot(T dat)
          Inserts a root element to this BiTree.
 T remRoot()
          Removes and returns the root data element of this BiTree.
 void setLeftSubTree(BiTree<T> biTree)
          Attaches a new left subtree to the left of this BiTree, allowing this BiTree to grow to the left.
 void setRightSubTree(BiTree<T> biTree)
          Attaches a new right subtree to the left of this BiTree, allowing this BiTree to grow to the right.
 void setRootDat(T dat)
          Sets the root data element to a given object.
(package private)  void setRootNode(ANode<T> node)
          Changes this BiTree to a given new node (i.e.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

_rootNode

private ANode<T> _rootNode
the state of this BiTree.


_toString

private ToString<T> _toString
Visitor to generate string representation.

Constructor Detail

BiTree

public BiTree()
Initializes this BiTree to the empty state.

Method Detail

getRootDat

public T getRootDat()
Gets the root data of this BiTree if it exists.

Returns:
the root data element of this BiTree if it exists.
Throws:
NoSuchElementException - if this BiTree is empty.

setRootDat

public void setRootDat(T dat)
Sets the root data element to a given object.

Parameters:
dat - the new root data.
Throws:
NoSuchElementException - if this BiTree is empty.

getLeftSubTree

public BiTree<T> getLeftSubTree()
Gets the left subtree of this BiTree if it exists.

Returns:
the left subtree of this BiTree if it exists.
Throws:
NoSuchElementException - if this BiTree is empty.

getRightSubTree

public BiTree<T> getRightSubTree()
Gets the right subtree of this BiTree if it exsists.

Returns:
the right subtree of this BiTree if it exists.
Throws:
NoSuchElementException - if this BiTree is empty.

setLeftSubTree

public void setLeftSubTree(BiTree<T> biTree)
Attaches a new left subtree to the left of this BiTree, allowing this BiTree to grow to the left.

Parameters:
biTree - to replace the current left subtree.
Throws:
NoSuchElementException - if this BiTree is empty.

setRightSubTree

public void setRightSubTree(BiTree<T> biTree)
Attaches a new right subtree to the left of this BiTree, allowing this BiTree to grow to the right.

Parameters:
biTree - to replace the current right subtree.
Throws:
NoSuchElementException - if this BiTree is empty.

insertRoot

public void insertRoot(T dat)
Inserts a root element to this BiTree. Allows for state change from empty to non-empty.

Parameters:
dat - the new root data.
Throws:
java.lang.IllegalStateException - if this BiTree has more than one element.

remRoot

public T remRoot()
Removes and returns the root data element of this BiTree.

Returns:
the root data element of this BiTree.
Throws:
NoSuchElementException - if this BiTree is empty.
java.lang.IllegalStateException - if one of the subtrees is not empty.

execute

public <R,P> R execute(IVisitor<T,R,P> algo,
                       P... inp)
Hook to execute any algorithm that presents itself as a visitor to this BiTree.

Parameters:
algo - a visitor to a BiTree.
inp - the vararg input for the algo visitor.
Returns:
the output for the execution of algo.

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

setRootNode

final void setRootNode(ANode<T> node)
Changes this BiTree to a given new node (i.e. state).

Parameters:
node - a new root node (i.e.state) for this BiTree.

getRootNode

final ANode<T> getRootNode()
Returns the current node (i.e. state) of this BiTree.

Returns:
the current root node (state) of this BiTree.