红黑树(模拟实现)

一 . 概念

红黑树,是一种二叉搜索树,但在每个结点上增加一个存储位表示结点的颜色,可以是Red或
Black。 通过对任何一条从根到叶子的路径上各个结点着色方式的限制,红黑树确保没有一条路
径会比其他路径长出俩倍
,因而是接近平衡的。

近似平衡:最长路径不超过最短的2倍。

严格平衡:左右子树高度差不超过1。

二 . 性质

1. 每个结点不是红色就是黑色
2. 根节点是黑色的
3. 如果一个节点是红色的,则它的两个孩子结点是黑色的
4. 对于每个结点,从该结点到其所有后代叶结点的简单路径上,均 包含相同数目的黑色结点
5. 每个叶子结点都是黑色的(此处的叶子结点指的是空结点)

红黑树(模拟实现)_第1张图片
而3,4两点是保证红黑树近似平衡的原因。

理由:最长路径:黑-红-黑-红……交替                                                               

           最短路径:黑-黑-黑…… 

三 . 红黑树的模拟实现(不讲删除)

1. 大概框架

enum Color
{
	RED,
	BLACK
};

template
struct RBTreeNode
{
	RBTreeNode* _left;
	RBTreeNode* _right;
	RBTreeNode* _parent;

	pair _kv;

	Color _col;

	RBTreeNode(const pair& kv)
		:_left(nullptr)
		,_right(nullptr)
		,_parent(nullptr)
		,_kv(kv)
		,_col(RED)
	{}
};

template
class RBTree
{
	typedef RBTreeNode Node;
public:
    bool insert(const pair& kv);//插入
    Node* find(const K& key);//查找
    bool IsBalance();//判断是否近似平衡
    void Inorder();//中序遍历
private:
    Node* _root = nullptr;
};

2. 插入

先按照二叉搜索树找到合适的位置进行插入,之后再根据红黑树的性质调整。

如何调整:

红黑树(模拟实现)_第2张图片

bool insert(const pair& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}
		//找合适位置
		Node* cur = _root;
		Node* parent = nullptr;
		while (cur)
		{
			if (cur->_kv.first < kv.first)
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (cur->_kv.first > kv.first)
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
		//插入
		cur = new Node(kv);
		cur->_col = RED;
		if (parent->_kv.first < kv.first)
		{
			parent->_right = cur;
			cur->_parent = parent;
		}
		else
		{
			parent->_left = cur;
			cur->_parent = parent;
		}
		//调整红黑树
		while (parent && parent->_col == RED)
		{
			Node* grandparent = parent->_parent;
			//     g
			//  p     u
			//  c
			if(parent == grandparent->_left)
			{
				Node* uncle = grandparent->_right;
				if (uncle && uncle->_col == RED)
				{
					//变色
					parent->_col = uncle->_col = BLACK;
					grandparent->_col = RED;
					//向上继续调整
					cur = grandparent;
					parent = cur->_parent;
				}
				else//叔叔不存在或黑的
				{
					if (cur == parent->_left)
					{
						RotateR(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
					}
					else//cur == parent->_right
					{
						RotateL(parent);
						RotateR(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
			//    g
			// u     p
			//       c
			else
			{
				Node* uncle = grandparent->_left;
				if (uncle && uncle->_col == RED)
				{
					//变色
					parent->_col = uncle->_col = BLACK;
					grandparent->_col = RED;
					//向上继续调整
					cur = grandparent;
					parent = cur->_parent;
				}
				else//叔叔不存在或黑的
				{
					if (cur == parent->_right)
					{
						RotateL(grandparent);
						parent->_col = BLACK;
						grandparent->_col = RED;
					}
					else//cur == parent->_right
					{
						RotateR(parent);
						RotateL(grandparent);
						cur->_col = BLACK;
						grandparent->_col = RED;
					}
					break;
				}
			}
		}
        //情况一最后要把根节点变为黑
		_root->_col = BLACK;
		return true;
	}

 左旋,右旋是AVL树的知识点。

AVL树-插入-CSDN博客

void RotateL(Node* parent)//右右:左单旋
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;

		parent->_right = subRL;
		subR->_left = parent;

		Node* pp = parent->_parent;
		//subR->_parent = pp;

		parent->_parent = subR;

		if (subRL != nullptr) subRL->_parent = parent;

		if (_root == parent)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (pp->_left == parent)
				pp->_left = subR;
			else
				pp->_right = subR;
			subR->_parent = pp;
		}
		//parent->_bf = subR->_bf = 0;
	}

	void RotateR(Node* parent)//左左:右单旋
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

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

		Node* pp = parent->_parent;

		subL->_right = parent;
		parent->_parent = subL;


		if (_root == parent)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (pp->_left == parent)
				pp->_left = subL;
			else
				pp->_right = subL;
			subL->_parent = pp;
		}
		//parent->_bf = subL->_bf = 0;
	}

3. find, Inorder(中序遍历)

	Node* find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

    void Inorder()
	{
		_Inorder(_root);
		cout << endl;
	}
	void _Inorder(Node* root)
	{
		if (root == nullptr)
			return;
		_Inorder(root->_left);
		cout << root->_kv.first << " ";
		_Inorder(root->_right);
	}

4. IsBalance(用来检查是否是红黑树)

bool IsBalance()
	{
		if (_root == nullptr)
			return true;

		if (_root->_col == RED)
			return false;

		int ref = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
				ref++;
			cur = cur->_left;
		}
		
		int blacknum = 0;
		return check(_root, blacknum, ref);
	}
	bool check(Node* cur, int blacknum, int ref)
	{
		if (cur == nullptr)
		{
			if (blacknum != ref)
			{
				cout << "存在黑色节点数不相等的路径" << endl;
				return false;
			}
			return true;
		}

		if (cur->_col == RED && cur->_parent->_col == RED)
		{
			cout << "存在连续的红色节点" << endl;
			return false;
		}
		if (cur->_col == BLACK)
		{
			blacknum++;
		}

		return check(cur->_left, blacknum, ref) &&
			check(cur->_right, blacknum, ref);
	}

测验红黑树的代码:

#include
#include
#include
#include
#include
#include
#include

using namespace std;
#include"RBTree.h"

void test()
{
	const int n = 10000;
	srand((unsigned int)time(NULL));
	vector v;
	for (size_t i = 0; i < n; i++)
	{
		v.push_back(rand() + 1);
	}

	RBTree rb;
	int begin = clock();
	for (auto e : v)
	{
		if (e == -1)
		{
			;
		}
		cout << "insert:" << e << endl;
		rb.insert(make_pair(e, e));
		cout << rb.IsBalance() << endl;
	}
	int end = clock();
	rb.Inorder();
	cout << end - begin << endl;

}

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

你可能感兴趣的:(c++,算法,数据结构)