2.19 红黑树

1️⃣9️⃣ 红黑树

😭 不是个正确的代码,目前还没有能力把它改到正确,因为自己没能完全理解,以后学得更多了之后,再来改进吧。

#include <iostream>
using namespace std;

class RedBlackTree;
class RedBlackNode;

class RedBlackNode{
	friend class RedBlackTree;
public:
	int data;
	RedBlackNode* left;
	RedBlackNode* right;
	int color;
public:
	RedBlackNode(const int d=0, RedBlackNode* l=NULL, RedBlackNode* r=NULL, int c = 1):
		data(d), left(l), right(r), color(c) {}

};

class RedBlackTree{
public:
	RedBlackNode* head;
	RedBlackNode* nullNode;
	RedBlackNode* current; //现节点
	RedBlackNode* parent;  //父节点
	RedBlackNode* grand;   //祖父节点
	RedBlackNode* great;   //曾祖父节点
	void rotateWithLeftChild(RedBlackNode* k2);//向右转
	void rotateWithRightChild(RedBlackNode* k1);//向左转

	void doubleRorateWithLeftChild(RedBlackNode* k3);//双旋转之向右转
	void doubleRorateWithRightChild(RedBlackNode* k4);//双旋转之向左转

	void handleReorient(const int x);
	RedBlackNode* Rotate(const int x, RedBlackNode* parent);
	void reclaimMemory(RedBlackNode* t)const;
public:
	enum{RED, BLACK};
	RedBlackTree(const int neginf);
	~RedBlackTree();
	bool IsEmpty()const;
	void MakeEmpty();
	bool Insert(const int x);
	RedBlackNode* findMin()const;
	RedBlackNode* findMax()const;
	RedBlackNode* Find(const int x)const;
};

RedBlackTree::RedBlackTree(const int neginf){
	nullNode = new RedBlackNode();
	nullNode->left = nullNode->right = nullNode;
	head = new RedBlackNode(neginf);
	head->left = head->right = nullNode;
}

RedBlackTree::~RedBlackTree(){
	MakeEmpty();
	delete nullNode;
	delete head;
}

bool RedBlackTree::Insert(const int x){
	current = parent = grand = head;
	nullNode->data = x;

	while(current->data != x){
		great = grand;
		grand = parent;
		parent = current;
		current = (x < current->data) ? current->left : current->right;

		if(current->left->color == RED && current->right->color == RED)
			handleReorient(x);
	}
	if(current != nullNode)
		return false;

	current = new RedBlackNode(x, nullNode, nullNode);
	if(x < parent->data)
		parent->left = current;
	else
		parent->right = current;

	// 自动平衡->红黑树
	handleReorient(x);

	return true;
}

void RedBlackTree::rotateWithLeftChild(RedBlackNode* k2){
	RedBlackNode* k1 = k2->left;
	k2->left = k1->right;
	k1->right = k2;
	k2 = k1;//现在k1赋值给根
}

void RedBlackTree::rotateWithRightChild(RedBlackNode* k1){
	RedBlackNode* k2 = k1->right;
	k1->right = k2->left;
	k2->left = k1;
	k1 = k2;//现在k2赋值给根
}

void RedBlackTree::doubleRorateWithLeftChild(RedBlackNode* k3){
	rotateWithRightChild(k3->left);
	rotateWithLeftChild(k3);
}

void RedBlackTree::doubleRorateWithRightChild(RedBlackNode* k4){
	rotateWithLeftChild(k4->right);
	rotateWithRightChild(k4);
}

void RedBlackTree::handleReorient(const int x){
	//变色
	current->color = RED;
	current->left->color = BLACK;
	current->right->color = BLACK;

	if(parent->color == RED){
		grand->color = RED;
		if(x < grand->data != x < parent->data) //不同时小于,是内部孙子
			parent = Rotate(x, grand);
		current = Rotate(x, great);
		current->color = BLACK;
	}
	head->right->color = BLACK;
}

RedBlackNode* RedBlackTree::Rotate(const int x, RedBlackNode* theParent){
	if(x < theParent->data){
		(x < theParent->left->data) ?
			rotateWithLeftChild(theParent->left) :
			rotateWithRightChild(theParent->left);
		return theParent->left;
	}
	else{
		(x < theParent->right->data) ?
			rotateWithLeftChild(theParent->right) :
			rotateWithRightChild(theParent->right);
		return theParent->right;
	}
}

bool RedBlackTree::IsEmpty()const{
	return head->right == nullNode;
}

void RedBlackTree::MakeEmpty(){
	reclaimMemory(head->right);
	head->right = nullNode;
}

void RedBlackTree::reclaimMemory(RedBlackNode* t)const{
	if(t != t->left){
		reclaimMemory(t->left);
		reclaimMemory(t->right);
		delete t;
	}
}

RedBlackNode* RedBlackTree::findMin()const{
	if(IsEmpty())
		return nullNode;
	RedBlackNode* itr = head->right;
	while(itr->left != nullNode)
		itr = itr->left;
	return itr;
}

RedBlackNode* RedBlackTree::findMax()const{
	if(IsEmpty())
		return nullNode;
	RedBlackNode* itr = head->right;
	while(itr->right != nullNode)
		itr = itr->right;
	return itr;
}

RedBlackNode* RedBlackTree::Find(const int x)const{
	nullNode->data = x;
	RedBlackNode* curr = head->right;
	while(1){
		if(x < curr->data)
			curr = curr->left;
		else if(x > curr->data)
			curr = curr->right;
		else if(curr != nullNode)
			return curr;
		else return NULL;
	}
}

int main(){
	const int NEG_Inf = -999999;
	RedBlackTree t(NEG_Inf);
	t.Insert(50);
	t.Insert(40);
	t.Insert(30);
	cout << t.head->right->data << endl;
	if(!t.IsEmpty())	cout << "红黑树不是空的" << endl;
	t.MakeEmpty();
	if(t.IsEmpty())	cout << "红黑树是空的" << endl;

	t.Insert(200);
	t.Insert(100);
	t.Insert(90);
	t.Insert(50);
	t.Insert(80);
	t.Insert(60);
	t.Insert(70);

	cout << "最小的数:" << t.findMin()->data << endl;
	cout << "最大的数:" << t.findMax()->data << endl;
	cout << "查找:" ; (t.Find(100)) ? cout << "找到了" << endl : cout << "没找到!" << endl;
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值