C++之<set和map模拟实现>

🌈前言

本篇文章进行STL中map和set的补充!!!


🌅set和map模拟实现

注意:模拟实现,需要掌握【map和set的原理】和【RBTree的实现


🌷1、红黑树的迭代器

迭代器的好处是可以方便遍历,是数据结构的底层实现与用户透明。如果想要给红黑树增加迭代器,需要考虑以前问题:

  • begin()和end()
  1. STL明确规定,begin()与end()代表的是一段前闭后开的区间,而对红黑树进行中序遍历后,可以得到一个有序的序列,因此:begin()为红黑树中最小节点(即最左侧节点)的位置

  2. end()在最大节点(最右侧节点)的下一个位置,只需要让end()为空就行了,简化操作

  3. 注意:库中实现begin和end是使用带头节点来进行控制左闭右开的,头节点的左节点指向begin(),右节点指向end(),这样的操作更复杂

库实现的方式:
在这里插入图片描述


  • operator++ 和 operator–
  1. 因为红黑树是中序遍历的,使用operator++需要遵循(左-根-右)进行自增,两种情况:
  • 根的右子树不为空时,说明右子树没找完,继续找根右子树的最左节点

  • 根的右子树为空时,说明可能已经找完一个子树或局部子树,向上找祖先的左节点

// 注意:self为迭代器的类型重命名,pNode为指向RBTree节点的指针
self& operator++()
	{
		// 右子树不为空,需要一直往上找,因为中序是左 根 右,右完了那么左节点和中间的根节点也遍历完了,控制cur和它的父节点
		if (pNode->right == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->right)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else // 右子树为空,则继续找这个节点的右子树的最左节点
		{
			Node* subLeft = pNode->right;

			while (subLeft->left)
			{
				subLeft = subLeft->left;
			}
			pNode = subLeft;
		}
		return *this;
	}

	self operator++(int)
	{
		self tmp(*this);
		++(*this);

		return tmp;
	}

在这里插入图片描述


  1. 因为红黑树是中序遍历的,使用operator–也需要遵循(左-根-右)进行自减,两种情况:
  • 根的左子树不为空时,说明左子树没找完,继续找根左子树的最右节点

  • 根的左子树为空时,说明可能已经找完一个子树或局部子树,向上找祖先的右节点

注意:operator–和operator++的操作是相反的,可以认为是右 根 左这样走的,右完了到根最后到左

// 注意:self为迭代器的类型重命名,pNode为指向RBTree节点的指针
self& operator--()
	{
		// ++反过来走就是--
		if (pNode->left == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->left)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else
		{
			Node* subRight = pNode->left;

			while (subRight->right)
			{
				subRight = subRight->right;
			}
			pNode = subRight;
		}
		return *this;
	}

	self operator--(int)
	{
		self tmp(*this);
		--(*this);

		return tmp;
	}

在这里插入图片描述


完整迭代器代码:

// RBTreeNode<...>是红黑树的节点类

template <typename T, typename Sef, typename Ptr>
struct RBTree_Iterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTree_Iterator<T, Sef, Ptr> self;

	RBTree_Iterator(Node* _pNode)
		: pNode(_pNode)
	{}

	RBTree_Iterator(const self& s)
		: pNode(s.pNode)
	{}

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

	self& operator++()
	{
		// 右子树不为空,需要一直往上找,因为中序是左 根 右,右完了那么左节点和中间的根节点也遍历完了,控制cur和它的父节点
		if (pNode->right == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->right)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else // 右子树为空,则走这个节点的右子树的最左节点
		{
			Node* subLeft = pNode->right;

			while (subLeft->left)
			{
				subLeft = subLeft->left;
			}
			pNode = subLeft;
		}
		return *this;
	}

	self operator++(int)
	{
		self tmp(*this);
		++(*this);

		return tmp;
	}

	self& operator--()
	{
		// ++反过来走就是--
		if (pNode->left == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->left)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else
		{
			Node* subRight = pNode->left;

			while (subRight->right)
			{
				subRight = subRight->right;
			}
			pNode = subRight;
		}
		return *this;
	}

	self operator--(int)
	{
		self tmp(*this);
		--(*this);

		return tmp;
	}

	bool operator==(const self& s) { return pNode == s.pNode; }
	bool operator!=(const self& s) { return pNode != s.pNode; }
