封装模拟实现的红黑树以实现set和map

目录

一、set和map在这里只是一个壳子,去调用红黑树里的各种接口

二、迭代器的实现

三、红黑树的各种接口


难点总结:

1、set中一个节点放一个值,而map是Key-Value结构,因此在红黑树内部比较的时候为了能够实现泛型,要用仿函数实现set直接取出节点的值比较,而对于map直接取出Key-Value中的Key进行比较(虽然pair支持比较,但是规则是先比较Key,再比较Value,与我们需求不同)

2、迭代器的实现:核心就是改变指针的指向,需要实现的运算符重载有()、* (主要是set在使用)、->(主要是map使用) 、++、!= 、==

3、关于是否可以修改,对于set来说,每个节点的值都不可以修改;对于map来说,first的值不允许修改,second可以修改。最简单的实现方法:将set的K设置为const K,将map的pair<K, V>设置为pair<const K, V>

一、set和map在这里只是一个壳子,去调用红黑树里的各种接口

这是set

namespace wjl
{
	template<class K>
	class set
	{
		//对map进行兼容
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};


	public:
		typedef typename RBTree<const K, SetKeyOfT>::Iterator iterator;
		typedef typename RBTree<const K, SetKeyOfT>::ConstIterator const_iterator;
		//typedef只能作用于类,不能作用于对象和变量
		//因为RBTree在这里还没有实例化,所以编译器在这里无法区分,
		// 需要手动加上typename帮助编译器进行区分

		iterator begin()
		{
			return _t.Begin();
		}
		
		const_iterator begin() const
		{
			return _t.Begin();
		}

		iterator end()
		{
			return _t.End();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		bool insert(const K& key)
		{
			return _t.Insert(key);
		}
	private:
		//使key不能被修改,以及注意要修改迭代器的参数
		RBTree<const K, SetKeyOfT> _t;
	};
}

这是map

namespace wjl
{
	template<class K, class V>
	class map
	{
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<pair<const K, V>, MapKeyOfT>::Iterator iterator;
		typedef typename RBTree<pair<const K, V>, MapKeyOfT>::ConstIterator const_iterator;
		
		iterator begin()
		{
			return _t.Begin();
		}

		iterator end()
		{
			return _t.End();
		}

		const_iterator begin() const
		{
			return _t.Begin();
		}

		const_iterator end() const
		{
			return _t.End();
		}

		bool insert(const pair<K, V>& kv)
		{
			return _t.Insert(kv);
		}

	private:
		//使first不能被修改,second可以被修改,以及注意修改迭代器,不然就成了两个不相同的类
		RBTree<pair<const K, V>, MapKeyOfT> _t;
	};
}

二、迭代器的实现

template<class T, class Ref, class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T, Ref, Ptr> Self;

	Node* _node;
	RBTreeIterator(Node* Node)
		: _node(Node)
	{ }

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

	Ptr operator->()//返回指向节点数据的指针
	{
		return &_node->_data;
	}

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

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

	Self operator++()
	{
		if (_node->_right)
		{
			Node* min = _node->_right;
			while (min->_left)
			{
				min = min->_left;
			}
			_node = min;
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}
};

三、红黑树的各种接口

template <class T>
class RBTreeNode
{
public:
	T _data;
	RBTreeNode<T>* _parent;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	Colours _col;
	RBTreeNode(const T& data)
		: _data(data)
		, _parent(nullptr)
		, _left(nullptr)
		, _right(nullptr)
	{

	}
};


template <class T, class KeyofT>
class RBTree
{
public:
	typedef RBTreeIterator<T, T&, T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
	typedef RBTreeNode<T> Node;

	Iterator Begin()//不存在哨兵位
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur);
	}

	Iterator End()//为了保证访问到最后一个节点,所以end是空
	{
		return Iterator(nullptr);
	}

	ConstIterator Begin() const//不存在哨兵位
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return ConstIterator(cur);
	}

	ConstIterator End() const//为了保证访问到最后一个节点,所以end是空
	{
		return ConstIterator(nullptr);
	}

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

		if (subLR != nullptr)
		{
			subLR->_parent = parent;
		}
		Node* pParent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;
		parent->_left = subLR;

		//有可能最新的parent是根节点
		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		//有可能旋转的是一颗子树
		else
		{
			if (pParent->_left == parent)
			{
				pParent->_left = subL;
			}
			else
			{
				pParent->_right = subL;
			}
			subL->_parent = pParent;
		}
	}

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

		if (subRL != nullptr)
		{
			subRL->_parent = parent;
		}
		parent->_right = subRL;
		Node* pParent = parent->_parent;
		parent->_parent = subR;
		subR->_left = parent;

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

	bool Insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}

		//取pair的first或者是Key本身
		KeyOfT kot;
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))//pair支持比较first小就小,再比second
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}
			cur = new Node(data);
			cur->_col = RED;
		if (kot(parent->_data) < kot(data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			//    g
			//  p   u
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;
					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
						if (cur == parent->_left)
						{
							RotateR(grandfather);
							parent->_col = BLACK;
							grandfather->_col = RED;
						}
						else
						{
							RotateL(parent);
							RotateR(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)
							{
								RotateL(grandfather);
								parent->_col = BLACK;
								grandfather->_col = RED;
							}
							else
							{
								//      g
								//   u     p
								//      c
								RotateR(parent);
								RotateL(grandfather);
								cur->_col = BLACK;
								grandfather->_col = RED;
							}
						break;
					
					}
			}
		}
		_root->_col = BLACK;
		return true;
 }


private:
	Node* _root = nullptr;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值