C++构建红黑树

#include "iostream"

using namespace std;

class Node
{
public:
	int data;
	bool red;
	Node *parent = NULL;
	Node *lchild = NULL;
	Node *rchild = NULL;

	Node(int data, bool red = true)
	{
		this->data = data;
		this->red = red;
	}

	void toggleColor()
	{
		this->red = !this->red;
	}
};

class RBTree
{
public:
	Node *root;

	RBTree(int data)
	{
		this->root = new Node(data, false);
	}

	Node* insert(int data)
	{
		Node *current = this->root;
		Node *previous = NULL;

		while (current != NULL)
		{
			previous = current;
			if (current->lchild != NULL && current->rchild != NULL &&
				current->red == false && current->lchild->red == true && current->rchild->red == true)
			{
				this->flip(current);
				this->solveCollision(current->parent, current);
			}
			current = data >= current->data ? current->rchild : current->lchild;
		}

		current = new Node(data);
		if (data >= previous->data)
		{
			previous->rchild = current;
		}
		else
		{
			previous->lchild = current;
		}
		current->parent = previous;

		this->solveCollision(previous, current);

		return current;
	}

protected:
	void flip(Node *root)
	{
		if (root != this->root)
		{
			root->toggleColor();
		}
		root->lchild->toggleColor();
		root->rchild->toggleColor();
	}

	void solveCollision(Node *parent, Node *current)
	{
		if (parent != NULL && current != NULL && current->red && parent->red)
		{
			Node *granpa = parent->parent;
			Node *oldGranpa = granpa->parent;

			if (granpa->lchild == parent)
			{
				if (parent->lchild == current)
				{
					this->leftOutterRotate(oldGranpa, granpa, parent, current);
				}
				else
				{
					this->leftInnerRotate(oldGranpa, granpa, parent, current);
				}
			}
			if (granpa->rchild == parent)
			{
				if (parent->rchild == current)
				{
					this->rightOutterRotate(oldGranpa, granpa, parent, current);
				}
				else
				{
					this->rightInnerRotate(oldGranpa, granpa, parent, current);
				}
			}
		}
	}

	void leftOutterRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	{
		//toggle color
		granpa->toggleColor();
		parent->toggleColor();
		//rotate
		if (oldGranpa == NULL)
		{
			this->root = parent;
		}
		else
		{
			oldGranpa->lchild = parent;
		}
		granpa->lchild = parent->rchild;
		parent->rchild = granpa;
		parent->parent = oldGranpa;
		granpa->parent = parent;
	}

	void leftInnerRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	{
		parent->rchild = current->lchild;
		current->lchild = parent;
		granpa->lchild = current;
		current->parent = granpa;
		parent->parent = current;
		this->leftOutterRotate(oldGranpa, granpa, current, parent);
	}

	void rightOutterRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	{
		//toggle color
		granpa->toggleColor();
		parent->toggleColor();
		//rotate
		if (oldGranpa == NULL)
		{
			this->root = parent;
		}
		else
		{
			oldGranpa->rchild = parent;
		}
		granpa->rchild = parent->lchild;
		parent->lchild = granpa;
		parent->parent = oldGranpa;
		granpa->parent = parent;
	}

	void rightInnerRotate(Node* oldGranpa, Node* granpa, Node* parent, Node* current)
	{
		parent->lchild = current->rchild;
		current->rchild = parent;
		granpa->rchild = current;
		current->parent = granpa;
		parent->parent = current;
		this->rightOutterRotate(oldGranpa, granpa, current, parent);
	}
};


void main()
{
	RBTree *rbt = new RBTree(50);

	rbt->insert(25);
	rbt->insert(75);
	rbt->insert(90);
	rbt->insert(80);
}

  

转载于:https://www.cnblogs.com/SHQHDMR/p/11020415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值