The complete implementation of C + + red and black tree

Source: Internet
Author: User

#include <iostream>usingnamespacestd;typedef enum color{RED, Black,}color;template<typename type>struct rbnode{ColorColor;    Type data; Rbnode<type>*parent; Rbnode<type>*left; Rbnode<type>*right; Rbnode (Type D=type ()): Left (null), Right (null),Parent(NULL), data (d),Color(RED)        {}};template<typename type>class rbtree{public:rbtree () {Root = NULL;        Nul = NULL;              Start = Buynode (); Start->Color= BLACK; } void Insert (Type ar[],intN) { for(intI=0; i<n;i++) {_insert (root,ar[i]); }} Private:bool _insert (rbnode<type>*p,intVal) {rbnode<type>*PR= NULL; while(P!=null) {if(P->data = = val)returnFalse PR = p;if(P->data > Val) p=p->left;Elsep=p->right; }if(Pr==null)                {Root = Buynode (val);            p = Root; }Elsep = Buynode (val);if(Pr!=null) {if(pr->data>val) pr->left = p;ElsePr->right = p; P->Parent= PR;        } init_set (Root,p); Root->Color= BLACK; } rbnode<type>* Buynode (Type d=type ()) {rbnode<type>*s= new Rbnode<type> (d);returnS } bool Init_set (rbnode<type>*&T,rbnode<type>*&P) {p->Color= RED;if(p->Parent!=nul && p->Parent-Color= = RED) {if(p->Parent-Parent->left==p->Parent)                {if(p->Parent->left==p) {rbnode<type>*s= p->Parent-Parent->right;if(S!=nul && s->Color==red) {p->Parent-Color= BLACK; S->Color= BLACK; P=s->Parent;                            Init_set (ROOT,P); }Else{p->Parent-Color= BLACK; P->Parent-Parent-Color=red; p = p->Parent-Parent;                            Stater (ROOT,P); }                        }Else{rbnode<type>*s= p->Parent-Parent->right;if(S!=nul && s->Color==red) {p->Parent-Color= BLACK; S->Color= BLACK; P=s->Parent;                            Init_set (ROOT,P); }Else{p = p->Parent;                                Statel (ROOT,P);                            Init_set (Root,p->left); }                                           }                }Else{if(p->Parent-&GT;RIGHT==P)//\ s{rbnode<type>*s= p->Parent-Parent->left;if(S!=nul && s->Color= = RED) {p->Parent-Color= BLACK; S->Color= BLACK; p = s->Parent;                        Init_set (ROOT,P); }Else{p->Parent-Color= BLACK; P->Parent-Parent-Color=red; P=p->Parent-Parent;                        Statel (ROOT,P); }                    }Else{rbnode<type>*s= p->Parent-Parent->left;if(S!=nul && s->Color==red) {p->Parent-Color= BLACK; S->Color= BLACK; P=s->Parent;                            Init_set (ROOT,P); }Else{p = p->Parent;                                Stater (ROOT,P);                            Init_set (Root,p->right); }}}}} void Statel (rbnode<type>*&T,rbnode<type>*&P) {intFlogs =0; Rbnode<type>*q= p->right; Rbnode<type>*save= p->Parent;if(p==t)            {flogs++; } p->right = q->left;if(Q->left) q->left->Parent= P;        Q->left = p; P->Parent= q;if(save) {if(save->left==p) save->left=q;Elsesave->right=q; Q->Parent=save; } p = q;if(flogs==1) {Root = P; Root->Parent=start;} } void Stater (rbnode<type>*&T,rbnode<type>*&P) {intFlogs =0; Rbnode<type>*q= p->left;if(t==p) flogs++; Rbnode<type>*save= p->Parent; P->left = q->right;if(q->right!=null) q->right->Parent= P;        Q->right = p; P->Parent= q;if(Save!=null)if(save->left==p)                {save->left = q; }Else{save->right=q; } q->Parent= Save; p = q;if(flogs==1) {Root = P; Root->Parent=start;}    } public:void printf () {printf (Root);    } void Remove (Type val) {remove (root,val); } private:void Remove (rbnode<type>*t, Type val) {rbnode<type>*p= t; Rbnode<type>*PR= NULL; while(P!=null) {if(P->data = = val) Break;if(P->data>val) p=p->left;Elsep=p->right; }if(P==null)return;Else{//t = P;            if(P->left!=null && P->right!=null) {PR = p->right; while(pr->left!=null) pr=pr->left;                    T->data = pr->data;                p = PR; } PR = p->Parent;if(t->left==p) {rbnode<type>*s= p->right; T->left = s;if(S!=null) {s->Parent= NULL; S->Parent= t; }if(p->Color==black) {if(S!=nul && s->Color==red) {s->Color=black; }Else if(S!=nul && s->Color==black) {remove_set (root,s); }                        }Else{rbnode<type>*s= p->right; T->left = s;if(S!=null) {s->Parent= NULL; S->Parent= t; }if(p->Color==black) {if(S!=nul && s->Color==red) {s->Color= BLACK; }Else if(S!=nul && s->Color==black) {remove_set (root,s); }}}} root->Color= BLACK;DeleteP;p=null; } void Remove_set (rbnode<type>*&T,rbnode<type>*p) {rbnode<type>*s= p->Parent->right; while(P!=start && p->Color!=red) {if(S!=null) {if(s->Color==red) {s->Parent-Color= RED; S->Color= BLACK; S=s->Parent;                Statel (root,s); }Else if(s->Color==black) {if(S->left!=null && S->right!=null) {if(s->left->Color==black && s->left->right->Color==black) {s->Color= RED; S->Parent-Color= BLACK; p = s->Parent; }Else if(s->right->Color==black && s->left->Color==red) {stater (root,s); }Else if(s->right->Color==red && s->Color==black) {s=s->Parent;                            Statel (root,s);                        p = s; }}}}}} void Printf (rbnode<type>*&T) {if(T==null)return;Else{Printf (t->left); cout<<t->data<<":"<<t->Color<<"\ T";        Printf (T->right); }} rbnode<type>*start; Rbnode<type>*root; Rbnode<type>*nul;};intMain () {inta[]={0,2,3,1,5,Ten, the,7,8}; rbtree<int> RB; Rb. Insert (A,9); Rb. Remove (5); Rb. Printf ();return 0;}

The complete implementation of C + + red and black tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.