码迷,mamicode.com
首页 > 其他好文 > 详细

红黑树

时间:2016-07-24 07:07:05      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:红黑树

    红黑树是一棵二叉搜索树,它在每个节点上增加了一个存储位来表示节点的颜色,可以是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;
}

运行结果:

技术分享

本文出自 “zwy” 博客,请务必保留此出处http://10548195.blog.51cto.com/10538195/1829211

红黑树

标签:红黑树

原文地址:http://10548195.blog.51cto.com/10538195/1829211

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