• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/collection/RedBlackTree.h

00001 /*
00002  *  Modified from code found on the net somewhere, need to re-google the attribution.
00003  */
00004 /*
00005  *   This file is part of the Standard Portable Library (SPL).
00006  *
00007  *   SPL is free software: you can redistribute it and/or modify
00008  *   it under the terms of the GNU General Public License as published by
00009  *   the Free Software Foundation, either version 3 of the License, or
00010  *   (at your option) any later version.
00011  *
00012  *   SPL is distributed in the hope that it will be useful,
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  *   GNU General Public License for more details.
00016  *
00017  *   You should have received a copy of the GNU General Public License
00018  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00019  */
00020 #ifndef _redblacktree_h
00021 #define _redblacktree_h
00022 
00023 #include <spl/types.h>
00024 #include <spl/Debug.h>
00025 #ifdef DEBUG
00026 #include <spl/collection/List.h>
00027 #endif
00028 #include <spl/Compare.h>
00029 #include <spl/Exception.h>
00030 #include <spl/collection/IEnumerable.h>
00031 #include <spl/Int32.h>
00032 #include <spl/IIterator.h>
00033 #include <spl/collection/List.h>
00034 #include <spl/Memory.h>
00035 
00041 template<typename K, typename V>
00042 class RBNode : public IMemoryValidate
00043 {
00044 protected:
00045         K m_key;
00046         V m_data;
00047 
00048 public:
00049         RBNode<K, V> *m_parent;
00050         RBNode<K, V> *m_left;
00051         RBNode<K, V> *m_right;
00052         bool m_isRed;
00053 
00054         RBNode(K key, V data)
00055         : m_key(key), m_data(data), m_parent(NULL), m_left(NULL), m_right(NULL), m_isRed(true)
00056         { 
00057         }
00058 
00059         RBNode(const RBNode<K, V>& node)
00060         : m_key(node.m_key), m_data(node.m_data), m_parent(node.m_parent), m_left(node.m_left), m_right(node.m_right), m_isRed(node.m_isRed)
00061         { 
00062         }
00063 
00064         virtual ~RBNode() {}
00065 
00066         RBNode<K, V>& operator =(const RBNode<K, V>& node)
00067         {
00068                 m_key = node.m_key;
00069                 m_data = node.m_data;
00070                 m_parent = node.m_parent;
00071                 m_left = node.m_left;
00072                 m_right = node.m_right;
00073                 m_isRed = node.m_isRed;
00074                 return *this;
00075         }
00076 
00077         inline V GetData() const { return m_data; }
00078         inline V& GetDataRef() { return m_data; }
00079         inline const V& GetDataRef() const { return m_data; }
00080         inline void SetData(V data) { m_data = data; }
00081         inline K GetKey() const { return m_key; }
00082         inline void SetKey(const K key) { m_key = key; }
00083         
00084         inline void SetBlack() { m_isRed = false; }
00085         inline void SetRed() { m_isRed = true; }
00086         inline bool IsBlack() const { return !m_isRed; }
00087         inline bool IsRed() const { return m_isRed; }
00088         inline void SetColor(bool isred) { m_isRed = isred; }
00089 
00090         inline RBNode<K, V> *GetLeft() const { return m_left; }
00091         inline RBNode<K, V> *GetRight() const { return m_right; }
00092         inline RBNode<K, V> *GetParent() const { return m_parent; }
00093         
00094         inline RBNode<K, V> *GrandParent() const
00095         {
00096                 ASSERT( NULL != m_parent && NULL != m_parent->m_parent );
00097                 return m_parent->m_parent;
00098         }
00099 
00100         inline RBNode<K, V> *Uncle() const
00101         {
00102                 if (m_parent == GrandParent()->m_left)
00103                 {
00104                         return GrandParent()->m_right;
00105                 }
00106                 else
00107                 {
00108                         return GrandParent()->m_left;
00109                 }
00110         }
00111 
00112         inline RBNode<K, V> *Sibling() const
00113         {
00114                 if ( NULL == m_parent )
00115                 {
00116                         return NULL;
00117                 }
00118                 if (this == m_parent->m_left)
00119                 {
00120                         return m_parent->m_right;
00121                 }
00122                 else
00123                 {
00124                         return m_parent->m_left;
00125                 }
00126         }
00127 
00128         bool SiblingIsBlack() const
00129         {
00130                 if ( NULL == m_parent )
00131                 {
00132                         return true;
00133                 }
00134                 if (this == m_parent->m_left)
00135                 {
00136                         return NULL == m_parent->m_right || m_parent->m_right->IsBlack();
00137                 }
00138                 else
00139                 {
00140                         return NULL == m_parent->m_left || m_parent->m_left->IsBlack();
00141                 }
00142         }
00143 
00144         bool SiblingIsRed() const
00145         {
00146                 if ( NULL == m_parent )
00147                 {
00148                         return false;
00149                 }
00150                 if (this == m_parent->m_left)
00151                 {
00152                         return (NULL == m_parent->m_right) ? false : m_parent->m_right->IsRed();
00153                 }
00154                 else
00155                 {
00156                         return (NULL == m_parent->m_left) ? false : m_parent->m_left->IsRed();
00157                 }
00158         }
00159 
00160         void SiblingSetRed() 
00161         {
00162                 if ( NULL == m_parent )
00163                 {
00164                         return;
00165                 }
00166                 if (this == m_parent->m_left)
00167                 {
00168                         if (NULL != m_parent->m_right) 
00169                         {
00170                                 m_parent->m_right->SetRed();
00171                         }
00172                 }
00173                 else
00174                 {
00175                         if (NULL != m_parent->m_left)
00176                         {
00177                                  m_parent->m_left->SetRed();
00178                         }
00179                 }
00180         }
00181 
00182         bool SiblingLeftIsBlack() const
00183         {
00184                 RBNode<K, V> *s = Sibling();
00185                 if ( NULL == s )
00186                 {
00187                         return true;
00188                 }
00189                 if ( NULL == (s = s->m_left) )
00190                 {
00191                         return true;
00192                 }
00193                 return s->IsBlack();
00194         }
00195 
00196         bool SiblingRightIsBlack() const
00197         {
00198                 RBNode<K, V> *s = Sibling();
00199                 if ( NULL == s )
00200                 {
00201                         return true;
00202                 }
00203                 if ( NULL == (s = s->m_right) )
00204                 {
00205                         return true;
00206                 }
00207                 return s->IsBlack();
00208         }
00209 
00210         bool SiblingLeftIsRed() const
00211         {
00212                 RBNode<K, V> *s = Sibling();
00213                 if ( NULL == s )
00214                 {
00215                         return false;
00216                 }
00217                 if ( NULL == (s = s->m_left) )
00218                 {
00219                         return false;
00220                 }
00221                 return s->IsRed();
00222         }
00223 
00224         bool SiblingRightIsRed() const
00225         {
00226                 RBNode<K, V> *s = Sibling();
00227                 if ( NULL == s )
00228                 {
00229                         return false;
00230                 }
00231                 if ( NULL == (s = s->m_right) )
00232                 {
00233                         return false;
00234                 }
00235                 return s->IsRed();
00236         }
00237 
00238         void SetRightChild(RBNode<K, V> *node) 
00239         {
00240                 ASSERT( NULL == m_right );
00241                 m_right = node;
00242                 m_right->m_parent = this;
00243         }
00244 
00245         void SetLeftChild(RBNode<K, V> *node)
00246         {
00247                 ASSERT( NULL == m_left );
00248                 m_left = node;
00249                 m_left->m_parent = this;
00250         }
00251 
00252         void Replace(RBNode<K, V> *node)
00253         {
00254                 if ( NULL != m_parent )
00255                 {
00256                         if ( m_parent->m_left == this )
00257                         {
00258                                 m_parent->m_left = node;
00259                         }
00260                         else
00261                         {
00262                                 ASSERT( m_parent->m_right == this );
00263                                 m_parent->m_right = node;
00264                         }
00265                 }
00266                 if ( NULL != node )
00267                 {
00268                         node->m_parent = m_parent;
00269                 }
00270         }
00271 
00272         void Prune()
00273         {
00274                 ASSERT( NULL == m_right && NULL == m_left );
00275                 if ( NULL != m_parent )
00276                 {
00277                         if ( m_parent->m_left == this )
00278                         {
00279                                 m_parent->m_left = NULL;
00280                         }
00281                         else
00282                         {
00283                                 ASSERT( m_parent->m_right == this );
00284                                 m_parent->m_right = NULL;
00285                         }
00286                 }
00287         }
00288 
00289         void RotateLeft()
00290         {
00291                 //Let Y be X's right child.
00292                 RBNode<K, V> *y = m_right;
00293                 //Set Y to be the new root.
00294                 y->m_parent = m_parent;
00295                 if ( NULL != m_parent )
00296                 {
00297                         if ( this == m_parent->m_left )
00298                         {
00299                                 m_parent->m_left = y;
00300                         }
00301                         else
00302                         {
00303                                 ASSERT(m_parent->m_right == this);
00304                                 m_parent->m_right = y;
00305                         }
00306                 }
00307                 //Set X's right child to be Y's left child.
00308                 if ( NULL != (m_right = y->m_left) )
00309                 {
00310                         m_right->m_parent = this;
00311                 }
00312                 //Set Y's left child to be X.
00313                 y->m_left = this;
00314                 m_parent = y;
00315         }
00316 
00317         void RotateRight()
00318         {
00319                 //Let Y be X's left child.
00320                 RBNode<K, V> *y = m_left;
00321                 //Set Y to be the new root.
00322                 y->m_parent = m_parent;
00323                 if ( NULL != m_parent )
00324                 {
00325                         if ( this == m_parent->m_left )
00326                         {
00327                                 m_parent->m_left = y;
00328                         }
00329                         else
00330                         {
00331                                 ASSERT(m_parent->m_right == this);
00332                                 m_parent->m_right = y;
00333                         }
00334                 }
00335                 //Set X's left child to be Y's right child.
00336                 if ( NULL != (m_left = y->m_right) )
00337                 {
00338                         m_left->m_parent = this;
00339                 }
00340                 //Set Y's right child to be X.
00341                 y->m_right = this;
00342                 m_parent = y;
00343         }
00344 #ifdef DEBUG
00345         void ValidateMem() const
00346         {
00347                 ValidateType( m_key );
00348                 ValidateType( m_data );
00349         }
00350         inline void CheckMem() const
00351         {
00352                 ValidateType( m_key );
00353                 ValidateType( m_data );
00354         }
00355 #endif
00356 };
00357 
00359 template<typename K, typename V>
00360 class RedBlackTreeIterator : public IIterator<RBNode<K, V> *>
00361 {
00362 private:
00363         List<RBNode<K, V> * > m_stack;
00364         RBNode<K, V> *m_cnode;
00365 
00366 public:
00367         RedBlackTreeIterator()
00368         : m_stack(), m_cnode(NULL)
00369         {
00370         }
00371 
00372         RedBlackTreeIterator(RBNode<K, V> *cnode)
00373         : m_stack(), m_cnode(cnode)
00374         {
00375         }
00376 
00377         RedBlackTreeIterator(const RedBlackTreeIterator<K, V>& ti)
00378         : m_stack(ti.m_stack), m_cnode(ti.m_cnode)
00379         {
00380         }
00381 
00382         virtual bool Next( )
00383         {
00384                 if (NULL == m_cnode)
00385                 {
00386                         return false;
00387                 }
00388                 if ( NULL != m_cnode->m_left )
00389                 {
00390                         if ( NULL != m_cnode->m_right )
00391                         {
00392                                 m_stack.Add( m_cnode->m_right );
00393                         }
00394                         m_cnode = m_cnode->m_left;
00395                         return true;
00396                 }
00397                 else if ( NULL != m_cnode->m_right )
00398                 {
00399                         m_cnode = m_cnode->m_right;
00400                         return true;
00401                 }
00402                 else if ( m_stack.Count() > 0 )
00403                 {
00404                         m_cnode = m_stack.Pop();
00405                         return true;
00406                 }
00407                 else
00408                 {
00409                         m_cnode = NULL;
00410                         return false;
00411                 }
00412         }
00413 
00414         virtual bool Prev( )
00415         {
00416                 throw new NotImplementedException();
00417         }
00418 
00419         virtual RBNode<K, V> * Current()
00420         {
00421                 return m_cnode;
00422         }
00423 
00424         virtual RBNode<K, V> *& CurrentRef()
00425         {
00426                 return m_cnode;
00427         }
00428 };
00429 
00431 template<typename K, typename V>
00432 class RedBlackTree : public IEnumerable<V>
00433 {
00434 private:
00435         RBNode<K, V> *m_root;
00436         int m_count;
00437 
00438 protected:
00439         virtual RefCountPtr<IIterator<V> > IteratorPtr()
00440         {
00441                 return RefCountPtr<IIterator<V> >((IIterator<V> *)new RedBlackTreeIterator<K, V>(m_root));
00442         }
00443 
00444 public:
00445 
00447         class Iterator : public IIterator<V>
00448         {
00449         private:
00450                 RedBlackTreeIterator<K, V> m_iter;
00451 
00452         public:
00453                 Iterator()
00454                 : m_iter()
00455                 {
00456                 }
00457 
00458                 Iterator(RBNode<K, V> *cnode)
00459                 : m_iter(cnode)
00460                 {
00461                 }
00462 
00463                 Iterator(const Iterator& ti)
00464                 : m_iter(ti)
00465                 {
00466                 }
00467 
00468                 virtual bool Next( )
00469                 {
00470                         return m_iter.Next();
00471                 }
00472 
00473                 virtual bool Prev( )
00474                 {
00475                         return m_iter.Prev();
00476                 }
00477 
00478                 virtual V Current()
00479                 {
00480                         return m_iter.Current()->GetData();
00481                 }
00482 
00483                 virtual V& CurrentRef()
00484                 {
00485                         return m_iter.Current()->GetDataRef();
00486                 }
00487         };
00488 
00489         RedBlackTree()
00490         : m_root(NULL), m_count(0)
00491         {
00492         }
00493 
00494         RedBlackTree(const RedBlackTree<K, V>& rbt)
00495         : m_root(NULL), m_count(0)
00496         {
00497                 *this = rbt;
00498         }
00499 
00500         ~RedBlackTree()
00501         {
00502                 Clear();
00503         }
00504 
00505         RedBlackTree<K, V>& operator =(const RedBlackTree<K, V>& rbt)
00506         {
00507                 Clear();
00508 
00509                 if ( NULL == m_root )
00510                 {
00511                         return;
00512                 }
00513                 List<RBNode<K, V> * > stack;
00514                 RBNode<K, V> *cnode = m_root;
00515                 while ( NULL != cnode )
00516                 {
00517                         Insert( cnode.GetKey(), cnode.GetData() );
00518 
00519                         if ( NULL != cnode->m_left )
00520                         {
00521                                 if ( NULL != cnode->m_right )
00522                                 {
00523                                         stack.Add( cnode->m_right );
00524                                 }
00525                                 cnode = cnode->m_left;
00526                         }
00527                         else if ( NULL != cnode->m_right )
00528                         {
00529                                 cnode = cnode->m_right;
00530                         }
00531                         else if ( stack.Count() > 0 )
00532                         {
00533                                 cnode = stack.Pop();
00534                         }
00535                         else
00536                         {
00537                                 cnode = NULL;
00538                         }
00539                 }
00540         }
00541 
00542         inline RedBlackTreeIterator<K, V> NodeIterator()
00543         {
00544                 return RedBlackTreeIterator<K, V>(m_root);
00545         }
00546 
00547         inline Iterator Begin() const
00548         {
00549                 return Iterator(m_root);
00550         }
00551 
00552         int Count() const
00553         {
00554                 return m_count;
00555         }
00556 
00557         void Insert( const K key, V data )
00558         {
00559                 RBNode<K, V> *cnode = m_root;
00560                 m_count++;
00561                 if ( NULL == cnode )
00562                 {
00563                         if ( NULL == (m_root = CreateNode(key, data)) )
00564                         {
00565                                 throw OutOfMemoryException();
00566                         }
00567                         m_root->SetBlack();
00568                         return;
00569                 }
00570                 while ( true )
00571                 {
00572                         int cmp = (int)Compare::Cmp(cnode->GetKey(), key );
00573                         if ( 0 == cmp )
00574                         {
00575                                 throw new InvalidArgumentException("Duplicate key");
00576                         }
00577                         if ( 0 > cmp )
00578                         {
00579                                 if ( NULL == cnode->GetRight() )
00580                                 {
00581                                         RBNode<K, V> *rt = CreateNode(key, data);
00582                                         if ( NULL == rt )
00583                                         {
00584                                                 throw OutOfMemoryException();
00585                                         }
00586                                         cnode->SetRightChild(rt);
00587                                         InsertCase1(rt);
00588                                         break;
00589                                 }
00590                                 else
00591                                 {
00592                                         cnode = cnode->GetRight();
00593                                 }
00594                         }
00595                         else
00596                         {
00597                                 if ( NULL == cnode->GetLeft() )
00598                                 {
00599                                         RBNode<K, V> *rt = CreateNode(key, data);
00600                                         if ( NULL == rt )
00601                                         {
00602                                                 throw OutOfMemoryException();
00603                                         }
00604                                         cnode->SetLeftChild(rt);
00605                                         InsertCase1(rt);
00606                                         break;
00607                                 }
00608                                 else
00609                                 {
00610                                         cnode = cnode->GetLeft();
00611                                 }
00612                         }
00613                 }
00614                 VerifyTree();
00615         }
00616 
00617         void Remove( const K& key )
00618         {
00619                 RBNode<K, V> *n = FindNode( key );
00620                 if ( NULL == n )
00621                 {
00622                         return;
00623                 }
00624                 if ( n->GetRight() != NULL && n->GetLeft() != NULL )
00625                 {
00626                         // Node has two children - get max of left subtree
00627                         RBNode<K, V> *temp = n->m_left;
00628                         ASSERT_PTR( temp );
00629                         while (NULL != temp->m_right) 
00630                         {
00631                                 temp = temp->m_right;
00632                         }
00633                         K tkey = temp->GetKey();
00634                         V tdata = temp->GetData();
00635                         Remove( tdata );
00636                         n->SetData(tdata);
00637                         n->SetKey(tkey);
00638                         VerifyTree();
00639                         return;
00640                 }
00641                 m_count--;
00642                 /* Precondition: n has at most one non-null child */
00643                 RBNode<K, V> *child = (NULL == n->m_right) ? n->m_left : n->m_right;
00644                 if ( n == m_root )
00645                 {
00646                         if ( NULL != child )
00647                         {
00648                                 ASSERT( n == child->m_parent );
00649                                 child->m_parent = NULL;
00650                         }
00651                         m_root = child;
00652                 }
00653                 else
00654                 {
00655                         n->Replace(child);
00656                 }
00657                 if (n->IsBlack() && NULL != child)
00658                 {
00659                         if (child->IsRed())
00660                         {
00661                                 child->SetBlack();
00662                         }
00663                         else
00664                         {
00665                                 DeleteCase1(child);
00666                         }
00667                 }
00668                 delete n;
00669                 VerifyTree();
00670         }
00671 
00672         void Clear()
00673         {
00674                 while ( m_count > 0 )
00675                 {
00676                         Remove( m_root->GetKey() );
00677                 }
00678         }
00679 
00680         V Find( const K& key ) const
00681         {
00682                 RBNode<K, V> *cnode = FindNode( key );
00683                 if ( NULL != cnode )
00684                 {
00685                         return cnode->GetData();
00686                 }
00687                 return V();
00688         }
00689 
00690         V& FindRef( const K& key ) const
00691         {
00692                 RBNode<K, V> *cnode = FindNode( key );
00693                 if ( NULL != cnode )
00694                 {
00695                         return cnode->GetDataRef();
00696                 }
00697                 throw InvalidArgumentException("key not found");
00698         }
00699 
00700         bool ContainsKey( const K& key ) const
00701         {
00702                 return NULL != FindNode( key );
00703         }
00704 
00705 #ifdef DEBUG
00706         void VerifyTree() const
00707         {
00708                 int count = 0;
00709                 if ( NULL == m_root )
00710                 {
00711                         ASSERT( m_count == 0 );
00712                         return;
00713                 }
00714                 ASSERT( NULL == m_root->m_parent );
00715 
00716                 List<RBNode<K, V> * > stack;
00717                 RBNode<K, V> *cnode = m_root;
00718                 while ( NULL != cnode )
00719                 {
00720                         ASSERT_PTR( cnode );
00721                         count++;
00722                         if ( NULL != cnode->m_parent )
00723                         {
00724                                 ASSERT( cnode == cnode->m_parent->m_left || cnode == cnode->m_parent->m_right );
00725                         }
00726                         else
00727                         {
00728                                 ASSERT( cnode == m_root );
00729                         }
00730                         if ( cnode->IsRed() )
00731                         {
00732                                 ASSERT( cnode->m_left == NULL || cnode->m_left->IsBlack() );
00733                                 ASSERT( cnode->m_right == NULL || cnode->m_right->IsBlack() );
00734                         }
00735                         if ( NULL != cnode->m_left )
00736                         {
00737                                 if ( NULL != cnode->m_right )
00738                                 {
00739                                         stack.Add( cnode->m_right );
00740                                 }
00741                                 cnode = cnode->m_left;
00742                         }
00743                         else if ( NULL != cnode->m_right )
00744                         {
00745                                 cnode = cnode->m_right;
00746                         }
00747                         else if ( stack.Count() > 0 )
00748                         {
00749                                 cnode = stack.Pop();
00750                         }
00751                         else
00752                         {
00753                                 cnode = NULL;
00754                         }
00755                 }
00756                 ASSERT( m_count == count );
00757         }
00758 
00759         virtual void ValidateMem() const
00760         {
00761                 if ( NULL == m_root )
00762                 {
00763                         return;
00764                 }
00765                 List<RBNode<K, V> * > stack;
00766                 RBNode<K, V> *cnode = m_root;
00767                 while ( NULL != cnode )
00768                 {
00769                         ASSERT_PTR( cnode );
00770                         cnode->ValidateMem();
00771                         if ( NULL != cnode->m_left )
00772                         {
00773                                 if ( NULL != cnode->m_right )
00774                                 {
00775                                         stack.Add( cnode->m_right );
00776                                 }
00777                                 cnode = cnode->m_left;
00778                         }
00779                         else if ( NULL != cnode->m_right )
00780                         {
00781                                 cnode = cnode->m_right;
00782                         }
00783                         else if ( stack.Count() > 0 )
00784                         {
00785                                 cnode = stack.Pop();
00786                         }
00787                         else
00788                         {
00789                                 cnode = NULL;
00790                         }
00791                 }
00792         }
00793 
00794         virtual void CheckMem() const
00795         {
00796                 if ( NULL == m_root )
00797                 {
00798                         return;
00799                 }
00800                 List<RBNode<K, V> * > stack;
00801                 RBNode<K, V> *cnode = m_root;
00802                 while ( NULL != cnode )
00803                 {
00804                         DEBUG_NOTE_MEM_ALLOCATION( cnode );
00805                         cnode->CheckMem();
00806                         if ( NULL != cnode->m_left )
00807                         {
00808                                 if ( NULL != cnode->m_right )
00809                                 {
00810                                         stack.Add( cnode->m_right );
00811                                 }
00812                                 cnode = cnode->m_left;
00813                         }
00814                         else if ( NULL != cnode->m_right )
00815                         {
00816                                 cnode = cnode->m_right;
00817                         }
00818                         else if ( stack.Count() > 0 )
00819                         {
00820                                 cnode = stack.Pop();
00821                         }
00822                         else
00823                         {
00824                                 cnode = NULL;
00825                         }
00826                 }
00827         }
00828 #else
00829         inline void VerifyTree() const {}
00830 #endif
00831 
00832 protected:
00833         inline RBNode<K, V> *CreateNode(const K key, V data)
00834         {
00835                 return new RBNode<K, V>(key, data);
00836         }
00837 
00838         void RotateLeft(RBNode<K, V> *node)
00839         {
00840                 node->RotateLeft();
00841                 if ( node == m_root )
00842                 {
00843                         m_root = node->m_parent;
00844                 }
00845         }
00846 
00847         void RotateRight(RBNode<K, V> *node)
00848         {
00849                 node->RotateRight();
00850                 if ( node == m_root )
00851                 {
00852                         m_root = node->m_parent;
00853                 }
00854         }
00855 
00856         RBNode<K, V> *FindNode( const K& key ) const
00857         {
00858                 VerifyTree();
00859                 RBNode<K, V> *cnode = m_root;
00860                 while ( NULL != cnode )
00861                 {
00862                         int cmp = (int)Compare::Cmp(cnode->GetKey(), key );
00863                         if ( 0 == cmp )
00864                         {
00865                                 return cnode;
00866                         }
00867                         if ( 0 > cmp )
00868                         {
00869                                 cnode = cnode->GetRight();
00870                         }
00871                         else
00872                         {
00873                                 cnode = cnode->GetLeft();
00874                         }
00875                 }
00876                 return NULL;
00877         }
00878 
00879         void DeleteCase1(RBNode<K, V> *n)
00880         {
00881                 if (n->m_parent == NULL)
00882                 {
00883                         return;
00884                 }
00885                 else
00886                 {
00887                         DeleteCase2(n);
00888                 }
00889         }
00890 
00891         void DeleteCase2(RBNode<K, V> *n)
00892         {
00893                 if (n->SiblingIsRed()) 
00894                 {
00895                         n->m_parent->SetRed();
00896                         n->Sibling()->SetBlack();
00897                         if (n == n->m_parent->m_left)
00898                         {
00899                                 RotateLeft(n->m_parent);
00900                         }
00901                         else
00902                         {
00903                                 RotateRight(n->m_parent);
00904                         }
00905                 }
00906                 DeleteCase3(n);
00907         }
00908 
00909         void DeleteCase3(RBNode<K, V> *n)
00910         {
00911                 if (n->m_parent->IsBlack() &&
00912                         n->SiblingIsBlack() &&
00913                         n->SiblingLeftIsBlack() &&
00914                         n->SiblingRightIsBlack())
00915                 {
00916                         n->SiblingSetRed();
00917                         DeleteCase1(n->m_parent);
00918                 }
00919                 else
00920                 {
00921                         DeleteCase4(n);
00922                 }
00923         }
00924 
00925         void DeleteCase4(RBNode<K, V> *n)
00926         {
00927                 if (n->m_parent->IsRed() &&
00928                         n->SiblingIsBlack() &&
00929                         n->SiblingLeftIsBlack() &&
00930                         n->SiblingRightIsBlack())
00931                 {
00932                         n->SiblingSetRed();
00933                         n->m_parent->SetBlack();
00934                 }
00935                 else
00936                 {
00937                         DeleteCase5(n);
00938                 }
00939         }
00940 
00941         void DeleteCase5(RBNode<K, V> *n)
00942         {
00943                 if (n == n->m_parent->m_left &&
00944                         n->SiblingIsBlack() &&
00945                         n->SiblingLeftIsRed() &&
00946                         n->SiblingRightIsBlack())
00947                 {
00948                         ASSERT(NULL != n->Sibling() && NULL != n->Sibling()->m_left);
00949                         n->Sibling()->SetRed();
00950                         n->Sibling()->m_left->SetBlack();
00951                         RotateRight(n->Sibling());
00952                 }
00953                 else if (n == n->m_parent->m_right &&
00954                                  n->SiblingIsBlack() &&
00955                                  n->SiblingRightIsRed() &&
00956                                  n->SiblingLeftIsBlack())
00957                 {
00958                         ASSERT(NULL != n->Sibling() && NULL != n->Sibling()->m_right);
00959                         n->Sibling()->SetRed();
00960                         n->Sibling()->m_right->SetBlack();
00961                         RotateLeft(n->Sibling());
00962                 }
00963                 DeleteCase6(n);
00964         }
00965 
00966         void DeleteCase6(RBNode<K, V> *n)
00967         {
00968                 ASSERT(NULL != n->Sibling());
00969                 n->Sibling()->SetColor(n->m_parent->IsRed());
00970                 n->m_parent->SetBlack();
00971                 if (n == n->m_parent->m_left) 
00972                 {
00973                         /* Here, sibling(n)->right->color == RED */
00974                         ASSERT(NULL != n->Sibling()->m_right);
00975                         n->Sibling()->m_right->SetBlack();
00976                         RotateLeft(n->m_parent);
00977                 }
00978                 else
00979                 {
00980                         /* Here, sibling(n)->left->color == RED */
00981                         ASSERT(NULL != n->Sibling()->m_left);
00982                         n->Sibling()->m_left->SetBlack();
00983                         RotateRight(n->m_parent);
00984                 }
00985         }
00986 
00987         void InsertCase1(RBNode<K, V> *n)
00988         {
00989                 if (n->m_parent == NULL)
00990                 {
00991                         n->m_isRed = false;
00992                 }
00993                 else
00994                 {
00995                         InsertCase2(n);
00996                 }
00997         }
00998 
00999         void InsertCase2(RBNode<K, V> *n)
01000         {
01001                 if (!n->m_parent->m_isRed)
01002                 {
01003                         return; /* Tree is still valid */
01004                 }
01005                 else
01006                 {
01007                         InsertCase3(n);
01008                 }
01009         }
01010         
01011         void InsertCase3(RBNode<K, V> *n)
01012         {
01013                 RBNode<K, V> *uncle = n->Uncle();
01014                 if (uncle != NULL && uncle->m_isRed) 
01015                 {
01016                         n->m_parent->m_isRed = false;
01017                         uncle->m_isRed = false;
01018                         n->GrandParent()->m_isRed = true;
01019                         InsertCase1(n->GrandParent());
01020                 }
01021                 else
01022                 {
01023                         InsertCase4(n);
01024                 }
01025         }
01026         
01027         void InsertCase4(RBNode<K, V> *n)
01028         {
01029                 if (n == n->m_parent->m_right && n->m_parent == n->GrandParent()->m_left) 
01030                 {
01031                         RotateLeft(n->m_parent);
01032                         n = n->m_left;
01033                 } 
01034                 else if (n == n->m_parent->m_left && n->m_parent == n->GrandParent()->m_right) 
01035                 {
01036                         RotateRight(n->m_parent);
01037                         n = n->m_right;
01038                 }
01039                 InsertCase5(n);
01040         }
01041 
01042         void InsertCase5(RBNode<K, V> *n)
01043         {
01044                 n->m_parent->m_isRed = false;
01045                 n->GrandParent()->m_isRed = true;
01046                 if (n == n->m_parent->m_left && n->m_parent == n->GrandParent()->m_left) 
01047                 {
01048                         RotateRight(n->GrandParent());
01049                 } 
01050                 else 
01051                 {
01052                         /* Here, n == n->parent->right && n->parent == grandparent(n)->right */
01053                         RotateLeft(n->GrandParent());
01054                 }
01055         }
01056 };
01057 
01058 REGISTER_TYPEOF2( 350, RedBlackTree<int,int> );
01059 REGISTER_TYPEOF2( 351, RedBlackTree<String,String> );
01060 REGISTER_TYPEOF2( 590, RedBlackTree<int,Int32> );
01061 
01064 #endif