码迷,mamicode.com
首页 > 编程语言 > 详细

C++构建红黑树

时间:2019-06-14 01:00:35      阅读:113      评论:0      收藏:0      [点我收藏+]

标签:using   include   void   ota   tree   pre   turn   clu   err   

#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);
}

  

C++构建红黑树

标签:using   include   void   ota   tree   pre   turn   clu   err   

原文地址:https://www.cnblogs.com/SHQHDMR/p/11020415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!