0
点赞
收藏
分享

微信扫一扫

信息安全评估

忍禁 2023-05-09 阅读 108

目录

概述

算法

调整策略

源码

RBTree.h

test.cpp


概述

红黑树,是一种二叉搜索树,每一个节点上有一个存储位表示节点的颜色,可以是Red或Black。

通过对任何一条从根到叶子的路径上各个节点着色方式的限制,红黑树确保没有一条路径会比其他路径长上两倍,因而是接进平衡的。

红黑树性质:

  • 每个节点不是红色就是黑色
  • 根节点是黑色的
  • 红节点的两个孩子一定是黑色的;黑节点的两个孩子不一定是红色的。没有连续的红节点
  • 对于每个节点,从该节点到其后所有后代叶节点的简单路径上,均包含相同数目的黑节点
  • 每个叶子节点都是黑色的(NIL空节点)

算法

红黑树在设计的时候,插入策略与AVL树一样,只是插入之后的调整策略与AVL不同(旋转策略是一样的,但是红黑树需要考虑变色且无需再考虑平衡因子)

只看遍历的时间复杂度的话,AVL树的时间复杂度是低于红黑树的,因为AVL树的时间复杂度是无限接近于O(log_2 n),而红黑树的时间复杂度是O(log_2 n) ~ 2 * O(log_2 n),但这才系统层面的时间损失很小。

从调整策略的角度,红黑树的调整次数与旋转次数都远低于AVL树。所以综合来看,红黑树的性能是优于AVL树的,map和set的底层封装的也正是红黑树。

调整策略

红黑树的根节点一定是黑色,新插入的节点默认为是红色。

当新插入一个红色节点cur时,先观察cur的父节点parent,如果父节点是黑色,则无需调整;如果父节点也是红节点,那么再观察cur节点的叔叔节点uncle,根据uncle节点的情况进行调整。

红黑树调整策略的核心思路:不能出现连续的红色节点,每条路径的黑色节点数量一样

调整策略分为三种情况:

  • 情况1:父节点parent和叔叔节点uncle都是红色,此时只需变色调整,不需旋转,并向上调整
  • 情况2:父节点parent为红色,叔叔节点不存在或为黑色,cur节点和parent节点都同为左节点或同为右节点,此时需要左单旋或者右单旋,无需向上调整
  • 情况3:父节点parent为红色,叔叔节点不存在或为黑色,cur节点为左节点时parent节点为右节点,或者cur节点为右节点时parent节点为左节点,此时需要左右双旋或者右左双旋,无需向上调整

情况1:parent节点是红色,uncle节点也是红色

调整方法:parent节点与uncle节点变为黑色,祖父节点grandparent节点变为红色,然后将cur变为祖父节点,parent节点依然为cur节点的父节点,向上调整,直到出现parent节点为空,最后再将根节点置为黑色。如图:

情况1
情况1

情况2: 父节点parent为红色,叔叔节点不存在或为黑色,cur节点和parent节点都同为左节点或同为右节点

调整方法:以祖父节点grandparent为轴点进行左单旋或则右单旋,父节点变成黑色,祖父节点变成红色。如图:

情况2

情况1 情况2 结合调整

情况3:父节点parent为红色,叔叔节点不存在或为黑色,cur节点为左节点时parent节点为右节点,或者cur节点为右节点时parent节点为左节点

调整方法:先以parent节点为轴心进行左单旋或者右单旋,再以grandparent节点为轴心进行与上一步操作相反的单旋,最后将cur节点变成黑色,将grandparent节点变为红色

示例:将数列{ 16, 3, 7, 9, 26, 18, 14, 15, 13, 11 }按顺序插入红黑色中

图1 插入16、3、7
图2 插入9
图3 插入26、18
图4 插入14、15
图5 插入13
图6 插入11

源码

RBTree.h

#include <iostream>

enum Colour
{
	RED,
	BLACK
};

