当前位置: 首页 > news >正文

【数据结构】红黑树相关知识详细梳理

1. 红黑树的概念

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

        例如:
 

2. 红黑树的性质 

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

        如何理解以上性质呢:

                首先,根据红黑树的主要特点:最长路径中节点个数不会超过最短路径节点
个数的两倍
。在以上性质的约束下,我们可以假设出最长路径和最短路径的差值来看看他是否符合要求。例如:

        可以看到,通过几条性质的约束,的确可以使最长路径中节点个数不会超过最短路径节点个数的两倍,从而达到整棵树的相对平衡。

3. 红黑树插入

        和AVL树一样,在插入节点破坏树的平衡的情况下,红黑树也要进行旋转操作来维持树的平衡。那么什么情况下红黑树会进行旋转,如何旋转呢?

3.1 插入变色/旋转

        在进行旋转操作之前,我们首先要决定插入节点的颜色:

        黑色可以吗?根据性质4(对于每个结点,从该结点到其所有后代叶结点的简单路径上,均 包含相同数目的黑色结点),如果我们将插入节点设为黑色,则一定会破坏规则,由于树形结构的路径具有唯一性,插入节点一定会违反性质4。

        所以我们默认设定除根节点外的(性质2)插入节点为红色。

        那么就有了以下几种情况:

        情况1:

        情况2: 

 

        情况3:

3.2删除

        和AVL树一样,红黑树的删除比较复杂,掌握插入的原理就够用了,这里不再详细说明。

4. 红黑树模拟实现

4.1 红黑树结构:

            首先为了方便封装map和set,我定义了一个头节点方便后续操作。

(头节点的父节点指向红黑树的根,左节点指向红黑树的最小值,右节点指向红黑树的最大值)

        

enum Color
{RED,BLACK
};
template<class T>
struct RBTreeNode
{RBTreeNode(const T& data = T())//构造:_data(data),_color(RED)//新节点默认插入红色,_pParent(nullptr),_pLeft(nullptr),_pRight(nullptr){}T _data;//数据Color _color;//颜色RBTreeNode* _pParent;//父节点RBTreeNode* _pLeft;//左节点RBTreeNode* _pRight;//右节点
};template<class T>
class RBTree
{typedef RBTreeNode<T> Node;
public://默认构造RBTree(){//为了后序封装map和set,本文在实现时给红黑树多增加了一个头节点_pHead = new Node;//可以特别把头节点设为黑色_pHead->_color = BLACK;_pHead->_pLeft = _pHead;_pHead->_pRight = _pHead;_pHead->_pParent = _pHead;}//析构~RBTree(){Destroy(GetRoot());delete _pHead;}//插入bool Insert(const T& data);//查找Node* Find(const T& data);//获取红黑树最左侧节点Node* LeftMost();//获取红黑树最右侧节点Node* RightMost();//检测是否为有效红黑树bool IsValidRBTree();
private://检测是否为有效红黑树bool _IsValidRBTree(Node* pRoot,size_t k,size_t blackcount);//左单旋void RotateL(Node* pParent);//右单旋void RotateR(Node* pParent);//获取根节点Node* GetRoot();//析构void Destroy(Node* pRoot);
private:Node* _pHead;
};

 4.2 构造/析构

//默认构造
RBTree()
{//为了后序封装map和set,本文在实现时给红黑树多增加了一个头节点_pHead = new Node;//可以特别把头节点设为黑色_pHead->_color = BLACK;_pHead->_pLeft = _pHead;_pHead->_pRight = _pHead;_pHead->_pParent = _pHead;
}//析构
~RBTree()
{Destroy(GetRoot());delete _pHead;
}//析构
template<class T>
void RBTree<T>::Destroy(Node* pRoot)
{if (pRoot == nullptr)return;Destroy(pRoot->_pLeft);Destroy(pRoot->_pRight);delete pRoot;
}

4.3 获取元素: 

