<code> rbtree插入算法分析
1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 5 struct rbnode{ 6 int key; 7 int color;//1=black,0=red; 8 struct rbnode *p,*left,*right; 9 rbnode(){} 10 rbnode(int k){key=k; 11 } 12 }; 13 rbnode *nil; 14 bool RBinsert(rbnode **root,int k); 15 void LevelOrder(rbnode *t); 16 void RBinserFix(rbnode **root,rbnode *z); 17 void Left(rbnode **root, rbnode *x); 18 void Right(rbnode **root, rbnode *x); 19 void Left(rbnode **root, rbnode *x){ 20 if(x->right!=nil){ 21 rbnode * y=nil; 22 y=x->right; 23 x->right=y->left; 24 if(y->left!=nil)y->left->p=x; 25 y->p=x->p; 26 if(x->p==nil) (*root)=y; 27 else if(x==x->p->left) x->p->left=y; 28 else x->p->right=y; 29 y->left=x; 30 x->p=y;} 31 } 32 void Right(rbnode **root, rbnode *x){ 33 if(x->left!=nil){ 34 rbnode * y=nil; 35 y=x->left; 36 x->left=y->right; 37 if(y->right!=nil)y->right->p=x; 38 y->p=x->p; 39 if(x->p==nil) (*root)=y; 40 else if(x==x->p->right) x->p->right=y; 41 else x->p->left=y; 42 y->right=x; 43 x->p=y; 44 } 45 } 46 47 void RBinserFix(rbnode **root,rbnode *z){ 48 rbnode* y=nil; 49 50 while(z!=*root&&z->p->color==0){ 51 if(z->p==z->p->p->left){ 52 y=z->p->p->right; 53 if(y->color==0){ 54 z->p->color=1; 55 y->color=1; 56 z->p->p->color=0; 57 z=z->p->p; 58 }else 59 { if(z==z->p->right){z=z->p;//LEFT 60 Left(root,z);} 61 z->p->color=1; 62 z->p->p->color=0; 63 //RightRotate(); 64 Right((root),z->p->p); 65 } 66 67 68 }else { 69 y=z->p->p->left; 70 if(y->color==0){ 71 z->p->color=1; 72 y->color=1; 73 z->p->p->color=0; 74 z=z->p->p; 75 }else 76 { if(z==z->p->left){z=z->p;//LEFT 77 Right(root,z); 78 } 79 z->p->color=1; 80 z->p->p->color=0; 81 //RightRotate(); 82 Left((root),z->p->p);} 83 84 85 } 86 87 88 } 89 (*root)->color=1; 90 91 } 92 bool RBinsert(rbnode **root,int k){ 93 rbnode* z=new rbnode(k); 94 95 //cout<<root->color; 96 rbnode* y=nil; 97 98 rbnode* x=*root; 99 while(x!=nil){ 100 y=x; 101 if(k==x->key)return 0; 102 if(k<x->key)x=x->left; 103 else x=x->right; 104 } 105 z->p=y; 106 107 if(y==nil) {(*root)=z;(*root)->p=nil;} 108 else if(k<y->key) y->left=z; 109 else y->right=z; 110 z->left=nil;z->right=nil; 111 z->color=0; 112 //LevelOrder(*root); 113 RBinserFix(root,z); 114 return 1; 115 } 116 117 void Visit(rbnode *t) { 118 if (t) { 119 cout << t->key; 120 if(t->color)cout<<"黑 "; 121 else cout<<"红 "; 122 } 123 } 124 125 void LevelOrder(rbnode *t) {// 对* t逐层遍历 126 queue<rbnode*> Q; 127 while (t!=nil) { 128 Visit(t); 129 if (t->left!=nil) 130 Q.push(t->left); 131 if (t->right!=nil) 132 Q.push(t->right); 133 if (Q.empty()) 134 break; 135 t=Q.front(); 136 Q.pop(); 137 } 138 } 139 140 141 void main(){ 142 143 //rbnode* root= 144 //root->color=1;//1=black 145 //root->p=nil; 146 //root->left=nil;root->right=nil; 147 rbnode** root=(rbnode**)malloc(sizeof(rbnode*));; 148 nil=new rbnode(); 149 nil->color=1; 150 *root =nil; 151 //rbnode 152 cout<<"输入操作类型:1插入元素 2输出结构 3删除节点 4退出"<<endl; 153 int sinin,num; 154 cin>>sinin; 155 while(sinin!=4) 156 {switch(sinin){ 157 case 1: 158 cout<<"插入: "; 159 cin>>num; 160 if(!RBinsert(root,num))cout<<"插入重复值"<<endl; 161 // cout<<" root --key:"<<(*root)->p->color<<endl; 162 163 break; 164 case 2: 165 LevelOrder(*root); 166 cout<<endl; 167 break; 168 case 3: 169 break; 170 }cout<<"输入操作类型:1插入元素 2输出结构 3删除节点 4退出"<<endl; 171 cin>>sinin; 172 } 173 174 }