template<class K, class V>
struct RBTreeNode
{
	std::pair<K, V> _kv;
	RBTreeNode<K, V>* _left;
	RBTreeNode<K, V>* _right;
	RBTreeNode<K, V>* _parent;
	Colour _col;

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

template<class K, class V>
class RBTree
{
	typedef RBTreeNode<K, V> Node;
public:
	bool insert(const std::pair<K, V>& kv)
	{
		if (_root == nullptr)
		{
			_root = new Node(kv);
			_root->_col = BLACK;
			return true;
		}

		Node* parent = nullptr;
		Node* cur = _root;
		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* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;

				if (uncle && uncle->_col == RED)
				{
					// 情况1:叔叔存在且为红
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						// 情况2: 右单旋
						rotate_right(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						// 情况3: 左右双旋
						rotate_left(parent);
						rotate_right(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;

				if (uncle&& uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_right)
					{
						// 情况2: 左单旋
						rotate_left(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						// 情况3: 右左双旋
						rotate_right(parent);
						rotate_left(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}

		}

		_root->_col = BLACK;

		return true;
	}

	void rotate_right(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 == nullptr)
		{
			subL->_parent = nullptr;
			_root = subL;
		}
		else
		{
			if (parent == ppNode->_left)
			{
				ppNode->_left = subL;
			}
			else
			{
				ppNode->_right = subL;
			}
			subL->_parent = ppNode;
		}
	}

	void rotate_left(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 == nullptr)
		{
			subR->_parent = nullptr;
			_root = subR;
		}
		else
		{
			if (parent == ppNode->_left)
			{
				ppNode->_left = subR;
			}
			else
			{
				ppNode->_right = subR;
			}
			subR->_parent = ppNode;
		}
	}

	void in_order()
	{
		_in_order(_root);
		std::cout << std::endl;
	}

	bool is_balance()
	{
		if (_root == nullptr)
		{
			return true;
		}

		if (_root->_col != BLACK)
		{
			return false;
		}

		int ref = 0;
		Node* left = _root;
		while (left)
		{
			if (left->_col == BLACK)
			{
				++ref;
			}
			left = left->_left;
		}

		return _is_balance(_root, 0, ref);
	}

private:
	void _in_order(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}
		_in_order(root->_left);
		std::cout << root->_kv.first << "; " << root->_kv.second << std::endl;
		_in_order(root->_right);
	}

	// 检查是否有联系的红节点
	bool _is_balance(Node* root, int blackNum, const int ref)
	{
		if (root == nullptr)
		{
			if (blackNum != ref)
			{
				std::cout << "路径黑色节点跟最左路径不相等" << std::endl;
				return false;
			}
			return true;
		}

		if (root->_col == RED && root->_parent->_col == RED)
		{
			std::cout << "出现连续红节点: " << root->_kv.first << ", " << root->_parent->_kv.first << std::endl;
			return false;
		}

		if (root->_col == BLACK)
		{
			++blackNum;
		}

		return _is_balance(root->_left, blackNum, ref)
			&& _is_balance(root->_right, blackNum, ref);
	}

private:
	Node* _root = nullptr;
};

test.cpp

#include "RBTree.h"
#include <ctime>

void test1_RBTree()
{
	int arr[] = { 16, 3, 7, 9, 26, 18, 14, 15, 13, 11 };
	//int arr[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };
	RBTree<int, int> t;
	for (auto& e : arr)
	{
		t.insert(std::make_pair(e, e));
	}
	t.in_order();
	std::cout << std::endl;
	std::cout << t.is_balance() << std::endl;
}

void test2_RBTree()
{
	RBTree<int, int> t;
	for (int i = 0; i < 10000; ++i)
	{
		int x = rand();
		t.insert(std::make_pair(x, x));
	}
	//t.in_order();
	//std::cout << std::endl;
	std::cout << t.is_balance() << std::endl;
}

int main()
{
	srand(time(0));
	test1_RBTree();
	test2_RBTree();
	
	return 0;
}

举报

相关推荐

6安全评估与测试

0 条评论