7 #include <KrisLibrary/utils/cmp_func.h> 11 enum Color { Black, Red };
13 enum Lookup { None, Equal,GTEQ,LTEQ,Less,Greater,Next,Prev};
17 enum Visit { PreOrder, PostOrder, EndOrder, Leaf };
19 virtual void Visit(
const T& key, Visit visit,
int level) {}
34 void dumptree(std::ostream& out,
int n);
35 MyT* successor()
const;
36 MyT* predecessor()
const;
38 inline void setColor(Color c) {
if(
this==NULL) assert(c==Black);
else color=c; }
39 inline Color getColor()
const {
if(
this==NULL)
return Black;
return color; }
40 inline bool hasColor(Color c)
const {
return c==getColor(); }
51 template <
class T,
class Cmp=std::cmp_func<T> >
61 inline iterator(NodeT* n) { curp=n; }
63 inline operator const T* ()
const { assert(curp);
return &curp->key; }
64 inline const T* operator->()
const { assert(curp);
return &curp->key; }
65 inline void operator++() {
if(curp) curp=curp->successor(); }
66 inline void operator--() {
if(curp) curp=curp->predecessor(); }
67 inline void operator++(
int) {
if(curp) curp=curp->successor(); }
68 inline void operator--(
int) {
if(curp) curp=curp->predecessor(); }
69 inline bool operator == (
const iterator& i)
const {
return i.curp==curp; }
70 inline bool operator != (
const iterator& i)
const {
return i.curp!=curp; }
71 inline operator bool()
const {
return curp!=NULL; }
72 inline bool operator !()
const {
return curp==NULL; }
73 inline bool end()
const {
return curp==NULL; }
80 bool checkValid()
const;
81 inline bool empty()
const {
return root==NULL; }
84 inline void erase(
const iterator& x) { _erase(x.curp); }
87 iterator lookup(Lookup type,
const T&);
97 NodeT* _insert(
const T&);
98 NodeT* _find(
const T&)
const;
99 NodeT* _lookup(Lookup,
const T&)
const;
100 void _erase(NodeT* x);
101 void _erase_fix(NodeT* x,NodeT* p);
102 void _left_rotate(NodeT* x);
103 void _right_rotate(NodeT* x);
115 :left(NULL),right(NULL),up(NULL),color(Black)
120 :left(NULL),right(NULL),up(NULL),key(_key),color(Black)
126 if(left)
delete left;
127 if(right)
delete right;
148 for (y=right; y->left!=NULL; y=y->left);
158 while(y!=NULL && x==y->right)
179 for (y=left; y->right!=NULL; y=y->right);
189 while(y!=NULL && x==y->left)
201 if (left==NULL && right==NULL)
208 if(left != NULL) left->walk(callback, level+1);
210 if(right != NULL) right->walk(callback, level+1);
220 if (!left->hasColor(Black) && !right->hasColor(Black))
229 if (left->up !=
this)
235 if (!left->isValid())
241 if (right->up !=
this)
247 if (!right->isValid())
259 int nleft=left->count_black(), nright=right->count_black();
260 if (nleft==-1 || nright==-1)
269 if (hasColor(Black)) nleft++;
278 out<<
"Tree: "<<n<<
" "<<(
unsigned int)
this<<
279 ": color="<<(hasColor(Black)?
"Black" :
"Red") <<
", key="<<key<<
280 ", left="<<(
unsigned int)left<<
", right="<<(
unsigned int)right<<std::endl;
281 left->dumptree(out,n);
282 right->dumptree(out,n);
303 template <
class T,
class Cmp>
308 template <
class T,
class Cmp>
314 template <
class T,
class Cmp>
317 if (root)
delete root;
321 template <
class T,
class Cmp>
332 template <
class T,
class Cmp>
335 _walk(root, callback);
338 template <
class T,
class Cmp>
343 if(start)
while(start->left!=NULL) start=start->left;
347 template <
class T,
class Cmp>
351 return iterator(_lookup(mode, key));
354 template <
class T,
class Cmp>
359 if(start)
while(start->left!=NULL) start=start->left;
363 template <
class T,
class Cmp>
368 if(start)
while(start->right!=NULL) start=start->right;
377 template <
class T,
class Cmp>
389 cmp=cmpFunc(key, x->key);
412 cmp=cmpFunc(z->key, y->key);
435 while(x != root && x->up->hasColor(Red))
438 if (x->up == x->up->up->left)
442 if (y->hasColor(Red))
445 x->up->setColor(Black);
449 x->up->up->setColor(Red);
457 if (x == x->up->right)
465 x->up->setColor(Black);
467 x->up->up->setColor(Red);
469 _right_rotate(x->up->up);
479 if (y->hasColor(Red))
481 x->up->setColor(Black);
483 x->up->up->setColor(Red);
489 if (x == x->up->left)
495 x->up->setColor(Black);
496 x->up->up->setColor(Red);
497 _left_rotate(x->up->up);
503 root->setColor(Black);
507 template <
class T,
class Cmp>
515 cmp=cmpFunc(key, x->key);
527 template <
class T,
class Cmp>
538 while(x!=NULL && found==0)
541 cmp=cmpFunc(key, x->key);
551 if (found && (mode==Equal || mode==GTEQ || mode==LTEQ))
554 if (!found && (mode==Equal || mode==Next || mode==Prev))
557 if (mode==GTEQ || (!found && mode==Greater))
560 return(y->successor());
565 if (mode==LTEQ || (!found && mode==Less))
568 return(y->predecessor());
573 if (mode==Next || (found && mode==Greater))
574 return(x->successor());
576 if (mode==Prev || (found && mode==Less))
577 return(x->predecessor());
598 template <
class T,
class Cmp>
604 assert(x->right!=NULL);
626 if (x == x->up->left)
643 template <
class T,
class Cmp>
649 assert(y->left!=NULL);
657 if (x->right != NULL)
671 if (y == y->up->left)
688 template <
class T,
class Cmp>
693 if (z->left == NULL || z->right == NULL)
717 if (y->hasColor(Black))
729 if(z->up->left == z) z->up->left=y;
730 else { assert(z->up->right == z); z->up->right=y; }
733 if(z->left) z->left->up = y;
735 if(z->right) z->right->up = y;
737 y->setColor(z->getColor());
745 template <
class T,
class Cmp>
749 while (x!=root && x->hasColor(Black))
754 if (w->hasColor(Red))
762 if (w->left->hasColor(Black) && w->right->hasColor(Black))
770 if (w->right->hasColor(Black))
772 w->left->setColor(Black);
779 w->setColor(p->getColor());
781 w->right->setColor(Black);
789 if (w->hasColor(Red))
797 if (w->right->hasColor(Black) && w->left->hasColor(Black))
805 if (w->left->hasColor(Black))
807 w->right->setColor(Black);
813 w->setColor(p->getColor());
815 w->left->setColor(Black);
826 template <
class T,
class Cmp>
831 LOG4CXX_ERROR(KrisLibrary::logger(),
"Root up pointer not NULL" <<
"\n");
832 root->dumptree(std::cerr,0);
836 if (!root->isValid())
838 root->dumptree(std::cerr,0);
842 if (root->count_black()==-1)
844 LOG4CXX_ERROR(KrisLibrary::logger(),
"Error in black count"<<
"\n");
845 root->dumptree(std::cerr,0);
Definition: RedBlack.h:58
Definition: RedBlack.h:23
Definition: RedBlack.h:16
The logging system used in KrisLibrary.
Definition: RedBlack.h:52