<code> 区间树查找
1 #include<iostream> 2 #include<queue> 3 using namespace std; 4 5 struct rbnode{ 6 int key;//key值即为low点值 7 int color;//1=black,0=red; 8 int max,high;//在每次旋转后更新max 9 struct rbnode *p,*left,*right; 10 rbnode(){} 11 rbnode(int k,int hi){key=k;high=hi;max=hi; 12 } 13 }; 14 rbnode *nil; 15 int max(int a,int b) 16 {return a>b?a:b;} 17 18 int mmax(rbnode * a){ 19 int xx=-1,yy=-1,zz=-1; 20 if(a!=nil)xx=a->high;else return -1; 21 if(a->left!=nil)yy=a->left->max; 22 if(a->right!=nil)zz=a->right->max; 23 return max(max(xx,yy),zz);} 24 25 rbnode * Minintervalsearch(rbnode **root,rbnode *x,int i1,int i2); 26 bool RBinsert(rbnode **root,int k,int high); 27 void Fixmax(rbnode **root,int num,int high); 28 void LevelOrder(rbnode *t); 29 void RBinserFix(rbnode **root,rbnode *z); 30 void Left(rbnode **root, rbnode *x); 31 void Right(rbnode **root, rbnode *x); 32 void Left(rbnode **root, rbnode *x){ 33 if(x->right!=nil){ 34 rbnode * y=nil; 35 y=x->right; 36 x->right=y->left; 37 if(y->left!=nil)y->left->p=x; 38 y->p=x->p; 39 if(x->p==nil) (*root)=y; 40 else if(x==x->p->left) x->p->left=y; 41 else x->p->right=y; 42 y->left=x; 43 x->p=y;} 44 } 45 void Right(rbnode **root, rbnode *x){ 46 if(x->left!=nil){ 47 rbnode * y=nil; 48 y=x->left; 49 x->left=y->right; 50 if(y->right!=nil)y->right->p=x; 51 y->p=x->p; 52 if(x->p==nil) (*root)=y; 53 else if(x==x->p->right) x->p->right=y; 54 else x->p->left=y; 55 y->right=x; 56 x->p=y; 57 } 58 } 59 60 void RBinserFix(rbnode **root,rbnode *z){ 61 rbnode* y=nil; 62 63 while(z!=*root&&z->p->color==0){ 64 if(z->p==z->p->p->left){ 65 y=z->p->p->right; 66 if(y->color==0){ 67 z->p->color=1; 68 y->color=1; 69 z->p->p->color=0; 70 z=z->p->p; 71 }else 72 { if(z==z->p->right){z=z->p;//LEFT 73 Left(root,z); 74 ////////////////////////////////////////////// 75 z->max=mmax(z); 76 z->p->p->max=mmax(z->p->p); 77 } 78 z->p->color=1; 79 z->p->p->color=0; 80 //RightRotate(); 81 Right((root),z->p->p); 82 /////////////////////////////////////////////////////// 83 if(z->p!=nil){//z->p不能为nil,否则z->p->left为空 84 z->p->max=mmax(z->p); 85 z->p->right->max=mmax(z->p->right); 86 } 87 } 88 89 90 }else { 91 y=z->p->p->left; 92 if(y->color==0){ 93 z->p->color=1; 94 y->color=1; 95 z->p->p->color=0; 96 z=z->p->p; 97 }else 98 { if(z==z->p->left){z=z->p;//LEFT 99 Right(root,z); 100 101 z->max=mmax(z); 102 z->p->p->max=mmax(z->p->p); 103 } 104 z->p->color=1; 105 z->p->p->color=0; 106 //RightRotate(); 107 Left((root),z->p->p);} 108 if(z->p!=nil) 109 { z->p->max=mmax(z->p); 110 z->p->left->max=mmax(z->p->left); 111 } 112 } 113 114 115 } 116 (*root)->color=1; 117 118 } 119 //调节max方法 120 void Fixmax(rbnode **root,int k,int high){ 121 rbnode* x=*root; 122 while(x!=nil){ 123 124 if(x->max<high)x->max=high; 125 126 if(k==x->key)break; 127 if(k<x->key)x=x->left; 128 else x=x->right; 129 130 } 131 132 } 133 134 rbnode* Minintervalsearch(rbnode **root,rbnode *x,int i1,int i2){ 135 rbnode* y=nil; 136 if(x==nil)return nil; 137 if(x->left!=nil&&x->left->max>=i1) 138 {y=Minintervalsearch(root,x->left,i1,i2); 139 if(y!=nil)return y; 140 else if((x->key<=i1&&x->high>=i1)||(x->key<=i2&&x->high>=i2)||(x->key>=i1&&x->key<=i2))return x; 141 else return nil; 142 } 143 else if((x->key<=i1&&x->high>=i1)||(x->key<=i2&&x->high>=i2)||(x->key>=i1&&x->key<=i2))return x; 144 else return Minintervalsearch(root,x->right,i1,i2); 145 } 146 147 148 149 bool RBinsert(rbnode **root,int k,int high){ 150 rbnode* z=new rbnode(k,high); 151 152 //cout<<root->color; 153 rbnode* y=nil; 154 155 rbnode* x=*root; 156 while(x!=nil){ 157 y=x; 158 if(k==x->key)return 0; 159 if(k<x->key)x=x->left; 160 else x=x->right; 161 } 162 z->p=y; 163 164 if(y==nil) {(*root)=z;(*root)->p=nil;} 165 else if(k<y->key) y->left=z; 166 else y->right=z; 167 z->left=nil;z->right=nil; 168 z->color=0; 169 //LevelOrder(*root); 170 RBinserFix(root,z); 171 return 1; 172 } 173 174 void Visit(rbnode *t) { 175 if (t) { 176 cout << t->key<<'.'<<t->high<<'.'<<t->max; 177 if(t->color)cout<<"黑 "; 178 else cout<<"红 "; 179 } 180 } 181 182 void LevelOrder(rbnode *t) {// 对* t逐层遍历 183 queue<rbnode*> Q; 184 while (t!=nil) { 185 Visit(t); 186 if (t->left!=nil) 187 Q.push(t->left); 188 if (t->right!=nil) 189 Q.push(t->right); 190 if (Q.empty()) 191 break; 192 t=Q.front(); 193 Q.pop(); 194 } 195 } 196 197 198 void main(){ 199 200 //rbnode* root= 201 //root->color=1;//1=black 202 //root->p=nil; 203 //root->left=nil;root->right=nil; 204 rbnode** root=(rbnode**)malloc(sizeof(rbnode*));; 205 nil=new rbnode(); 206 nil->color=1; 207 *root =nil; 208 rbnode* y=nil; 209 //rbnode 210 cout<<"输入操作类型:1插入元素 2输出结构 3区间树查找 4退出"<<endl; 211 int sinin,num,high; 212 cin>>sinin; 213 while(sinin!=4) 214 {switch(sinin){ 215 case 1: 216 cout<<"插入: "; 217 cin>>num>>high; 218 if(num>=high){cout<<"数据范围有问题,重新输入"<<endl;break;} 219 if(!RBinsert(root,num,high))cout<<"插入重复值"<<endl; 220 else Fixmax(root,num,high); 221 // cout<<" root --key:"<<(*root)->p->color<<endl; 222 223 break; 224 case 2: 225 LevelOrder(*root); 226 cout<<endl; 227 break; 228 case 3: 229 cin>>num>>high; 230 y=Minintervalsearch(root,*root,num,high); 231 if(y!=nil) 232 cout<<y->key<<" "<<y->high<<endl; 233 else cout<<"结果为空"<<endl; 234 break; 235 }cout<<"输入操作类型:1插入元素 2输出结构 3区间树查找 4退出"<<endl; 236 cin>>sinin; 237 } 238 239 }