红黑树(Red Black Tree)

class RBTree{
	struct node{
		int key, value;
		
		node *lson, *rson;
		node *p;
		bool color;
	};

	node *root;
	node *null;

	void Left_Rotate(node *x) {
		node *t = x->rson;
		x->rson = t->lson;
		if (t->lson != null)	t->lson->p = x;
		t->p = x->p;
		if (x->p == null) 
			root = t;
		else if (x == x->p->lson)
			x->p->lson = t;
		else
			x->p->rson = t;
		x->p = t;
		t->lson = x;
	}
	
	void Right_Rotate(node *x) {
		node *t = x->lson;
		x->lson = t->rson;
		if(t->rson != null)		t->rson->p = x;
		t->p = x->p;
		if (x->p == null)
			root = t;
		else if (x == x->p->lson)
			x->p->lson = t;
		else
			x->p->rson = t;
		t->rson = x;
		x->p = t;
	}
	
	void RB_Balance(node *z) {
		while (z->p->color) {
			if(z->p == z->p->p->lson) {
				if (z->p->p->rson->color) {
					z->p->p->color = 1;
					z->p->color = z->p->p->rson->color = 0;
					z = z->p->p;
				}
				else {
					if (z == z->p->rson) {
						z = z->p;
						Left_Rotate(z);
					}
					z->p->color = 0;
					z->p->p->color = 1;
					Right_Rotate(z->p->p);
				}
			}
			else {
				if (z->p->p->lson->color) {
					z->p->p->color = 1;
					z->p->color = z->p->p->lson->color = 0;
					z = z->p->p;
				}
				else{
					if (z == z->p->lson) {
						z = z->p;
						Right_Rotate(z);
					}
					z->p->color = 0;
					z->p->p->color = 1;
					Left_Rotate(z->p->p);
				}
			}
		}
		root->color = 0;
	}

	void RB_insert(node *z) {
		node *x = root, *y = null;
		while (x != null) {
			y = x;
			if (z->key < x->key)
				x = x->lson;
			else
				x = x->rson;
		}
		z->p = y;
		if (z->p == null)
			root = z;
		else if (z->key < y->key)
			y->lson = z;
		else
			y->rson = z;
		RB_Balance(z);
	}

	node* RB_find(int key) {
		node *t = root;
		while (t != null && t->key != key)
			t = key < t->key ? t->lson : t->rson;
		return t;
	}

	node* RB_Minson(node *x) {
		while (x->lson != null)
			x = x->lson;
		return x;
	}

	void RB_Transplant(node *u, node *v) {
		if (u->p == null)
			root = v;
		else if (u == u->p->lson)
			u->p->lson = v;
		else
			u->p->rson = v;
		v->p = u->p;
	}
	
	void RB_Fixup(node *x) {
		while (x!=root && x->color==false) {
			if (x == x->p->lson) {
				node *w = x->p->rson;
				if (w->color) {
					x->p->color = 1;
					w->color = 0;
					Left_Rotate(x->p);
					w = x->p->rson;
				}
				if (w->lson->color == false && w->rson->color == false) {
					w->color = 1;
					x = x->p;
				}
				else {
					if (w->rson->color == 0) {
						w->lson->color = 0;
						w->color = 1;
						Right_Rotate(w);
						w = x->p->rson;
					}
					w->color = x->p->color;
					x->p->color = 0;
					w->rson->color = 0;
					Left_Rotate(x->p);
					x = root;
				}
			}
			else {
				node *w = x->p->lson;
				if (w->color) {
					w->color = 0;
					x->p->color = 1;
					Right_Rotate(x->p);
					w = x->p->lson;
				}
				if (w->lson->color == false && w->rson->color == false) {
					w->color = 1;
					x = x->p;
				}
				else {
					if (w->lson->color == 0) {
						w->rson->color = 0;
						w->color = 1;
						Left_Rotate(w);
						w = x->p->lson;
					}
					w->color = x->p->color;
					x->p->color = 0;
					w->lson->color = 0;
					Right_Rotate(x->p);
					x = root;
				}
			}
		}
		x->color = 0;
	}

	void RB_Delete(node *z) {
		node *y = z, *x = null;
		bool y_color = z->color;
		if (z->lson == null) {
			x = z->rson;
			RB_Transplant(z, z->rson);
		}
		else if (z->rson == null) {
			x = z->lson;
			RB_Transplant(z, z->lson);
		}
		else {
			y = RB_Minson(z->rson);
			y_color = y->color;
			x = y->rson;
			if (y->p == z)
				x->p = y;
			else {
				RB_Transplant(y, x);
				y->rson = z->rson;
				y->rson->p = y;
			}
			RB_Transplant(z, y);
			y->lson = z->lson;
			y->lson->p = y;
			y->color = z->color;
		}
		delete z;
		if (y_color == false)
			RB_Fixup(x);
	}

public:
	RBTree(){
		null = new node;
		null->key = -1;
		null->lson = null->rson = null->p = null;
		null->color = 0;
		root = null;
	}

	void insert(int key, int value) {
		node *t=new node;
		t->key = key;
		t->value = value;
		t->lson = t->rson = t->p = null;
		t->color = 1;
		RB_insert(t);
	}
	
	bool find(int key) {
		return RB_find(key) != null;
	}

	bool erase(int key) {
		node *t = RB_find(key);
		if (t != null) {
			RB_Delete(t);
			return true;		
		}
		return false;
	}	
};

RBTree A;
A.insert(key, value);
A.find(key);
A.erase(key);

  

posted on 2017-06-26 16:37  gjing  阅读(111)  评论(0)    收藏  举报

导航