#include <stdio.h>
#include <stdlib.h>
#define RED 1
#define BLACK 0
typedef struct
{
int low;
int high;
}KeyType,*KT;
typedef struct RBTreeNode
{
KT key;
int color;
int max;
RBTreeNode *p;
RBTreeNode *left;
RBTreeNode *right;
}RBT,*pRBT;
pRBT nil=(pRBT)malloc(sizeof(RBT));
int bh=0;
void initNil()
{
nil->key=(KT)malloc(sizeof(KeyType));
nil->key->low=-1;
nil->key->high=-1;
nil->color=BLACK;
nil->p=NULL;
nil->left=NULL;
nil->right=NULL;
nil->max=-1;
}
int max(int a,int b,int c)
{
if(a>=b && a>=c)
return a;
else if(b>=c)
return b;
else
return c;
}
void leftRotate(pRBT *root,pRBT x)
{
//左旋要有右子树
if(x->right==nil)
return;
pRBT y=x->right;
x->right=y->left;
if(y->left != nil)
y->left->p=x;
y->p=x->p;
if(x->p==nil)
{
(*root)=y;
}
else if(x == x->p->left)
{
x->p->left=y;
}
else
{
x->p->right=y;
}
y->left=x;
x->p=y;
x->max=max(x->key->high,x->left->max,x->right->max);
y->max=max(y->key->high,y->left->max,y->right->max);
}
void rightRotate(pRBT *root,pRBT y)
{
//右旋要有左子树
if(y->left==nil)
return;
pRBT x=y->left;
y->left=x->right;
x->p=y->p;
if(x->right != nil)
x->right->p=y;
if(y->p==nil)
{
(*root)=x;
}
else if(y==y->p->left)
{
y->p->left=x;
}
else
{
y->p->right=x;
}
x->right=y;
y->p=x;
y->max=max(y->key->high,y->left->max,y->right->max);
x->max=max(x->key->high,x->left->max,x->right->max);
}
void rbInsertFixup(pRBT *root,pRBT z)
{
while(z->p->color==RED)
{
if(z->p==z->p->p->left)
{
pRBT y=z->p->p->right;
if(y->color==RED)
{
z->p->color=BLACK;
y->color=BLACK;
z->p->p->color=RED;
z=z->p->p;
}
else
{
if(z==z->p->right)
{
z=z->p;
leftRotate(root,z);
}
z->p->color=BLACK;
z->p->p->color=RED;
rightRotate(root,z->p->p);
}
}
else
{
pRBT y=z->p->p->left;
if(y->color==RED)
{
z->p->color=BLACK;
y->color=BLACK;
z->p->p->color=RED;
z=z->p->p;
}
else
{
if(z==z->p->left)
{
z=z->p;
rightRotate(root,z);
}
z->p->color=BLACK;
z->p->p->color=RED;
leftRotate(root,z->p->p);
}
}
}
if((*root)==nil || (*root)->color==RED)
bh++;
(*root)->color=BLACK;
}
void rbInsert(pRBT *root,KT key)
{
pRBT z=(pRBT)malloc(sizeof(RBT));
z->key=key;
pRBT x=(*root);
pRBT y=nil;
while(x != nil)
{
y=x;
if(z->key->low<x->key->low)
{
x=x->left;
}
else
{
x=x->right;
}
}
z->p=y;
if(y==nil)
{
(*root)=z;
}
else if(z->key->low<y->key->low)
{
y->left=z;
}
else
{
y->right=z;
}
z->left=nil;
z->right=nil;
z->color=RED;
pRBT u=z;
while(u!=nil)
{
u->max=max(u->key->high,u->left->max,u->right->max);
u=u->p;
}
rbInsertFixup(root,z);
}
void rbTransplant(pRBT *root,pRBT u,pRBT v)
{
if(u->p==nil)
(*root)=v;
else if(u==u->p->left)
u->p->left=v;
else
u->p->right=v;
v->p=u->p;
}
pRBT treeMinimum(pRBT root)
{
if(root==nil)
return root;
pRBT x=root;
while(x->left!=nil)
{
x=x->left;
}
return x;
}
void rbDeleteFixup(pRBT *root,pRBT x)
{
while(x != *root && x->color==BLACK)
{
if(x==x->p->left)
{
pRBT w=x->p->right;
if(w->color==RED)
{
w->color=BLACK;
x->p->color=RED;
leftRotate(root,x->p);
w=x->p->right;
}
if(w->left->color==BLACK && w->right->color==BLACK)
{
w->color=RED;
x=x->p;
if(x==*root)
bh--;
}
else
{
if(w->right->color == BLACK)
{
w->left->color=BLACK;
w->color=RED;
rightRotate(root,w);
w=x->p->right;
}
w->color=x->p->color;
x->p->color=BLACK;
w->right->color=BLACK;
leftRotate(root,x->p);
x=(*root);
}
}
else
{
pRBT w=x->p->left;
if(w->color==RED)
{
w->color=BLACK;
x->p->color=RED;
rightRotate(root,x->p);
w=x->p->left;
}
if(w->left->color==BLACK && w->right->color==BLACK)
{
w->color=RED;
x=x->p;
}
else
{
if(w->left->color == BLACK)
{
w->right->color=BLACK;
w->color=RED;
leftRotate(root,w);
w=x->p->left;
}
w->color=x->p->color;
x->p->color=BLACK;
w->left->color=BLACK;
rightRotate(root,x->p);
x=(*root);
}
}
}
x->color=BLACK;
}
void rbDelete(pRBT *root,pRBT z)
{
pRBT y=z,x;
int yOrigColor=y->color;
if(z->left==nil)
{
x=z->right;
rbTransplant(root,z,x);
}
else if(z->right==nil)
{
x=z->left;
rbTransplant(root,z,x);
}
else
{
y=treeMinimum(z->right);
yOrigColor=y->color;
x=y->right;
if(y->p==z)
{
x->p=y;
}
else
{
rbTransplant(root,y,x);
y->right=z->right;
y->right->p=y;
}
rbTransplant(root,z,y);
y->left=z->left;
y->left->p=y;
y->color=z->color;
free(z->key);
free(z);
pRBT u=x;
while(u!=nil)
{
u->max=max(u->key->high,u->left->max,u->right->max);
u=u->p;
}
if(yOrigColor==BLACK)
rbDeleteFixup(root,x);
}
}
void preTrav(pRBT root,char c)
{
if(root==nil)
return;
else
{
printf("(%d,%d) ",root->key->low,root->key->high);
printf("%d ",root->max);
if(root->color==BLACK)
printf("%s ","黑");
else
printf("%s ","红");
printf("%c ",c);
printf("\n");
preTrav(root->left,'L');
preTrav(root->right,'R');
}
}
bool isOverlap(KT x,KT y)
{
return x->low<=y->high && y->low<=x->high;
}
pRBT intervalSearch(pRBT root,KT i)
{
pRBT x=root;
while(x!=nil && !isOverlap(i,x->key))
{
if(x->left && x->left->max>=i->low)
x=x->left;
else
x=x->right;
}
return x;
}
KT newKT(int low,int high)
{
KT x=(KT)malloc(sizeof(KeyType));
x->low=low;
x->high=high;
return x;
}
void main()
{
initNil();
pRBT root=nil;
rbInsert(&root,newKT(0,3));
rbInsert(&root,newKT(5,8));
rbInsert(&root,newKT(6,10));
rbInsert(&root,newKT(8,9));
rbInsert(&root,newKT(15,23));
rbInsert(&root,newKT(16,21));
rbInsert(&root,newKT(25,30));
rbInsert(&root,newKT(17,19));
rbInsert(&root,newKT(26,26));
rbInsert(&root,newKT(19,20));
preTrav(root,'M');
printf("\n");
KT y=newKT(22,25);
pRBT z=intervalSearch(root,y);
printf("%d,%d",z->key->low,z->key->high);
getchar();
}
算法导论 区间树
最新推荐文章于 2022-09-14 21:56:24 发布