红黑树(模拟实现)

一 . 概念

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

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

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

二 . 性质

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


而3,4两点是保证红黑树近似平衡的原因。

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

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

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

1. 大概框架

enum Color
{
	RED,
	BLACK
};

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

	pair<K, V> _kv;

	Color _col;

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

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

2. 插入

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

如何调整:

bool insert(const pair<K, V>& 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<iostream>
#include<utility>
#include<vector>
#include<assert.h>
#include<string>
#include<utility>
#include<stdlib.h>

using namespace std;
#include"RBTree.h"

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

	RBTree<int, int> 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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值