//查找
template<class T>
typename RBTree<T>::Node* RBTree<T>::Find(const T& data)
{if (_pHead->_pParent == _pHead)return nullptr;Node* pcur = GetRoot();while (pcur){if (data == pcur->_data)return pcur;if (data > pcur->_data)pcur = pcur->_pRight;elsepcur = pcur->_pLeft;}return pcur;
}//获取红黑树最左侧节点
template<class T>
typename RBTree<T>::Node* RBTree<T>::LeftMost()
{if (GetRoot() == nullptr)return nullptr;return _pHead->_pLeft;
}//获取红黑树最右侧节点
template<class T>
typename RBTree<T>::Node* RBTree<T>::RightMost()
{if (GetRoot() == nullptr)return nullptr;return _pHead->_pRight;
}//获取红黑树根节点
template<class T>
typename RBTree<T>::Node* RBTree<T>::GetRoot()
{if (_pHead->_pParent == _pHead)return nullptr;return _pHead->_pParent;
}

4.4 旋转: 

//左单旋
template<class T>
void RBTree<T>::RotateL(Node* pParent)
{Node* subR = pParent->_pRight;Node* subRL = subR->_pLeft;Node* grand = pParent->_pParent;subR->_pParent = grand;//如果祖父节点不是头节点if (grand != _pHead){if (pParent == grand->_pRight)grand->_pRight = subR;elsegrand->_pLeft = subR;}//如果祖父节点是头节点else{grand->_pParent = subR;}subR->_pLeft = pParent;pParent->_pParent = subR;pParent->_pRight = subRL;//如果subRL不为空if (subRL)subRL->_pParent = pParent;//变色pParent->_color = RED;subR->_color = BLACK;
}//右单旋
template<class T>
void RBTree<T>::RotateR(Node* pParent)
{Node* subL = pParent->_pLeft;Node* subLR = subL->_pRight;Node* grand = pParent->_pParent;//如果祖父节点不是头节点subL->_pParent = grand;if (grand != _pHead){if (pParent == grand->_pRight)grand->_pRight = subL;elsegrand->_pLeft = subL;}//如果祖父节点是头节点else{grand->_pParent = subL;}subL->_pRight = pParent;pParent->_pParent = subL;pParent->_pLeft = subLR;//如果subLR不为空if (subLR)subLR->_pParent = pParent;//变色pParent->_color = RED;subL->_color = BLACK;
}

4.5 插入: 

template<class T>
bool RBTree<T>::Insert(const T& data)
{Node* pcur = GetRoot();//如果树为空,只有头节点if (pcur == nullptr){//直接插入节点Node* newnode = new Node(data);_pHead->_pParent = newnode;_pHead->_pLeft = newnode;_pHead->_pRight = newnode;newnode->_pParent = _pHead;newnode->_color = BLACK;//插入成功,返回truereturn true;}//树不为空,按照搜索树规律找到插入位置Node* parent = nullptr;while (pcur){//如果树中存在该元素,插入失败if (data == pcur->_data)return false;parent = pcur;if (data > pcur->_data)pcur = pcur->_pRight;elsepcur = pcur->_pLeft;}//在正确的位置插入节点Node* newnode = new Node(data);if (data > parent->_data)parent->_pRight = newnode;elseparent->_pLeft = newnode;newnode->_pParent = parent;//插入后检查红黑树结构是否需要调整pcur = newnode;Node* grand = nullptr;Node* uncle = nullptr;//以pcur为基准,循环向上调整while (pcur!=GetRoot()){//更新父,叔节点parent = pcur->_pParent;grand = parent->_pParent;uncle = nullptr;//如果父节点颜色为黑,则不需要调整,跳出循环if (parent->_color == BLACK){break;}//父节点颜色为红,违反规则,需要调整else{//此时祖父节点一定存在if (grand->_pRight == parent)uncle = grand->_pLeft;elseuncle = grand->_pRight;//如果叔节点存在if (uncle){//如果叔节点为红色if (uncle->_color == RED){//父,叔节点都变黑,祖父节点变红parent->_color = BLACK;uncle->_color = BLACK;grand->_color = RED;//如果祖父节点为根节点,把祖父节点变黑if (grand == GetRoot())grand->_color = BLACK;//更新pcurpcur = grand;}//如果叔节点为黑色else{//如果叔节点为祖父节点的右节点if (uncle == grand->_pRight){//如果pcur为parent的右节点if (pcur == parent->_pRight){//对parent左单旋RotateL(parent);pcur->_color = RED;}//此时可看做pcur为parent的左节点的情况RotateR(grand);}//如果叔节点为祖父节点的左节点else{//如果pcur为parent的左节点if (pcur == parent->_pLeft){//对parent右单旋RotateR(parent);pcur->_color = RED;}//此时看看作pcur为parent的右节点的情况RotateL(grand);}}}//如果叔节点不存在,则pcur一定为新增节点else{//如果parent为grand的右节点if (parent == grand->_pRight){//如果pcur为parent的左节点if (pcur == parent->_pLeft){RotateR(parent);pcur->_color = RED;}	RotateL(grand);}//如果parent为grand的左节点else{//如果pcur为parent的右节点if (pcur == parent->_pRight){RotateL(parent);pcur->_color = RED;}RotateR(grand);}}}}//更新最大最小值Node* max = GetRoot();while (max->_pRight){max = max->_pRight;}Node* min = GetRoot();while (min->_pLeft){min = min->_pLeft;}//将最小给头节点的左_pHead->_pLeft = min;//将最大给头节点的右_pHead->_pRight = max;//统一返回真return true;
}

