[C++] 红黑树(RBTree)的模拟实现

 代码实现

#pragma once

enum Colour{
	RED,
	BLACK,
};

template<class T>
struct RBtreeNode{
	RBtreeNode<T>* _left;
	RBtreeNode<T>* _right;
	RBtreeNode<T>* _parent;

	T _val;
	Colour _col;

	RBtreeNode(const T& val)
		:_left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
		, _val(val)
		, _col(RED)
	{}
};

template<class T, class Ptr, class Ref>
struct __RBTreeIterator{
	typedef RBtreeNode<T> Node;
	typedef __RBTreeIterator<T, Ptr, Ref> Self;

	Node* _node;

	__RBTreeIterator(Node* node)
		:_node(node)
	{}

	Ref operator*(){
		return _node->_val;
	}

	//Ptr operator->()
	//{
	//	return &_node->_val;
	//}

	Ptr operator->(){
		return &(operator*());
	}

	// ++it;
	Self& operator++(){
		if (_node->_right != nullptr){
			_node = _node->_right;
			while (_node->_left != nullptr){
				_node = _node->_left;
			}
		}
		else{
			Node* parent = _node->_parent;
			while (parent != nullptr && _node == parent->_right){
				_node = parent;
				parent = _node->_parent;
			}

			_node = parent;
		}
		return *this;
	}

	Self operator++(int){
		Self tmp(*this);
		++(*this);
		return tmp;
	}

	Self& operator--();
	Self operator--(int);

	bool operator != (const Self& s) const{
		return _node != s._node;
	}

	bool operator == (const Self& s) const;

};

template<class K, class T, class KeyOfValue>
class RBTree{
	typedef RBtreeNode<T> Node;
public:
	typedef __RBTreeIterator<T, T*, T&> iterator;
	typedef __RBTreeIterator<T, const T*, const T&> const_iterator;

	RBTree() = default;
	// 拷贝构造 + operator=
	// 析构函数

	iterator begin(){
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}

		return iterator(cur);
	}

	iterator end(){
		return iterator(nullptr);
	}

	const_iterator begin() const{
		Node* cur = _root;
		while (cur && cur->_left){
			cur = cur->_left;
		}

		return const_iterator(cur);
	}

	const_iterator end() const{
		return const_iterator(nullptr);
	}

	pair<iterator, bool> Insert(const T& val){
		// 插入节点
		if (_root == nullptr){
			_root = new Node(val);
			_root->_col = BLACK;
			return make_pair(iterator(_root), true);
		}

		KeyOfValue kov;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur){
			if (kov(cur->_val) < kov(val)){
				parent = cur;
				cur = cur->_right;
			}
			else if (kov(cur->_val) > kov(val)){
				parent = cur;
				cur = cur->_left;
			}
			else{
				return make_pair(iterator(cur), false);
			}
		}

		cur = new Node(val);
		Node* newnode = cur;
		cur->_col = RED;
		if (kov(parent->_val) < kov(val)){
			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;
				// 1.
				if (uncle && uncle->_col == RED){
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else{
					if (cur == parent->_right){
						RotateL(parent);
						swap(parent, cur);
					}

					RotateR(grandfather);
					parent->_col = BLACK;
					grandfather->_col = RED;

					break;
				}
			}
			else{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED){
					parent->_col = BLACK;
					uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else{
					if (cur == parent->_left){
						RotateR(parent);
						swap(parent, cur);
					}

					RotateL(grandfather);
					parent->_col = BLACK;
					grandfather->_col = RED;
					break;
				}
			}
		}

		_root->_col = BLACK;

		return make_pair(iterator(newnode), true);
	}

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

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

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

		if (parent == _root){
			_root = subR;
			_root->_parent = nullptr;
		}
		else{
			if (pNode->_left == parent)
				pNode->_left = subR;
			else
				pNode->_right = subR;

			subR->_parent = pNode;
		}

	}

	void RotateR(Node* parent){
		Node* subL = parent->_left;
		Node* subLR = subL->_right;

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

		subL->_right = parent;

		Node* pNode = parent->_parent;
		parent->_parent = subL;

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

			subL->_parent = pNode;
		}
	}



	iterator Find(const K& k){
		Node* cur = _root;
		while (cur){
			KeyOfValue kov;
			if (kov(cur->_val) < k){
				cur = cur->_right;
			}
			else if (kov(cur->_val) > k){
				cur = cur->_left;
			}
			else{
				return iterator(cur);
			}
		}

		return end();
	}

	bool IsValidRBTree(){
		Node* pRoot = _root;
		// 空树也是红黑树
		if (nullptr == pRoot)
			return true;

		// 检测根节点是否满足情况
		if (BLACK != pRoot->_col){
			cout << "违反红黑树性质二:根节点必须为黑色" << endl;
			return false;
		}

		// 获取任意一条路径中黑色节点的个数
		size_t blackCount = 0;
		Node* pCur = pRoot;
		while (pCur){
			if (BLACK == pCur->_col)
				blackCount++;

			pCur = pCur->_left;
		}

		// 检测是否满足红黑树的性质,k用来记录路径中黑色节点的个数
		size_t k = 0;
		return _IsValidRBTree(pRoot, k, blackCount);
	}

	bool _IsValidRBTree(Node* pRoot, size_t k, const size_t blackCount){
		//走到null之后,判断k和black是否相等
		if (nullptr == pRoot){
			if (k != blackCount){
				cout << "违反性质四:每条路径中黑色节点的个数必须相同" << endl;
				return false;
			}
			return true;
		}

		// 统计黑色节点的个数
		if (BLACK == pRoot->_col)
			k++;

		// 检测当前节点与其双亲是否都为红色
		Node* pParent = pRoot->_parent;
		if (pParent && RED == pParent->_col && RED == pRoot->_col){
			cout << "违反性质三:没有连在一起的红色节点" << endl;
			return false;
		}

		return _IsValidRBTree(pRoot->_left, k, blackCount) &&
			_IsValidRBTree(pRoot->_right, k, blackCount);
	}

	void InOrder(){
		_InOrder(_root);
		cout << endl;
	}

	void _InOrder(Node* root){
		if (root == nullptr)
			return;

		_InOrder(root->_left);
		cout << root->val.first << " ";
		_InOrder(root->_right);
	}

private:
	Node* _root = nullptr;
};

测试代码


void TestRBtree(){
	RBTree<int, int> t;
	int a[] = { 16, 3, 7, 11, 9, 26, 18, 14, 15 };
	//int a[] = { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14, };

	for (auto e : a){
		t.Insert(make_pair(e, e));
	}

	t.InOrder();
	cout << t.IsValidRBTree() << endl;
}

 

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

giturtle

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值