Class BinarySearchTree<E extends Comparable<? super E>>

java.lang.Object
  extended by BinarySearchTree<E>

public class BinarySearchTree<E extends Comparable<? super E>>
extends Object

Shell for a bianry search tree class.

Author:
scottm

Constructor Summary
BinarySearchTree()
           
 
Method Summary
 boolean add(E data)
          add the specified item to this Binary Search Tree if it is not already present.
 List<E> getAll()
          return a list of all the elements in this Binary Search Tree.
 int height()
          return the height of this Binary Search Tree.
 boolean isPresent(E data)
          check to see if the specified element is in this Binary Search Tree.
 boolean iterativeAdd(E data)
          An add method that implements the add algorithm iteratively instead of recursively.
 E max()
          return the maximum value in this binary search tree.
 E min()
          return the minimum value in this binary search tree.
 int numNodesAtDepth(int d)
          Find the number of nodes in this tree at the specified depth.
 void printTree()
          Prints a vertical representation of this tree.
 boolean remove(E data)
          remove a specified item from this Binaray Search Tree if it is present.
 int size()
          return how many elements are in this Binary Search Tree.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinarySearchTree

public BinarySearchTree()
Method Detail

add

public boolean add(E data)
add the specified item to this Binary Search Tree if it is not already present.
pre: data != null
post: add this item to the tree. Return true if this tree changed as a result of this method call, false otherwise.

Parameters:
data - the data to add to the tree
Returns:
false if an item equivalent to data is already present in the tree, return true if data is added to the tree and size() = old size() + 1

remove

public boolean remove(E data)
remove a specified item from this Binaray Search Tree if it is present.
pre: data != null
post: remove data from the tree, return true if this tree changed as a result of this method call, false otherwise.

Parameters:
data - the data to remove from the tree if present
Returns:
false if data was not present returns true if data was present and size() = old size() - 1

isPresent

public boolean isPresent(E data)
check to see if the specified element is in this Binary Search Tree.
pre: data != null
post: return true if data is present in tree, false otherwise

Parameters:
data - the data to look for in the tree

size

public int size()
return how many elements are in this Binary Search Tree.
pre: none
post: return the number of items in this tree


height

public int height()
return the height of this Binary Search Tree.
pre: none
post: return the height of this tree. If the tree is empty return -1, otherwise return the height of the tree


getAll

public List<E> getAll()
return a list of all the elements in this Binary Search Tree.
pre: none
post: return a List object with all data from the tree in ascending order. If the tree is empty return an empty List


max

public E max()
return the maximum value in this binary search tree.
pre: size() > 0
post: return the largest value in this Binary Search Tree

Returns:
the maximum value in this tree

min

public E min()
return the minimum value in this binary search tree.
pre: size() > 0
post: eturn the smallest value in this Binary Search Tree

Returns:
the minimum value in this tree

iterativeAdd

public boolean iterativeAdd(E data)
An add method that implements the add algorithm iteratively instead of recursively.
pre: data != null
post: if data is not present add it to the tree, otherwise do nothing.

Parameters:
data - the item to be added to this tree
Returns:
true if data was not present before this call to add, false otherwise.

numNodesAtDepth

public int numNodesAtDepth(int d)
Find the number of nodes in this tree at the specified depth.

Parameters:
d - The depth to find nodes at.
Returns:
The number of nodes in this tree at a depth equal tot the parameter d.

printTree

public void printTree()
Prints a vertical representation of this tree. The tree has been rotated counter clockwise 90 degrees. The root is on the left. Each node is printed out on its own row. A node's children wil not necessarily be at the rows directly above and below a row. They will be indented three spaces from the parent. Nodes indented the same amount are at the same depth. pre: none