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.

_rootNode
- the state of this BiTree

BiTree
()
- Initializes this BiTree to the empty state

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

_rootNode
private ANode _rootNode
- the state of this BiTree.

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

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.
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.
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.
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.
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.
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.
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.
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.
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.
toString
public String toString()
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.
getRootNode
final ANode getRootNode()
- Returns the current node (i.e. state) of this BiTree.
- Returns:
- the current root node (state) of this BiTree.