红黑树基本操作

#include <iostream>
using namespace std;

enum Color {
	RED,
	BLACK
};

struct Node {
	Color color;
	int key;
	Node* left;
	Node* right;
	Node* p;
};

struct Tree {
	Node* root;
	Node* nil;
};

//左旋
void LEFT_ROTATE(Tree& T, Node* x) {
	Node* y = x->right;
	x->right = y->left;
	if (y->left != T.nil)
		y->left->p = x;
	y->p = x->p;
	if (x->p == T.nil)
		T.root = y;
	else if (x == x->p->left)
		x->p->left = y;
	else
		x->p->right = y;
	y->left = x;
	x->p = y;
}

//右旋
void RIGHT_ROTATE(Tree& T, Node* y) {
	Node* x = y->left;
	y->left = x->right;
	if (x->right != T.nil)
		x->right->p = y;
	x->p = y->p;
	if (y->p == T.nil)
		T.root = x;
	else if (y == y->p->left)
		y->p->left = x;
	else
		y->p->right = x;
	x->right = y;
	y->p = x;
}

void INORDER_TREE_WALK(Tree& T,Node* x) {
	if (x != T.nil) {
		INORDER_TREE_WALK(T,x->left);
		cout << x->key;
		cout << (x->color == RED ? "红" : "黑") << endl;
		INORDER_TREE_WALK(T,x->right);
	}
}

void RB_INSERT_FIXUP(Tree& T, Node* z) {
	while (z->p->color == RED) {
		if (z->p == z->p->p->left) {
			Node* 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;
					LEFT_ROTATE(T, z);
				}
				z->p->color = BLACK;
				z->p->p->color = RED;
				RIGHT_ROTATE(T, z->p->p);
			}
		}
		else {
			Node* 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;
					RIGHT_ROTATE(T, z);
				}
				z->p->color = BLACK;
				z->p->p->color = RED;
				LEFT_ROTATE(T, z->p->p);
			}
		}
	}
	T.root->color = BLACK;
}

void RB_INSERT(Tree& T, Node* z) {
	Node *y = T.nil;
	Node *x = T.root;
	while (x != T.nil) {
		y = x;
		if (z->key < x->key)
			x = x->left;
		else
			x = x->right;
	}
	z->p = y;
	if (y == T.nil)
		T.root = z;
	else if (z->key < y->key)
		y->left = z;
	else
		y->right = z;
	z->left = T.nil;
	z->right = T.nil;
	z->color = RED;
	RB_INSERT_FIXUP(T, z);
	cout << "插入结点" << z->key << "成功!" << endl;
}

void RB_DELETE_ALL(Tree& T,Node* x) {
	if (x == T.nil)
		return;
	if (x->left != T.nil)
		RB_DELETE_ALL(T,x->left);
	if (x->right != T.nil)
		RB_DELETE_ALL(T, x->right);
	cout << "成功删除结点" << x->key << endl;
	delete x;
}

void main(){
	Node* nil = new Node;
	nil->color = BLACK;
	nil->key = -999;
	nil->p = nil->left = nil->right = NULL;
	Tree T;
	T.nil = nil;
	T.root = T.nil;
	int input;
	do {
		cout << "请输入要插入的结点值,按-999结束:";
		cin >> input;
		if (input == -999) break;
		Node* z = new Node;
		z->key = input;
		z->color = RED;
		z->p = z->left = z->right = NULL;
		RB_INSERT(T, z);
		INORDER_TREE_WALK(T, T.root);
	} while (1);
	cout << "插入结束,遍历结果为:" << endl;
	INORDER_TREE_WALK(T, T.root);
	RB_DELETE_ALL(T, T.root);
	delete T.nil;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值