Class brs.BiTree


public class BiTree
extends 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.

Author:
Dung X. Nguyen - Copyright 2002 - All rights reserved.

Variable Index

 o _rootNode
the state of this BiTree

Constructor Index

 o BiTree ()
Initializes this BiTree to the empty state

Method Index

 o execute (IVisitor, Object[])
Hook to execute any algorithm that presents itself as a visitor to this
 o getLeftSubTree ()
Gets the left subtree of this BiTree if it exists
 o getRightSubTree ()
Gets the right subtree of this BiTree if it exsists
 o getRootDat ()
Gets the root data of this BiTree if it exists
 o getRootNode ()
Returns the current node (i
 o insertRoot (Object)
Inserts a root element to this BiTree
 o remRoot ()
Removes and returns the root data element of this BiTree
 o setLeftSubTree (BiTree)
Attaches a new left subtree to the left of this BiTree, allowing this
 o setRightSubTree (BiTree)
Attaches a new right subtree to the left of this BiTree, allowing this
 o setRootDat (Object)
Sets the root data element to a given object
 o setRootNode (ANode)
Changes this BiTree to a given new node (i
 o toString ()

Variables

 o _rootNode
private ANode _rootNode
the state of this BiTree.

Constructors

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

Methods

 o getRootDat
public Object 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.
 o setRootDat
public void setRootDat(Object dat)
Sets the root data element to a given object.

Parameters:
dat - the new root data.
Throws: NoSuchElementException
if this BiTree is empty.
 o getLeftSubTree
public BiTree 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.
 o getRightSubTree
public BiTree 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.
 o setLeftSubTree
public void setLeftSubTree(BiTree 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.
 o setRightSubTree
public void setRightSubTree(BiTree 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.
 o insertRoot
public void insertRoot(Object dat)
Inserts a root element to this BiTree. Allows for state change from empty to non-empty.

Parameters:
dat - the new root data.
Throws: IllegalStateException
if this BiTree has more than one element.
 o remRoot
public Object 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.
Throws: IllegalStateException
if both subtrees are empty.
 o execute
public Object execute(IVisitor algo, Object[] inp)
Hook to execute any algorithm that presents itself as a visitor to this BiTree.

Parameters:
algo - a visitor to a BiTree.
inp - the input for the algo visitor.
Returns:
the output for the execution of algo.
 o toString
public String toString()
 o setRootNode
final void setRootNode(ANode node)
Changes this BiTree to a given new node (i.e. state).

Parameters:
node - a new root node (i.e.state) for this BiTree.
 o getRootNode
final ANode getRootNode()
Returns the current node (i.e. state) of this BiTree.

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