4.6 检验红黑树: 

//检测是否为有效红黑树
template<class T>
bool RBTree<T>::IsValidRBTree()
{Node* pRoot = GetRoot();if (pRoot == nullptr)//空树是有效红黑树return true;//检查根节点是否为黑色if (pRoot->_color != BLACK)return false;//记录任意一条路径的黑节点个数size_t blackcount = 0;Node* pcur = pRoot;while (pcur){if (pcur->_color == BLACK)++blackcount;pcur = pcur->_pRight;}size_t k = 0;//调用子函数return _IsValidRBTree(pRoot,k,blackcount);
}//检测是否为有效红黑树
template<class T>
bool RBTree<T>::_IsValidRBTree(Node* pRoot, size_t k, size_t blackcount)
{if (pRoot == nullptr){//一条路径走完,检查黑色节点个数是否和一开始给的相同if (k == blackcount)return true;elsereturn false;}//检查是否有连续的红色节点Node* pParent = pRoot->_pParent;//根节点为红色才判断pParent,不用考虑根节点if ( pRoot->_color == RED && pParent->_color == RED)//有连续的红色节点,违反规则return false;//如果pRoot为黑色节点,计数k+1if (pRoot->_color == BLACK)++k;//继续向子树递归return _IsValidRBTree(pRoot->_pLeft, k, blackcount) && _IsValidRBTree(pRoot->_pRight, k, blackcount);
}

5. 红黑树和AVL树的比较

        红黑树和AVL树都是高效的平衡二叉树,两者也非常相似,增删改查的时间复杂度都是O(log_2N),区别在于,红黑树不追求绝对平衡,其只需保证最长路径不超过最短路径的2倍,相对而言,降低了插入和旋转的次数,所以在经常进行增删的结构中性能比AVL树更优而且红黑树实现比较简单,所以实际运用中红黑树更多。

6. 红黑树的应用

        1. C++ STL库 -- map/set、mutil_map/mutil_set。
        2. Java 库。
        3. linux内核。
        4. 其他一些库。


http://www.mrgr.cn/news/41989.html

相关文章:

  • macos vscode+dosbox 8086汇编语言开发调试环境搭建方法 包含所有相关的工具的下载和安装配置方法
  • 前端面试如何说解vue项目性能优化,你确定不来看看吗?
  • strcmp和strncmp
  • 滚雪球学Oracle[4.4讲]:游标管理
  • 【MySQL】DML数据操作语句和基本的DQL语句
  • 函数式编程的优点和缺点
  • 【寻找one piece的算法之路】——双指针算法!他与她是否会相遇呢?
  • 数字中国建“2522”整体框架
  • Java研发笔记6——C语言程序设计学习笔记5
  • CGLIB原理
  • 影刀RPA实战:excel相关图片操作指令解
  • nodejs:实现大文件的分段上传
  • Cpp::STL—vector类的模拟实现(11)
  • Wireshark 解析QQ、微信的通信协议|TCP|UDP
  • 重生之我们在ES顶端相遇第 19 章 - 综合排序(进阶),打造你的个性化排序
  • QT篇:QT介绍
  • 【编辑距离算法】力扣72. 编辑距离
  • centos常用知识和命令
  • 前端初识之一
  • 【算法题】——数组、双指针