public:
	Node* pNode;
};

🌷2、改造红黑树

前言:set和map的实现是需要共用红黑树的,我们不可能set和map实现都各写一份

RBTree的实现

我们开始对RBTree进行改造:

  • 首先set的底层值是(key, key)类型的,而map是(key, value) 键值对
  1. 我们需要将红黑树节点类的模板参数改成一个,因为我们无法区分节点值的类型是key还是键值对
enum Color
{
	RED,
	BLACK
};

// T -- 泛化的类型,可能为key也可能为pair< , >
template <typename T>
struct RBTreeNode // 红黑树的节点
{
	RBTreeNode(const T& _data)
		: left(nullptr)
		, right(nullptr)
		, parent(nullptr)
		, data(_data)
		, col(RED)
	{}

	RBTreeNode<T>* left;
	RBTreeNode<T>* right;
	RBTreeNode<T>* parent;
	T data;
	Color col;
};

  1. 我们需要将红黑树的模板参数增加一个compare,用于控制值比较,并且值比较的地 方也要跟着改,如果不增加这个,编译器将无法进行值比较
// 注意KeyOfT是通过set或map里面封装一个仿函数类传过来的
template <typename K, typename T, class KeyOfT>
class RBTree
{
private:
	Node* root;
	KeyOfT kot; // 控制比较时的类型,里面重载了仿函数
public:
	bool insert(const T& data)
	{
		if (root == nullptr)
		{
			root = new Node(data);
			root->col = BLACK;
			return true;
		}

		// 按搜索树规则插入
		Node* cur = root;
		Node* parent = nullptr;

		while (cur != nullptr)
		{
			// 这里的值比较全部都要进行更改,否则无法进行比较
			if (kot(cur->data) > kot(data)) {
				parent = cur;
				cur = cur->left;
			}
			else if (kot(cur->data) < kot(data)) {
				parent = cur;
				cur = cur->right;
			}
			else {
				return false;
		}

		cur = new Node(data);
		// 如果插入节点是红色,可能破坏规则2  ---  如果插入节点是黑色,一定会破坏规则3(3很难处理)
		cur->col = RED;
		if (kot(parent->data) > kot(data)) {
			parent->left = cur;
		}
		else {
			parent->right = cur;
		}
		cur->parent = parent;


		// 存在连续红色节点时,需要一直向上调整
		while (parent != nullptr && parent->col == RED) // 因为插入节点是红色,如果parent也是红色(连续红节点),那么需要调整
		{
			Node* grandfather = parent->parent; // cur的祖父

			// 找叔叔时需要判断在祖父的哪一边
			if (grandfather->left == parent)
			{
				Node* uncle = grandfather->right;

				// 情况一:grandfather为黑,parent为红,uncle为红,cur为红 -- 需要调整g,p,u的颜色
				if (uncle && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					// 调整的这颗树可能为局部子树,需要继续向上处理,祖父的父亲可能为红节点, 更新cur,parent
					cur = grandfather;
					parent = cur->parent;
				}
				// 情况二:grandfather为黑,parent为红,uncle为nullptr/黑,cur为红 -- 需要旋转+变色处理
				else
				{
					// 右旋,插入节点为parent的左边,祖父为旋转点,parent为祖父的左边,uncle为祖父的右边
					if (cur == parent->left)
					{
						//     g
						//   p
						// c
						RotateR(grandfather);
						// 更新颜色 -- 旋转后parent为根,根要变黑(根变黑后所有子树的路径的黑节点数量都加1) -- grandfather为根的子树,变红
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 第三种情况,左右双旋,cur插入parent的右边,且parent为g的左边,,形成折线
					{
						//    g
						// p
						//    c
						RotateL(parent);
						RotateR(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->left;

				if (uncle != nullptr && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					cur = grandfather;
					parent = cur->parent;
				}
				else
				{
					// 左旋,插入节点为parent的右边,祖父为旋转点,parent为祖父的右边,uncle为祖父的左边
					// g
					//    p
					//      c
					if (cur == parent->right)
					{
						RotateL(grandfather);
						// 更新颜色
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 右左双旋,cur插入parent的左边,且parent为g的右边,形成折线
					{
						// g
						//    p
						// c
						RotateR(parent);
						RotateL(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
		}
		root->col = BLACK; // 调整完后,根节点可能是红,需要调整为黑
		return true;
	}
}

注意:除了inset和erase,后续的find也要跟着改


  1. 根据map的特性,封装红黑树时,inset的返回值类型应该是键值对(pair<iterator, bool>),这里关乎于operator[]的实现问题,也要进行更改…
pair<iterator, bool> insert(const T& data)
	{
		if (root == nullptr)
		{
			root = new Node(data);
			root->col = BLACK; // 根节点始终为黑色
			return make_pair(iterator(root), true); // 插入成功,返回一个键对值
		}

		// 按搜索树规则插入
		Node* cur = root;
		Node* newNode = cur; // 插入后,需要调整颜色,可能改变cur的位置,需要保存
		Node* parent = nullptr;

		while (cur != nullptr)
		{
			if (kot(cur->data) > kot(data)) {
				parent = cur;
				cur = cur->left;
			}
			else if (kot(cur->data) < kot(data)) {
				parent = cur;
				cur = cur->right;
			}
			else {
				return make_pair(iterator(root), false); // 插入失败,返回插入位置的迭代器的pair
			}
		}

		cur = new Node(data);
		// 如果插入节点是红色,可能破坏规则2  ---  如果插入节点是黑色,一定会破坏规则3(3很难处理)
		cur->col = RED;
		if (kot(parent->data) > kot(data)) {
			parent->left = cur;
		}
		else {
			parent->right = cur;
		}
		cur->parent = parent;


		// 存在连续红色节点时,需要一直向上调整
		while (parent != nullptr && parent->col == RED) // 因为插入节点是红色,如果parent也是红色(连续红节点),那么需要调整
		{
			Node* grandfather = parent->parent; // cur的祖父

			// 找叔叔时需要判断在祖父的哪一边
			if (grandfather->left == parent)
			{
				Node* uncle = grandfather->right;

				// 情况一:grandfather为黑,parent为红,uncle为红,cur为红 -- 需要调整g,p,u的颜色
				if (uncle && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					// 调整的这颗树可能为局部子树,需要继续向上处理,祖父的父亲可能为红节点, 更新cur,parent
					cur = grandfather;
					parent = cur->parent;
				}
				// 情况二:grandfather为黑,parent为红,uncle为nullptr/黑,cur为红 -- 需要旋转+变色处理
				else
				{
					// 右旋,插入节点为parent的左边,祖父为旋转点,parent为祖父的左边,uncle为祖父的右边
					if (cur == parent->left)
					{
						//     g
						//   p
						// c
						RotateR(grandfather);
						// 更新颜色 -- 旋转后parent为根,根要变黑(根变黑后所有子树的路径的黑节点数量都加1) -- grandfather为根的子树,变红
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 第三种情况,左右双旋,cur插入parent的右边,且parent为g的左边,,形成折线
					{
						//    g
						// p
						//    c
						RotateL(parent);
						RotateR(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->left;

				if (uncle != nullptr && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					cur = grandfather;
					parent = cur->parent;
				}
				else
				{
					// 左旋,插入节点为parent的右边,祖父为旋转点,parent为祖父的右边,uncle为祖父的左边
					// g
					//    p
					//      c
					if (cur == parent->right)
					{
						RotateL(grandfather);
						// 更新颜色
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 右左双旋,cur插入parent的左边,且parent为g的右边,形成折线
					{
						// g
						//    p
						// c
						RotateR(parent);
						RotateL(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
		}
		root->col = BLACK; // 调整完后,根节点可能是红,需要调整为黑
		return make_pair(iterator(root), true);
	}

注意:插入新的节点后,需要保存一份新节点的地址,否则后面旋转调整会改变,就找不到原来的地址了,inset的返回值需要返回原插入位置的迭代器


🌸3、set的模拟实现

  • set的底层为红黑树,因此只需在set内部封装一棵红黑树,即可将该容器实现出来:
#include "RBTree.h"

namespace MySet
{
	template <typename K>
	class set
	{
	public:
		// 仿函数,用于模板类型传参,控制RBtree中key数据的比较类型
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		// 迭代器
		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator iterator;
		typedef typename RBTree<K, K, SetKeyOfT>::const_iterator const_iterator;
	public:
		iterator begin() const
		{
			return rb.begin();
		}

		iterator end() const
		{
			return rb.end();
		}
	public:
		pair<iterator, bool> insert(const K& key)
		{
			pair<typename RBTree<K, K, SetKeyOfT>::iterator, bool> ret = rb.insert(key);
			return pair<iterator, bool>(iterator(ret.first.pNode) , ret.second);
		}

		iterator find(const K& key)
		{
			return rb.find(key);
		}
	private:
		// set对应红黑树的参数类型为set<K, K>
		RBTree<K, K, SetKeyOfT> rb;
	};
}

🌸4、map的模拟实现

  • map的底层结构就是红黑树,因此在map中直接封装一棵红黑树,然后将其接口包装下即可:
#include "RBTree.h"

namespace MyMap
{
	template <typename K, typename V>
	class map
	{
	public:
		// 仿函数,用于模板类型传参,控制RBtree中key数据的比较类型
		struct MapKeyOfT
		{
			const K& operator()(const pair<K, V>& kv)
			{
				return kv.first;
			}
		};
	public:
		typedef typename RBTree<K, pair<K, V>, MapKeyOfT>::iterator iterator;
		typedef typename RBTree<K, const pair<K, V>, MapKeyOfT>::const_iterator const_iterator;
	public:
		iterator begin()
		{
			return rb.begin();
		}
		iterator end()
		{
			return rb.end();
		}
	public:
		pair<iterator, bool> insert(const pair<K, V>& kv)
		{
			return rb.insert(kv);
		}
		iterator find(const K& key)
		{
			return rb.find(key);
		}
		V& operator[](const K& key)
		{
			pair<iterator, bool> ret = rb.insert(make_pair(key, V()));
			return ret.first->second;
		}
	private:
		// map对应红黑树的参数类型为map<K, pair<K, V>>
		RBTree<K, pair<K, V>, MapKeyOfT> rb;
	};
}

🌳5、完整改造红黑树的代码

#pragma once

#include <iostream>
using namespace std;

enum Color
{
	RED,
	BLACK
};

// T -- 泛化的类型,可能为key也可能为pair< , >
template <typename T>
struct RBTreeNode // 红黑树的节点
{
	RBTreeNode(const T& _data)
		: left(nullptr)
		, right(nullptr)
		, parent(nullptr)
		, data(_data)
		, col(RED)
	{}

	RBTreeNode<T>* left;
	RBTreeNode<T>* right;
	RBTreeNode<T>* parent;
	T data;
	Color col;
};

template <typename T, typename Sef, typename Ptr>
struct RBTree_Iterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTree_Iterator<T, Sef, Ptr> self;

	RBTree_Iterator(Node* _pNode)
		: pNode(_pNode)
	{}

	RBTree_Iterator(const self& s)
		: pNode(s.pNode)
	{}

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

	self& operator++()
	{
		// 右子树不为空,需要一直往上找,因为中序是左 根 右,右完了那么左节点和中间的根节点也遍历完了,控制cur和它的父节点
		if (pNode->right == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->right)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else // 右子树为空,则走这个节点的右子树的最左节点
		{
			Node* subLeft = pNode->right;

			while (subLeft->left)
			{
				subLeft = subLeft->left;
			}
			pNode = subLeft;
		}
		return *this;
	}

	self operator++(int)
	{
		self tmp(*this);
		++(*this);

		return tmp;
	}

	self& operator--()
	{
		// ++反过来走就是--
		if (pNode->left == nullptr)
		{
			Node* cur = pNode;
			Node* parent = cur->parent;

			while (parent != nullptr && cur == parent->left)
			{
				cur = cur->parent;
				parent = parent->parent;
			}
			pNode = parent;
		}
		else
		{
			Node* subRight = pNode->left;

			while (subRight->right)
			{
				subRight = subRight->right;
			}
			pNode = subRight;
		}
		return *this;
	}

	self operator--(int)
	{
		self tmp(*this);
		--(*this);

		return tmp;
	}

	bool operator==(const self& s) { return pNode == s.pNode; }
	bool operator!=(const self& s) { return pNode != s.pNode; }
public:
	Node* pNode;
};

// 红黑树的规则:1.根节点是黑色的,黑节点的左右孩子可能为红也可能为黑
//			   2.如果一个节点为红色,则它的左右孩子节点为黑色(没有连续的红节点)
//			   3.对于每个节点,从该节点到其所有后代叶节点的路径上,均包含相同的数目的黑色节点(每条路径包含相同数目的黑色节点)
//			   4.叶节点(nullptr)是黑色的

// KeyOfT -- 取出红黑树节点中对应的key这个值
template <typename K, typename T, class KeyOfT>
class RBTree
{
private:
	typedef RBTreeNode<T> Node;
public:
	// 正向迭代器
	typedef RBTree_Iterator<T, T&, T*> iterator;
	typedef RBTree_Iterator<T, const T&, const T*> const_iterator;

	iterator begin() // 找左子树中最左的节点就是begin
	{ 
		Node* cur = root;

		while (cur->left)
		{
			cur = cur->left;
		}
		return iterator(cur);
	}
	iterator end() { return iterator(nullptr); }
	const_iterator begin() const
	{
		Node* cur = root;

		while (cur->left)
		{
			cur = cur->left;
		}
		return const_iterator(cur);
	}

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

	iterator find(const K& key)
	{
		Node* cur = root;
		while (cur)
		{
			if (kot(cur->data) > key)
				cur = cur->left;
			else if (kot(cur->data) < key)
				cur = cur->right;
			else
				return iterator(cur);
		}
		return iterator(end());
	}
public:
	RBTree()
		: root(nullptr)
	{}

	RBTree(const RBTree& rb)
		: root(nullptr)
	{
		root = Building_Tree(rb.root);
	}

	RBTree& operator=(const RBTree& rb)
	{
		Destory(root);
		root = Building_Tree(rb.root);
		return *this;
	}

	~RBTree()
	{
		Destory(root);
	}

	pair<iterator, bool> insert(const T& data)
	{
		if (root == nullptr)
		{
			root = new Node(data);
			root->col = BLACK; // 根节点始终为黑色
			return make_pair(iterator(root), true); // 插入成功,返回一个键对值
		}

		// 按搜索树规则插入
		Node* cur = root;
		Node* newNode = cur; // 插入后,需要调整颜色,可能改变cur的位置,需要保存
		Node* parent = nullptr;

		while (cur != nullptr)
		{
			if (kot(cur->data) > kot(data)) {
				parent = cur;
				cur = cur->left;
			}
			else if (kot(cur->data) < kot(data)) {
				parent = cur;
				cur = cur->right;
			}
			else {
				return make_pair(iterator(root), false); // 插入失败,返回插入位置的迭代器的pair
			}
		}

		cur = new Node(data);
		// 如果插入节点是红色,可能破坏规则2  ---  如果插入节点是黑色,一定会破坏规则3(3很难处理)
		cur->col = RED;
		if (kot(parent->data) > kot(data)) {
			parent->left = cur;
		}
		else {
			parent->right = cur;
		}
		cur->parent = parent;


		// 存在连续红色节点时,需要一直向上调整
		while (parent != nullptr && parent->col == RED) // 因为插入节点是红色,如果parent也是红色(连续红节点),那么需要调整
		{
			Node* grandfather = parent->parent; // cur的祖父

			// 找叔叔时需要判断在祖父的哪一边
			if (grandfather->left == parent)
			{
				Node* uncle = grandfather->right;

				// 情况一:grandfather为黑,parent为红,uncle为红,cur为红 -- 需要调整g,p,u的颜色
				if (uncle && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					// 调整的这颗树可能为局部子树,需要继续向上处理,祖父的父亲可能为红节点, 更新cur,parent
					cur = grandfather;
					parent = cur->parent;
				}
				// 情况二:grandfather为黑,parent为红,uncle为nullptr/黑,cur为红 -- 需要旋转+变色处理
				else
				{
					// 右旋,插入节点为parent的左边,祖父为旋转点,parent为祖父的左边,uncle为祖父的右边
					if (cur == parent->left)
					{
						//     g
						//   p
						// c
						RotateR(grandfather);
						// 更新颜色 -- 旋转后parent为根,根要变黑(根变黑后所有子树的路径的黑节点数量都加1) -- grandfather为根的子树,变红
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 第三种情况,左右双旋,cur插入parent的右边,且parent为g的左边,,形成折线
					{
						//    g
						// p
						//    c
						RotateL(parent);
						RotateR(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
			else
			{
				Node* uncle = grandfather->left;

				if (uncle != nullptr && uncle->col == RED)
				{
					parent->col = BLACK;
					uncle->col = BLACK;
					grandfather->col = RED;

					cur = grandfather;
					parent = cur->parent;
				}
				else
				{
					// 左旋,插入节点为parent的右边,祖父为旋转点,parent为祖父的右边,uncle为祖父的左边
					// g
					//    p
					//      c
					if (cur == parent->right)
					{
						RotateL(grandfather);
						// 更新颜色
						parent->col = BLACK;
						grandfather->col = RED;
					}
					else // 右左双旋,cur插入parent的左边,且parent为g的右边,形成折线
					{
						// g
						//    p
						// c
						RotateR(parent);
						RotateL(grandfather);

						// 更新颜色
						grandfather->col = RED;
						cur->col = BLACK;
					}
					break;
				}
			}
		}
		root->col = BLACK; // 调整完后,根节点可能是红,需要调整为黑
		return make_pair(iterator(root), true);
	}
private:
	// 左旋,右边高,左边矮
	void RotateL(Node* parent)
	{
		Node* subR = parent->right;
		Node* subRL = subR->left;

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

		Node* PpNode = parent->parent;

		subR->left = parent;
		parent->parent = subR;

		if (parent == root)
		{
			root = subR;
			root->parent = nullptr;
		}
		else
		{
			if (PpNode->left == parent) {
				PpNode->left = subR;
			}
			else {
				PpNode->right = subR;
			}
			subR->parent = PpNode;
		}
	}

	// 右旋,左边高,右边矮
	void RotateR(Node* parent)
	{
		Node* subL = parent->left;
		Node* subLR = subL->right;

		parent->left = subLR;
		if (subLR != nullptr)
			subLR->parent = parent;

		Node* PpNode = parent->parent;

		subL->right = parent;
		parent->parent = subL;

		if (parent == root)
		{
			root = subL;
			root->parent = nullptr;
		}
		else
		{
			if (PpNode->left == parent) {
				PpNode->left = subL;
			}
			else {
				PpNode->right = subL;
			}
			subL->parent = PpNode;
		}
	}

	Node* Building_Tree(Node* _root)
	{
		if (_root == nullptr)
			return nullptr;

		Node* newTree = new Node(root->data);
		newTree->col = _root->col;

		newTree->left = Building_Tree(_root->left);
		newTree->right = Building_Tree(_root->right);

		return newTree;
	}

	void Destory(Node* _root)
	{
		if (_root == nullptr)
			return;

		Destory(_root->left);
		Destory(_root->right);

		delete _root;
	}
private:
	Node* root;
	KeyOfT kot; // 控制比较时的类型,里面重载了仿函数
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值