// Most of the code of the red/black tree is taken from class rset_ I <t> {T key; rset_ I <t> * father; rset_ I <t> * left; rset_ I <t> * right; int Col; // 1 is red, 0 is black}; Class rset <t> {rset_ I <t> * m_root; rset_ I <t> m_nil; int m_count ;~ Rset <t> () {clear () ;}rset <t> () {Init () ;}rset <t> (rset <t> &) {Init (); for P =. begin; P! =. End; P = next (p) insert (* P)} rset <t> (rset <t> A) {Init (); for P =. begin; P! =. End; P = next (p) insert (* P)} operator = (rset <t> & A) {clear (); for P =. begin; P! =. End; P =. next (p) insert (* P)} T & operator [] (int n) {T * P = index (n); return * P;} void Init () {m_nil.father = & m_nil; m_nil.left = & m_nil; m_nil.right = & m_nil; m_nil.col = 0; m_root = & m_nil; m_count = 0;} void clear () {clear_x (m_root ); init ();} void clear_x (rset_ I <t> * X) {If (x = & m_nil) {return;} clear_x (X-> left ); clear_x (X-> right); r_delete <rset_ I <t> X;} rbool empty () {return m_count = 0;} int count () {return m_count ;} void left_rota Te (rset_ I <t> * X) {rset_ I <t> * Y; If (X-> right ==& m_nil) {return;} y = x-> right; x-> right = Y-> left; y-> left-> father = x; y-> father = x-> father; if (X-> father = & m_nil) {m_root = y;} Elif (X-> father-> left = X) {X-> father-> left = y;} else {X-> father-> right = y;} y-> left = x; X-> father = y ;} void right_rotate (rset_ I <t> * X) {rset_ I <t> * Y; If (X-> left = & m_nil) {return;} y = x-> left; x-> left = Y-> right; y-> right-> father = x; y-> father = x-> father; If (X-> fa Ther = & m_nil) {m_root = y;} Elif (X-> father-> left = x) {X-> father-> left = y ;} else {X-> father-> right = y;} y-> right = x; X-> father = y;} void insert_fixup (rset_ I <t> * z) {rset_ I <t> * Y; while (Z-> father-> Col = 1) {If (Z-> father = z-> father-> left) {Y = z-> father-> right; if (Y-> Col = 1) {z-> father-> Col = 0; y-> Col = 0; z-> father-> Col = 1; Z = z-> father;} else {If (Z = z-> father-> right) {z = z-> father; lef T_rotate (z);} Z-> father-> Col = 0; Z-> father-> Col = 1; right_rotate (Z-> father) ;}} else {Y = z-> father-> left; If (Y-> Col = 1) {z-> father-> Col = 0; y-> Col = 0; Z-> father-> Col = 1; z = z-> father;} else {If (Z = z-> father-> left) {z = z-> father; right_rotate (z );} z-> father-> Col = 0; Z-> father-> Col = 1; left_rotate (Z-> father );}}} m_root-> Col = 0;} void insert_p (R Set_ I <t> * z) {rset_ I <t> * Y; rset_ I <t> * X; y = & m_nil; X = m_root; while (X! = & M_nil) {Y = x; If (Z-> key <X-> key) {x = x-> left ;} else {x = x-> right;} Z-> father = y; If (y = & m_nil) {m_root = z ;} elif (Z-> key <Y-> key) {Y-> left = z;} else {Y-> right = z;} Z-> left = & m_nil; z-> right = & m_nil; Z-> Col = 1; insert_fixup (z);} void insert (const T &) {rset_ I <t> * P = r_new <rset_ I <t> (1) P-> key = A; insert_p (p); m_count ++ ;} void insert (t a) {rset_ I <t> * P = r_new <rset_ I <t> (1) P-> key = A; insert_p (P ); m_count ++;} void insert_c (const T & A) {if (Exist (A) {return;} insert (a);} void insert_c (t a) {If (exist (A) {return;} insert ();} void test (rset_ I <t> * z) {If (Z! = & M_nil) {test (Z-> left); Z-> key. printl (); test (Z-> right) ;}} void erase_fixup (rset_ I <t> * X) {rset_ I <t> * w; while (X! = M_root & X-> Col = 0) {If (x = x-> father-> left) {W = x-> father-> right; if (W-> Col = 1) {w-> Col = 0; X-> father-> Col = 1; left_rotate (X-> father ); W = x-> father-> right;} If (W-> left-> Col = 0 & W-> right-> Col = 0) {w-> Col = 1; X = x-> father;} else {If (W-> right-> Col = 0) {w-> left-> Col = 0; W-> Col = 1; right_rotate (w); W = x-> father-> right ;} w-> Col = x-> father-> Col; X-> father-> Col = 0; W-> right-> Col = 0; left_rotate (X-> father); X = m_root;} else {W = x-> father-> Left; If (W-> Col = 1) {w-> Col = 0; X-> father-> Col = 1; right_rotate (X-> father ); W = x-> father-> left;} If (W-> right-> Col = 0 & W-> left-> Col = 0) {w-> Col = 1; X = x-> father;} else {If (W-> left-> Col = 0) {w-> right-> Col = 0; W-> Col = 1; left_rotate (w); W = x-> father-> left ;} w-> Col = x-> father-> Col; X-> father-> Col = 0; W-> left-> Col = 0; right_rotate (X-> father); X = m_root; }}x-> Col = 0;} rset_ I <t> * erase_p (rset_ I <t> * z) {rset_ I <t> * Y; rset_ I <t> * X; If (Z-> Left = & m_nil | Z-> right = & m_nil) {Y = z;} else {Y = successor (z);} If (Y-> left! = & M_nil) {x = Y-> left;} else {x = Y-> right;} X-> father = Y-> father; if (Y-> father = & m_nil) {m_root = x;} Elif (y = Y-> father-> left) {Y-> father-> left = x;} else {Y-> father-> right = x;} If (y! = Z) {z-> key = Y-> key;} If (Y-> Col = 0) {erase_fixup (x);} return y ;} void erase (const T & A) {rset_ I <t> * x = search (m_root, a); If (X! = & M_nil) {x = erase_p (x); r_delete <rset_ I <t> xm_count -- ;}} void erase (T) {rset_ I <t> * x = search (m_root, a); If (X! = & M_nil) {x = erase_p (x); r_delete <rset_ I <t> xm_count -- ;}} bool exist (const T & A) {return search (m_root, a )! = & M_nil;} rset_ I <t> * successor (rset_ I <t> * X) {rset_ I <t> * Y; If (X-> right! = & M_nil) {return minimum (X-> right);} y = x-> father; while (y! = & M_nil & X = Y-> right) {x = y; y = Y-> father;} return y ;} rset_ I <t> * predecessor (rset_ I <t> * X) {rset_ I <t> * Y; If (X-> left! = & M_nil) {return maximum (X-> left);} y = x-> father; while (y! = & M_nil & X = Y-> left) {x = y; y = Y-> father;} return y ;} rset_ I <t> * minimum (rset_ I <t> * X) {While (X-> left! = & M_nil) {x = x-> left;} return X;} rset_ I <t> * maximum (rset_ I <t> * X) {While (X-> right! = & M_nil) {x = x-> right;} return X;} rset_ I <t> * search (rset_ I <t> * X, const T &) {While (X! = & M_nil &! = X-> key) {if (a <X-> key) {x = x-> left;} else {x = x-> right ;}} return X ;} T * Find (const T & A) {rset_ I <t> * x = search (m_root, a); If (x = & m_nil) {return NULL ;} return r_to <t *, rset_ I <t> *> X;} t * Next (void * P) {rset_ I <t> * x = successor (r_to <rset_ I <t> *, void *> (p); If (x = & m_nil) {return NULL ;} return r_to <t *, rset_ I <t> *> X;} t * Prev (void * P) {rset_ I <t> * x = predecessor (r_to <rset_ I <t> *, void *> (p); If (x = & m_nil) {return NULL ;} return r_to <t *, rset_ I <t> *> X;} t * begin () {rset_ I <t> * x = Minimum (m_root ); if (x = & m_nil) {return NULL;} return r_to <t *, rset_ I <t> *> X;} t * end () {return NULL ;} T * index (int n) {T * P = begin (); For (; n --) {If (P = NULL) {return NULL ;} P = next (p) ;}return P ;}};