紅黑樹

    紅黑樹是一棵二叉搜索樹,它在每個節點上增加了一個存儲位來表示節點的顏色,可以是Red或Black。通過對任何一條從根到葉子簡單路徑上的顏色來約束,紅黑樹保證最長路徑不超過最短路徑的兩倍,因而近似於平衡。

    紅黑樹是滿足下面紅黑性質的二叉搜索樹:

    (1)每個節點,不是紅色就是黑色的。

    (2)根節點是黑色的。

    (3)如果一個節點是紅色的,則它的兩個子節點是黑色的(沒有連續的紅節點)。

    (4)對每個節點,從該節點到其所有後代葉節點的簡單路徑上,均包含相同數目的黑色節點。(每條路徑的黑色節點的數量相等

    (5)每個空節點都是黑色的。

    插入的幾種情況

    ps:cur爲當前節點,p爲父節點,g爲祖父節點,u爲叔叔節點。

    (1)第一種情況

    cur爲紅,p爲紅,g爲黑,u存在且爲紅。

    則將p,u改爲黑,g改爲紅,然後把g當成cur,繼續向上調整。

   (2)第二種情況

    cur爲紅,p爲紅,g爲黑,u不存在/u爲黑。

    p爲g的左孩子,cur爲p的左孩子則進行右單旋。

轉;相反,p爲g的右孩子,cur爲p的右孩子,則進   行左單旋轉。p、g變色--p變黑,g變紅。

    (3)第三種情況

    cur爲紅,p爲紅,g爲黑u不存在/u爲黑。

    p爲g的左孩子,cur爲p的右孩子,則針對p做左單旋轉;相反,p爲g的右孩子,cur爲p的左孩子,則針對p做右單旋轉,則轉換成了情況2。

代碼實現:

#include<iostream>
using namespace std;

enum Color
{
	RED,
	BLACK
};

template<class K,class V>
struct RBTreeNode{
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;

	K _key;
	V _value;

	Color _col;   //節點的顏色

	RBTreeNode(const K& key, const V& value)
		:_left(NULL)
		, _right(NULL)
		, _parent(NULL)
		, _key(key)
		, _value(value)
		, _col(RED)
	{}
};

template<class K,class V>
class RBTree{
	typedef RBTreeNode<K, V> Node;
public:
	RBTree()
		:_root(NULL)
	{}
	bool Insert(const K& key, const V& value)
	{
		if (_root == NULL)
		{
			_root = new Node(key,value);
			_root->_col = BLACK;
			return true;
		}
		Node* cur = _root;
		Node* parent = NULL;
		while (cur)
		{
			if (cur->_key > key)
			{
				parent = cur;
				cur = cur->_left;
			}
			else if (cur->_key < key)
			{
				parent = cur;
				cur = cur->_right;
			}
			else
			{
				return false;
			}
		}
		cur = new Node(key, value);
		if (parent->_key > key)
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_right = cur;
			cur->_parent = parent;
		}

		while (cur != _root&&parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				//第1種情況
				if (uncle&&uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//繼續向上調整
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//第3種情況轉換成第2種情況
					if (cur == parent->_right)
					{
						_RotateL(parent);
						swap(parent,cur);
					}
					//第2種情況
					_RotateR(grandfather);
					parent->_col = BLACK;
					grandfather->_col = RED;
					break;
				}
			}
			else //parent=grandfather->_right
			{
				Node* uncle = grandfather->_left;
				//第1種情況
				if (uncle&&uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					//向上繼續調整
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					//第3種情況
					if (cur == parent->_left)
					{
						_RotateR(parent);
						swap(parent,cur);
					}
					//第2種情況
					_RotateL(grandfather);
					grandfather->_col = RED;
					parent->_col = BLACK;
					break;
				}
			}
		}
		_root->_col = BLACK;
		return true;
	}
	Node* Find(const K& key)
	{
		if (_root == NULL)
			return NULL;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_key > key)
				cur = cur->_left;
			else if (cur->_key < key)
				cur = cur->_right;
			else
				return cur;
		}
		return NULL;
	}
	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}
	bool IsBlance()
	{
		if (_root == NULL)
			return true;
		if (_root->_col == RED)
			return false;
		int k = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
				++k;
			cur = cur->_left;
		}
		int count = 0;
		return _IsBlance(_root,k,count);
	}
protected:
	void _RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		subL->_right = parent;
		Node* ppNode = parent->_parent;
		parent->_parent = subL;

		if (ppNode == NULL)
		{
			_root = subL;
			subL->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subL;
				subL->_parent = ppNode;
			}
			else
			{
				ppNode->_right = subL;
				subL->_parent = ppNode;
			}
		}
	}

	void _RotateL(Node*  parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;

		subR->_left = parent;
		Node* ppNode = parent->_parent;
		parent->_parent = subR;

		if (ppNode == NULL)
		{
			_root = subR;
			subR->_parent = NULL;
		}
		else
		{
			if (ppNode->_left == parent)
			{
				ppNode->_left = subR;
				subR->_parent = ppNode;
			}
			else
			{
				ppNode->_right = subR;
				subR->_parent = ppNode;
			}
		}
	}
	void _InOrder(Node* root)
	{
		if (root == NULL)
		{
			return;
		}
		_InOrder(root->_left);
		cout << root->_key << " ";
		_InOrder(root->_right);
	}
	bool _IsBlance(Node* root, const int k, int count)
	{
		if (root == NULL)
			return true;
		//規則3:沒有連續的紅節點
		if (root->_col == RED&&root->_parent->_col == RED)
		{
			cout << "出現連續的紅色節點" << root->_key<<endl;
			return false;
		}
		if (root->_col == BLACK)
			++count;
		//規則4:每條路徑的黑色節點的數量相等
		if (root->_left == NULL&&root->_right == NULL&&count != k)
		{
			cout << "黑色節點個數不相等" << root->_key<<endl;
			return false;
		}
		return _IsBlance(root->_left, k, count) && _IsBlance(root->_right, k, count);
	}
protected:
	Node* _root;
};


#include "RBTree.h"

void  Test1()
{
	int a[] ={16, 3, 7, 11, 9, 26, 18, 14, 15};
	RBTree<int, int> rbt;
	for (int i = 0; i < sizeof(a) / sizeof(a[0]); ++i)
	{
		rbt.Insert(a[i],i);
	}
	rbt.InOrder();
	cout << rbt.IsBlance() << endl;

	RBTreeNode<int, int>* ret1=rbt.Find(15);
	if (ret1)
	{
		cout << ret1->_key << ":" << ret1->_value << endl;
	}
	else
	{
		cout << "沒有找到ret1" << endl;
	}

	RBTreeNode<int, int>* ret2 = rbt.Find(8);
	if (ret2)
	{
		cout << ret2->_key << ":" << ret2->_value << endl;
	}
	else
	{
		cout << "沒有找到ret2" << endl;
	}
}

int main()
{
	Test1();
	return 0;
}

運行結果:

wKiom1eTdXigE6wCAAAWmSOPOJ0124.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章