CSCI 151 with Dr. J, California State University, Chico

CSCI 151:  Algorithms and Data Structures


Main Page   Class Hierarchy   Compound List   File List   Compound Members  

RedBlackTree.h

Go to the documentation of this file.
00001 
00008         #ifndef RED_BLACK_TREE_H_
00009         #define RED_BLACK_TREE_H_
00010 
00011         #include "dsexceptions.h"
00012         #include <iostream>       // For NULL
00013 
00014         // Node and forward declaration because g++ does
00015         // not understand nested classes.
00016         template <class Comparable>
00017         class RedBlackTree;
00018 
00024         template <class Comparable>
00025         class RedBlackNode
00026         {
00027             Comparable    element;  
00028             RedBlackNode *left;     
00029             RedBlackNode *right;    
00030             int           color;    
00039             // c = 1 should be c = RedBlackTree<Comparable>::BLACK
00040             // But Visual 5.0 does not comprehend it.
00041             RedBlackNode( const Comparable & theElement = Comparable( ),
00042                               RedBlackNode *lt = NULL, RedBlackNode *rt = NULL,
00043                               int c = 1 )
00044               : element( theElement ), left( lt ), right( rt ), color( c ) { }
00045             friend class RedBlackTree<Comparable>;
00046         };
00047 
00048         // Red-black tree class
00049         //
00050         // CONSTRUCTION: with negative infinity object also
00051         //               used to signal failed finds
00052         //
00053         // ******************PUBLIC OPERATIONS*********************
00054         // void insert( x )       --> Insert x
00055         // void remove( x )       --> Remove x (unimplemented)
00056         // Comparable find( x )   --> Return item that matches x
00057         // Comparable findMin( )  --> Return smallest item
00058         // Comparable findMax( )  --> Return largest item
00059         // boolean isEmpty( )     --> Return true if empty; else false
00060         // void makeEmpty( )      --> Remove all items
00061         // void printTree( )      --> Print tree in sorted order
00062 
00068         template <class Comparable>
00069         class RedBlackTree
00070         {
00071           public:
00076             explicit RedBlackTree( const Comparable & negInf );
00081             RedBlackTree( const RedBlackTree & rhs );
00085             ~RedBlackTree( );
00086 
00091             const Comparable & findMin( ) const;
00096             const Comparable & findMax( ) const;
00102             const Comparable & find( const Comparable & x ) const;
00107             bool isEmpty( ) const;
00111             void printTree( ) const;
00112 
00116             void makeEmpty( );
00121             void insert( const Comparable & x );
00126             void remove( const Comparable & x );
00127 
00131             enum { 
00132                    RED,   
00133                    BLACK  
00134             };
00135 
00139             const RedBlackTree & operator=( const RedBlackTree & rhs );
00140 
00141           private:
00142             RedBlackNode<Comparable> *header;   // The tree header (contains negInf)
00143             const Comparable ITEM_NOT_FOUND;
00144             RedBlackNode<Comparable> *nullNode;
00145 
00146                 // Used in insert routine and its helpers (logically static)
00147             RedBlackNode<Comparable> *current;
00148             RedBlackNode<Comparable> *parent;
00149             RedBlackNode<Comparable> *grand;
00150             RedBlackNode<Comparable> *great;
00151 
00152                 // Usual recursive stuff
00153             void reclaimMemory( RedBlackNode<Comparable> *t ) const;
00154             void printTree( RedBlackNode<Comparable> *t ) const;
00155             RedBlackNode<Comparable> * clone( RedBlackNode<Comparable> * t ) const;
00156 
00157                 // Red-black tree manipulations
00158             void handleReorient( const Comparable & item );
00159             RedBlackNode<Comparable> * rotate( const Comparable & item,
00160                                         RedBlackNode<Comparable> *parent ) const;
00161             void rotateWithLeftChild( RedBlackNode<Comparable> * & k2 ) const;
00162             void rotateWithRightChild( RedBlackNode<Comparable> * & k1 ) const;
00163         };
00164 
00165         #include "RedBlackTree.cpp"
00166         #endif


Documentation generated using
doxygen