structure
Class RedBlackSearchTree<ELTTYPE extends java.lang.Comparable<ELTTYPE>>

java.lang.Object
  extended by structure.AbstractStructure<ELTTYPE>
      extended by structure.RedBlackSearchTree<ELTTYPE>
All Implemented Interfaces:
java.lang.Iterable<ELTTYPE>, OrderedStructure<ELTTYPE>, Structure<ELTTYPE>

public class RedBlackSearchTree<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
extends AbstractStructure<ELTTYPE>
implements OrderedStructure<ELTTYPE>

Red black trees, are binary trees that guarantee the following three properties.
1. Every child of every leaf is considered black
2. Every red node has two black children
3. Every path from a node to a descendant leaf contains the same number of black nodes.

These properties ensure that elements can be inserted, deleted, and located in logorithmic time.

Example usage:

To create a red-black tree containing the months of the year and to print out this tree as it grows we could use the following

 public static void main(String[] argv){
     RedBlackSearchTree test = new RedBlackSearchTree();
       
     //declare an array of months
     String[] months = new String[]{"March", "May", "November", "August", 
                              "April", "January", "December", "July",
                                      "February", "June", "October", "September"};
      
     //add the months to the tree and print out the tree as it grows
     for(int i=0; i < months.length; i++){
        test.add(months[i]);
        System.out.println("Adding: " + months[i] + "\n" +test.treeString());
      }
  }
 

See Also:
AVLTree, SplayTree, BinaryTree

Constructor Summary
RedBlackSearchTree()
          Constructs a red-black search tree with no data
 
Method Summary
 void add(ELTTYPE value)
          Add a (possibly duplicate) value to the red-black tree, and ensure that the resulting tree is a red-black tree.
 void clear()
          Removes all data from the binary search tree
 boolean contains(ELTTYPE value)
          Determines if the red-black search tree contains a value
 int hashCode()
          Returns the hashCode of the value stored by this object.
 boolean isEmpty()
          Checks for an empty binary search tree
 boolean isRedBlack()
          Returns true iff this tree is a red-black tree.
 java.util.Iterator<ELTTYPE> iterator()
          Returns an iterator over the red-black search tree.
 ELTTYPE remove(ELTTYPE value)
          Remove an value "equals to" the indicated value.
 int size()
          Determines the number of data values within the tree
 java.lang.String toString()
          Returns a string representing tree
 java.lang.String treeString()
          Returns a (possibly long) string representing tree.
 
Methods inherited from class structure.AbstractStructure
elements, values
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface structure.Structure
elements, values
 

Constructor Detail

RedBlackSearchTree

public RedBlackSearchTree()
Constructs a red-black search tree with no data

Post:
Constructs an empty red-black tree
Method Detail

isEmpty

public boolean isEmpty()
Checks for an empty binary search tree

Specified by:
isEmpty in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Overrides:
isEmpty in class AbstractStructure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Returns:
True iff the tree contains no data
Post:
Returns true iff the binary search tree is empty

clear

public void clear()
Removes all data from the binary search tree

Specified by:
clear in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Post:
Removes all elements from binary search tree

size

public int size()
Determines the number of data values within the tree

Specified by:
size in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Returns:
The number of nodes in the binary search tree
Post:
Returns the number of elements in binary search tree

add

public void add(ELTTYPE value)
Add a (possibly duplicate) value to the red-black tree, and ensure that the resulting tree is a red-black tree.

Specified by:
add in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Parameters:
val - A reference to non-null object
Post:
Adds a value to binary search tree

remove

public ELTTYPE remove(ELTTYPE value)
Remove an value "equals to" the indicated value. Only one value is removed, and no guarantee is made concerning which of duplicate values are removed. Value returned is no longer part of the structure

Specified by:
remove in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Parameters:
val - Value sought to be removed from tree
Returns:
Value to be removed from tree or null if no value removed
Post:
Removes one instance of val, if found

contains

public boolean contains(ELTTYPE value)
Determines if the red-black search tree contains a value

Specified by:
contains in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Overrides:
contains in class AbstractStructure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Parameters:
val - The value sought. Should be non-null
Returns:
True iff the tree contains a value "equals to" sought value
Post:
Returns true iff val is a value found within the tree

isRedBlack

public boolean isRedBlack()
Returns true iff this tree is a red-black tree. WARNING: This method executes in linear time and should not be frequently called during the process of insertion and deletion if the user wants

Returns:
True iff this tree is a red-black tree.

iterator

public java.util.Iterator<ELTTYPE> iterator()
Returns an iterator over the red-black search tree. Iterator should not be used if tree is modified, as behavior may be unpredicatable Traverses elements using in-order traversal order

Specified by:
iterator in interface java.lang.Iterable<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Specified by:
iterator in interface Structure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Returns:
An iterator over red-black search tree
See Also:
AbstractIterator, Iterator, Enumeration, Structure.elements()
Post:
Returns iterator to traverse red-blackST

treeString

public java.lang.String treeString()
Returns a (possibly long) string representing tree. Differs from toString() in that toString() outputs a single line representation of the contents of the tree. treeString, however, prints out a graphical representations of the tree's structure.

Returns:
String representation of tree
Post:
Generates a string representation of the AVLST

toString

public java.lang.String toString()
Returns a string representing tree

Overrides:
toString in class java.lang.Object
Returns:
String representation of tree
Post:
Generates a string representation of the AVLST

hashCode

public int hashCode()
Returns the hashCode of the value stored by this object.

Overrides:
hashCode in class AbstractStructure<ELTTYPE extends java.lang.Comparable<ELTTYPE>>
Returns:
The hashCode of the value stored by this object.