• 洛谷P3369 【模板】普通平衡树 红黑树实现


    您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:

    1. 插入 x 数
    2. 删除 x 数(若有多个相同的数,因只删除一个)
    3. 查询 x 数的排名(排名定义为比当前数小的数的个数 +1 )
    4. 查询排名为 x 的数
    5. 求 x 的前驱(前驱定义为小于 x,且最大的数)
    6. 求 x 的后继(后继定义为大于 x,且最小的数)

    一、红黑树定义

    红黑树的英文是“Red-Black Tree”,简称 R-B Tree,它是一种不严格的平衡二叉查找树

    二叉查找树这一数据结构并不难,而红黑树之所以难是难在它是自平衡的二叉查找树,在进行插入和删除等可能会破坏树的平衡的操作时,需要重新自处理达到平衡状态。

    红黑树是一种含有红色和黑色节点并能自平衡的二叉查找树,红黑树和其他二叉查找树类似,都是在进行插入和删除操作时通过特定操作保持二叉查找树的性质,从而获得较高的查找性能。它虽然是复杂的,但它的最坏情况运行时间也是非常良好的,并且在实践中是高效的:它可以在 O(log n) 时间内做查找,插入和删除,这里的 n 是树中元素的数目。

    二、红黑树性质

    红黑树是一颗二叉搜索树,它在每一个结点上增加了一个储存位来表示结点的颜色,可以是RED或BLACK。通过对任何一条从根到叶子的简单路径上的各个结点的颜色进行约束,红黑树确保没有一条路径会比其他路径长出两倍,因而是近似平衡的。

    树中每个结点包含5个属性:color、key、left、right、p。如果一个结点没有子结点或父结点,则该结点相应指针属性的值为NIL.我们可以把这些NIL视为指向二叉搜索树的叶结点(外部结点)的指针,而把带关键字的结点视为树的内部结点。

    一颗红黑树是满足下面红黑性质的二叉搜索树:

    性质1:每个结点是红色或黑色。

    性质2:根结点是黑色。

    性质3:每个叶结点(NIL)是黑色的。

    性质4:如果一个结点是红色的,则它的两个子结点都是黑色的。

    性质5:对每个结点,从该结点到其后代叶结点的简单路径上,均包含相同数目的黑色结点。

    为了便于处理红黑树代码中的边界条件,使用一个哨兵来代表NIL.对于一颗红黑树T,哨兵T.nil是一个与树中普通结点有相同属性的对象。它的color属性为BLACK,而其他属性p、left、right和key可以设为任意值。所有指向NIL的指针都用指向哨兵T.nil的指针替换。

    结点类代码如下

    1. class Node{
    2. private:
    3. pair<T,V> p;
    4. int s;
    5. bool color;//0代表红色 1代表黑色
    6. Node* Lt;
    7. Node* Rt;
    8. Node* pre;
    9. public:
    10. Node(pair<T,V> _p,int _s,bool _color,Node* _Lt = nullptr,Node* _Rt = nullptr,Node* _pre = nullptr):
    11. p(_p),s(_s),color(_color),Lt(_Lt),Rt(_Rt),pre(_pre){}
    12. Node(Node* tmp):p(tmp->p),s(tmp->s),color(tmp->color),Lt(tmp->Lt),Rt(tmp->Rt),pre(tmp->pre){}
    13. Node*& GetLt() { return Lt;}
    14. Node*& GetRt() { return Rt;}
    15. Node*& Getpre() { return pre;}
    16. T& Getdata() { return p.first;}
    17. V& Getinfo() { return p.second;}
    18. pair<T,V>* Getpair(){ return &p;}
    19. bool Getcolor() const{ return color;}
    20. int& Getsize() { return s;}
    21. void SetLt(Node*& tmp){Lt = tmp;}
    22. void SetRt(Node*& tmp){ Rt = tmp;}
    23. void Setpre(Node*& tmp){ pre = tmp;}
    24. void Setdata(T tmp){ p.first = tmp;}
    25. void Setinfo(V tmp){ p.second = tmp;}
    26. void Setcolor(bool tmp){ color = tmp;}
    27. };

    三、红黑树的相关操作

    在插入、删除节点的过程中,性质3性质4可能会被破坏为了维护这些性质,必须要改变树中某些结点的颜色以及指针结构。

    指针结构的修改是通过旋转(ratation)来完成的,分为左旋和右旋。

     

    左旋以x到y的链为“支轴”进行。它使y成为该子树新的根节点,x成为y的右孩子,y的左孩子成为x的右孩子。

    左旋、右旋代码:

    1. void LRotate(Node* x){//左旋
    2. Node* tmp = Rt(x); //tmp为x的右结点
    3. x->SetRt(Lt(tmp)); //让tmp的左结点作为x的右结点
    4. if(Lt(tmp) != nil) Lt(tmp)->Setpre(x);
    5. tmp->Setpre(pre(x)); //让x的父结点作为tmp的父结点
    6. if(pre(x) == nil) root = tmp; //如果x是根结点,更新根结点为tmp
    7. else{ //如果x是左结点,则让tmp为左结点,如果x是右结点,则让tmp为右结点
    8. if(Lt(pre(x)) == x) pre(x)->SetLt(tmp);
    9. else pre(x)->SetRt(tmp);
    10. }
    11. tmp->SetLt(x); //让x作为tmp的左结点
    12. x->Setpre(tmp); //让tmp作为x的父结点
    13. x->Getsize() = 1;
    14. if(x->GetLt() != nil) x->Getsize() += x->GetLt()->Getsize();
    15. if(x->GetRt() != nil) x->Getsize() += x->GetRt()->Getsize();
    16. tmp->Getsize() = x->Getsize() + 1;
    17. if(tmp->GetRt() != nil) tmp->Getsize() += tmp->GetRt()->Getsize();
    18. }
    19. void RRotate(Node* x){
    20. Node* tmp = Lt(x);
    21. x->SetLt(Rt(tmp));
    22. if(Rt(tmp) != nil) Rt(tmp)->Setpre(x);
    23. tmp->Setpre(pre(x));
    24. if(pre(x) == nil) root = tmp;
    25. else{
    26. if(Rt(pre(x)) == x) pre(x)->SetRt(tmp);
    27. else pre(x)->SetLt(tmp);
    28. }
    29. tmp->SetRt(x);
    30. x->Setpre(tmp);
    31. x->Getsize() = 1;
    32. if(x->GetLt() != nil) x->Getsize() += x->GetLt()->Getsize();
    33. if(x->GetRt() != nil) x->Getsize() += x->GetRt()->Getsize();
    34. tmp->Getsize() = x->Getsize() + 1;
    35. if(tmp->GetLt() != nil) tmp->Getsize() += tmp->GetLt()->Getsize();
    36. }

    红黑树的插入

    插入结点的颜色为红色(插入结点的颜色为黑色,可能破坏性质四,插入结点为红色,可能破坏性质三,但修复性质三的代价比修复性质四的代价小,所以选择插入结点的颜色为红色)

    第一步: 我们先按照二叉搜索树树插入节点的方式,插入节点
    第二步: 为了不破坏红黑树的规则,我们插入节点后要对红黑树进行相应的调整

    下面分两种情况

    第一种情况: 如果插入节点的父亲是黑色节点,那么可以直接结束,不需要继续调整了没有破坏红黑树的基本性质)
    第二种情况: 如果插入节点为的父亲为红色节点,就需要进行相应的调整性质4被破坏)

     

    上图为调整情况1:z的叔结点y是红色的

    这种情况在z.p和y都是红色时发生。因为z.p.p是黑色的,所以将z.p和y都着为黑色,以此解决z和z.p都是红色的问题,将z.p.p着为红色一保持性质5。然后,把z.p.p作为新结点z来重复修正被破坏的性质,直到所有性质都满足。指针z在树中上移两层。

     

    情况2:z的叔结点y是黑色的且z是一个右孩子

    情况3:z的叔结点y是黑色的且z是一个左孩子

    在情况2和情况3中,z的叔结点y是黑色的,通过z是z.p的右孩子还是左孩子来区别这两种情况。在情况2中,结点z是它父结点的右孩子,可以使用一个左旋来将此情形转变为情况3,此时结点z为左孩子。因为z和z.p都是红色的,所以该旋转对性质无影响。无论是直接进入情况3,还是通过情况2进入情况3,z的叔结点y总是是黑色的,否则就要执行情况1。在情况3中,改变某些结点的颜色并做一次右旋,以保持性质5。这样由于不再有两个红色结点相邻,所有处理到处完毕。因为此时z.p是黑色的,无需再进行循环。

    插入代码如下:

    1. void Insert(pair<T,V> p)
    2. {
    3. Node* y = nil;
    4. Node* x = root;
    5. Node* z = new Node(p,0,nil,nil,nil); //z为要插入的结点
    6. while(x != nil){ //寻找插入的位置
    7. y = x;
    8. if(p.first < x->Getdata()) x = Lt(x);
    9. else x = Rt(x);
    10. }
    11. z->Setpre(y); //让y作为z的父结点
    12. if(y == nil) root = z; //如果树为空,则z为根结点
    13. else if(z->Getdata() < y->Getdata()){ //如果z的key比y的key小,则z为y的左结点,否则为右 结点
    14. y->SetLt(z);
    15. }
    16. else y->SetRt(z);
    17. z->SetLt(nil);
    18. z->SetRt(nil);
    19. z->Setcolor(0);
    20. FixAfterInsert(z); //插入之后的调整
    21. }
    22. void FixAfterInsert(Node* z){
    23. while(color(z->Getpre()) == 0){ //如果z的父结点为红色,进入循环,否则结束循环
    24. if(pre(z) == Lt(pre(pre(z)))){ //如果z的父结点是z的祖父结点的左结点
    25. Node* y = Rt(pre(pre(z))); //y为z的叔结点
    26. if(color(y) == 0){ //如果y结点为红色进入情况1
    27. pre(z)->Setcolor(1); //case1 将z的父节点的颜色改为黑色
    28. y->Setcolor(1); //case1 将y的颜色改为黑色
    29. pre(pre(z))->Setcolor(0); //case1 将祖父结点的颜色改为红色
    30. z = pre(pre(z)); //case1 将指针z上移两层
    31. continue; //进行下一次循环
    32. } //y结点为黑色的情况
    33. if(z == Rt(pre(z))){ //case2 如果z是其父结点的右结点
    34. z = pre(z); //case2 对z的父结点做左旋
    35. LRotate(z); //case2 左旋之后z结点的key和val不变,只是右结点和父结点变了
    36. }
    37. pre(z)->Setcolor(1); //case3 将z的父结点的颜色改为黑色
    38. pre(pre(z))->Setcolor(0); //case3 将z的祖父结点的颜色改为红色
    39. RRotate(pre(pre(z))); //case3 对z的祖父结点右旋
    40. }
    41. else{ //z的父结点是z的祖父结点的y右结点的情况,与z的父结点是z的祖父结点的y左结点的情况对称
    42. Node* y = Lt(pre(pre(z)));
    43. if(color(y) == 0){
    44. pre(z)->Setcolor(1); //case1
    45. y->Setcolor(1); //case1
    46. pre(pre(z))->Setcolor(0); //case1
    47. z = pre(pre(z)); //case1
    48. continue;
    49. }
    50. if(z == Lt(pre(z))){ //case2
    51. z = pre(z); //case2
    52. RRotate(z); //case2
    53. }
    54. pre(z)->Setcolor(1); //case3
    55. pre(pre(z))->Setcolor(0); //case3
    56. LRotate(pre(pre(z))); //case3
    57. }
    58. }
    59. root->Setcolor(1); //循环过程中有可能修改根结点的颜色,要保持根结点为黑色
    60. }

    红黑树的删除

    与插入操作相比,删除操作要稍微麻烦一些。

    先要设计一个删除过程中要使用到的替换函数transplant,代码如下:

    1. void transplant(Node* u,Node* v){
    2. if(pre(u) == nil) root = v; //如果u是根结点,则让v为根结点
    3. else if(u == Lt(pre(u))) pre(u)->SetLt(v); //如果u是左结点,则让v为左结点
    4. else pre(u)->SetRt(v); //如果u是右结点,则让v为右结点
    5. v->Setpre(pre(u)); //让u的父结点作为v的父结点
    6. }

    还要设计一个寻找key值最小的结点的函数minimum,代码如下:

    1. Node* minimum(Node* x){
    2. if(x == nil) return x;
    3. while(Lt(x) != nil){
    4. x->Getsize()--; //删除时要维护size大小
    5. x = Lt(x);
    6. }
    7. return x;
    8. }

    下面进入删除:

    删除一个结点之后为了维护二叉搜索树的性质,需要找一个结点来替代删除结点的位置

    第一步: 我们先按照二叉搜索树树删除节点的方式,删除节点
    第二步: 为了不破坏红黑树的规则,我们删除节点后要对红黑树进行相应的调整

    下面分两种情况:

    第一种情况:删除的结点有一个左结点或一个右结点不是叶结点,或左右结点都是叶结点

    如果左右结点都是叶结点则用叶结点替换要删除的结点。如果删除的结点有一个左结点或一个右结点不是叶结点,则用非叶结点替换要删除的结点。

    第二种情况:删除的结点没有叶结点。

    用删除结点的后继结点替换删除的结点,后继结点是大于该结点且最小的结点。以删除结点的右结点为根结点的树中的最小结点就是删除结点的后继结点。由于后继结点替代了删除结点,后继结点也需要有结点替代,由于后继结点没有左结点,则后继结点的替代结点就是它的右结点。此时,有一种特殊情况,当删除结点的后继结点就是删除结点的右结点时,后继结点无需替代结点。

    如果删除的是黑色结点,则需要调整(破坏了性质5)。

    调整的情况:

    蓝色表示可以是红色也可以是黑色

     

    情况1:x的兄弟结点w是红色的

    因为w必须有黑色子结点,所以可以改变w的x.p的颜色,然后对x.p做一次左旋而不违反红黑树的任何性质。现在,x的新兄弟结点是旋转之前的w的某个子结点,其颜色为黑色。这样就将情况1转换为情况2、3或4处理。

    当结点w为黑色时,属于情况2、3和4;这些情况是由w的子结点的颜色来区分的。

    情况2:x的兄弟结点w是黑色的,而且w的两个子结点都是黑色的

    因为w的颜色是黑色的,所以在删除x结点时将w的颜色改为红色,这样B结点的左子树和右子树中都少了一个黑色结点。如果B结点是红色的,则退出循环,让B结点的颜色为黑色,补偿一个黑色结点。如果B结点是黑色的,B结点无法补偿一个黑色结点,则等效删除了一个黑色结点,B结点为删除结点的替代结点,继续循环。

    情况3:x的兄弟结点w是黑色的,w的左结点是红色的,w的右结点是黑色的

    可以交换w和w的左结点的颜色,然后对w进行右旋而不违反红黑树的任何性质。现在x的新兄弟结点w是一个有右孩子的黑色结点,这样就将情况3转换成了情况4。

    情况4:x的兄弟结点w是黑色的,且w的右结点是红色的

    可以交换B和w的颜色,并让E的颜色为黑色,然后对B进行一次左旋,这样删除x结点之后红黑树的性质没有被破坏。因为旋转前x结点的黑高等于C结点的黑高加一,所以在删除x结点之后,B的左子树的黑高等于右子树的黑高。旋转后B的黑高等于旋转前D的黑高(旋转后的B与旋转前的D都有相同的子树C)等于旋转前E的黑高,而旋转前E的黑高等于旋转后E的黑高,所以旋转后B的黑高和旋转后E的黑高相等,旋转前B的黑高等于旋转前E的黑高加一,旋转后D的黑高等于E的黑高加一,所以旋转前B的黑高等于旋转后D的黑高。所以红黑树的性质没有被破坏。这时应该把x置为根结点,以结束循环。

    删除代码如下:

    1. bool Delete(Node* z){
    2. Node* y = z; //y为z的替代结点
    3. int yoc = color(y); //记录y的颜色
    4. Node* x = z; //x为y的替代结点
    5. if(Lt(z) == nil){ //z有两个叶结点或左结点为叶结点
    6. x = Rt(z);
    7. transplant(z,Rt(z)); //用z的右结点替换z
    8. }
    9. else if(Rt(z) == nil){ //z的右结点为叶结点
    10. x = Lt(z);
    11. transplant(z,Lt(z)); //用z的左结点替换z
    12. }
    13. else{ //z没有叶结点
    14. y = minimum(Rt(z)); //让y为z的后继结点
    15. yoc = color(y); //记录y的颜色
    16. x = Rt(y); //让x为y的右结点
    17. if(pre(y) == z){ //判断特殊情况
    18. x->Setpre(y);
    19. }
    20. else{ //当y不是z的右结点时
    21. transplant(y,Rt(y)); //用y的右结点替换y
    22. y->SetRt(Rt(z)); //把z的右结点作为y右结点
    23. Rt(y)->Setpre(y);
    24. }
    25. transplant(z,y); // 用y替换z
    26. y->SetLt(Lt(z)); //把z的左结点作为y的左结点
    27. Lt(y)->Setpre(y);
    28. y->Setcolor(color(z)); //把y的颜色改为z的颜色
    29. }
    30. delete z;
    31. if(yoc == 1){ //当删除结点为黑色时,需要调整
    32. FixAfterDelete(x);
    33. }
    34. return true;
    35. }
    36. bool Delete(T val){
    37. Node* tmp = this->root;
    38. while(tmp != nil){ //寻找要删除的结点
    39. if(tmp->Getdata() > val){
    40. tmp = tmp->GetLt();
    41. }
    42. else if(tmp->Getdata() < val){
    43. tmp = tmp->GetRt();
    44. }
    45. else break;
    46. }
    47. if(tmp == nil){ //没有找到
    48. return false;
    49. }
    50. else{
    51. return Delete(tmp);
    52. }
    53. }
    54. void FixAfterDelete(Node* x){
    55. while(x != root && color(x) == 1){ //当x不为根结点或x的颜色为黑色
    56. if(Lt(pre(x)) == x){ //如果x的左结点
    57. Node* w = Rt(pre(x)); //w是x的兄弟结点
    58. if(color(w) == 0){ //如果w的颜色为红色
    59. w->Setcolor(1); //case1 让w的颜色为黑色
    60. pre(x)->Setcolor(0); //case1 让x的父结点的颜色为红色
    61. LRotate(pre(x)); //case1 对x的父结点左旋
    62. w = Rt(pre(x)); //case1 更新兄弟结点
    63. }
    64. if(color(Lt(w)) == 1 && color(Rt(w)) == 1){ //如果w有两个黑色子结点
    65. w->Setcolor(0); //case2 //让w的颜色为红色
    66. x = pre(x); //case2 //让x为x的父节点
    67. }
    68. else{
    69. if(color(Rt(w)) == 1){ //如果w的右结点为黑色
    70. Lt(w)->Setcolor(1); //case3 让w的左结点的颜色为黑色
    71. w->Setcolor(0); //case3 让w的颜色为红色
    72. RRotate(w); //case3 对w进行右旋
    73. w = Rt(pre(x)); //case3 更新兄弟结点
    74. }
    75. w->Setcolor(color(pre(x))); //case4 让w和x的父结点交换颜色
    76. pre(x)->Setcolor(1); //case4 让x的父结点的颜色为黑色
    77. Rt(w)->Setcolor(1); //case4 让w的右结点的颜色为黑色
    78. LRotate(pre(x)); //case4 对x的父结点进行左旋
    79. x = root; //case4 让x成为根结点,结束循环
    80. }
    81. }
    82. else{ //x是右结点的情况 与x是左结点的情况对称
    83. Node* w = Lt(pre(x));
    84. if(color(w) == 0){
    85. w->Setcolor(1); //case1
    86. pre(x)->Setcolor(0); //case1
    87. RRotate(pre(x)); //case1
    88. w = Lt(pre(x)); //case1
    89. }
    90. if(color(Lt(w)) == 1 && color(Rt(w)) == 1){
    91. w->Setcolor(0); //case2
    92. x = pre(x); //case2
    93. }
    94. else{
    95. if(color(Lt(w)) == 1){
    96. Rt(w)->Setcolor(1); //case3
    97. w->Setcolor(0); //case3
    98. LRotate(w); //case3
    99. w = Lt(pre(x)); //case3
    100. }
    101. w->Setcolor(color(pre(x))); //case4
    102. pre(x)->Setcolor(1); //case4
    103. Lt(w)->Setcolor(1); //case4
    104. RRotate(pre(x)); //case4
    105. x = root; //case4
    106. }
    107. }
    108. }
    109. x->Setcolor(1);
    110. }

    前驱、后继:

    1. Node* Lt_node(Node* x){ //前驱
    2. Node* tmp = x;
    3. if(tmp->GetLt() == nil){
    4. while(tmp->Getpre() != nil && tmp->Getpre()->GetLt() == tmp){
    5. tmp = tmp->Getpre();
    6. }
    7. tmp = tmp->Getpre();
    8. }
    9. else{
    10. tmp = tmp->GetLt();
    11. while(tmp->GetRt() != nil){
    12. tmp = tmp->GetRt();
    13. }
    14. }
    15. return tmp;
    16. }
    17. Node* Rt_node(Node* x){ //后继
    18. Node* tmp = x;
    19. if(tmp->GetRt() == nil){
    20. while(tmp->Getpre() != nil && tmp->Getpre()->GetRt() == tmp){
    21. tmp = tmp->Getpre();
    22. }
    23. tmp = tmp->Getpre();
    24. }
    25. else{
    26. tmp = tmp->GetRt();
    27. while(tmp->GetLt() != nil){
    28. tmp = tmp->GetLt();
    29. }
    30. }
    31. return tmp;
    32. }
    33. Node* Lfind(T key){ //key不一定在树中,所以要分类
    34. Node* parent = nil;
    35. Node* current = root;
    36. Node* t = nil;
    37. while(current != nil){//当当前节点不为空时 寻找结点
    38. if(current->Getdata() > key){
    39. parent = current;
    40. current = current->GetLt();
    41. }
    42. else if(current->Getdata() < key){
    43. parent = current;
    44. current = current->GetRt();
    45. }
    46. else{
    47. t = Lt_node(current);
    48. break;
    49. }
    50. }
    51. if(current->Getdata() == key){ //处理同值的情况
    52. while(t->Getdata() == key){
    53. t = Lt_node(t);
    54. }
    55. return t;
    56. }
    57. if(parent->Getdata() > key){
    58. return Lt_node(parent);
    59. }
    60. else{
    61. return parent;
    62. }
    63. }
    64. Node* Rfind(T key){
    65. Node* parent = nil;
    66. Node* current = root;
    67. Node* t = nil;
    68. while(current != nil){//当当前节点不为空时
    69. if(current->Getdata() > key){
    70. parent = current;
    71. current = current->GetLt();
    72. }
    73. else if(current->Getdata() < key){
    74. parent = current;
    75. current = current->GetRt();
    76. }
    77. else{
    78. t = Rt_node(current);
    79. break;
    80. }
    81. }
    82. if(current->Getdata() == key){
    83. while(t->Getdata() == key){
    84. t = Rt_node(t);
    85. }
    86. return t;
    87. }
    88. if(parent->Getdata() < key){
    89. return Rt_node(parent);
    90. }
    91. else{
    92. return parent;
    93. }
    94. }

    寻找第k大、查找排名(相同结点不合并版本)

    1. Node* findkth(int k,Node* p){
    2. if(p->GetLt() == nil){
    3. if(k == 1){
    4. return p;
    5. }
    6. else{
    7. return findkth(k-1,p->GetRt());
    8. }
    9. }
    10. else{
    11. if(p->GetLt()->Getsize() == k-1) return p;
    12. else if(p->GetLt()->Getsize() >= k) return findkth(k,p->GetLt());
    13. else return findkth(k-p->GetLt()->Getsize()-1,p->GetRt());
    14. }
    15. }
    16. int find_rank(T v,Node* p){
    17. if(p == nil) return 1;
    18. else if(p->Getdata() >= v) return find_rank(v,p->GetLt());
    19. else return (1 + (p->GetLt() ? p->GetLt()->Getsize() : 0 ) + find_rank(v,p->GetRt()));
    20. }

    完整代码:

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. #define RED 0 //红色结点
    4. #define BLACK 1 //黑色结点
    5. template<typename T,typename V>
    6. class redblacktree{
    7. private:
    8. class Node{
    9. private:
    10. pair<T,V> p;
    11. int s;
    12. bool color;//0代表红色 1代表黑色
    13. Node* Lt;
    14. Node* Rt;
    15. Node* pre;
    16. public:
    17. Node(pair<T,V> _p,int _s,bool _color,Node* _Lt = nullptr,Node* _Rt = nullptr,Node* _pre = nullptr):
    18. p(_p),s(_s),color(_color),Lt(_Lt),Rt(_Rt),pre(_pre){}
    19. Node(Node* tmp):p(tmp->p),s(tmp->s),color(tmp->color),Lt(tmp->Lt),Rt(tmp->Rt),pre(tmp->pre){}
    20. Node*& GetLt() { return Lt;}
    21. Node*& GetRt() { return Rt;}
    22. Node*& Getpre() { return pre;}
    23. T& Getdata() { return p.first;}
    24. V& Getinfo() { return p.second;}
    25. pair<T,V>* Getpair(){ return &p;}
    26. bool Getcolor() const{ return color;}
    27. int& Getsize() { return s;}
    28. void SetLt(Node*& tmp){Lt = tmp;}
    29. void SetRt(Node*& tmp){ Rt = tmp;}
    30. void Setpre(Node*& tmp){ pre = tmp;}
    31. void Setdata(T tmp){ p.first = tmp;}
    32. void Setinfo(V tmp){ p.second = tmp;}
    33. void Setcolor(bool tmp){ color = tmp;}
    34. };
    35. bool color(Node* node){
    36. if(node == nil) return true;
    37. else return node->Getcolor();
    38. }
    39. void Setcolor(Node* node,bool col){
    40. node->Setcolor(col);
    41. }
    42. Node*& pre(Node* node){
    43. return node->Getpre();
    44. }
    45. Node*& Lt(Node* node){
    46. return node->GetLt();
    47. }
    48. Node*& Rt(Node* node){
    49. return node->GetRt();
    50. }
    51. void LRotate(Node* x){//左旋
    52. Node* tmp = Rt(x);
    53. x->SetRt(Lt(tmp));
    54. if(Lt(tmp) != nil) Lt(tmp)->Setpre(x);
    55. tmp->Setpre(pre(x));
    56. if(pre(x) == nil) root = tmp;
    57. else{
    58. if(Lt(pre(x)) == x) pre(x)->SetLt(tmp);
    59. else pre(x)->SetRt(tmp);
    60. }
    61. tmp->SetLt(x);
    62. x->Setpre(tmp);
    63. x->Getsize() = 1;
    64. if(x->GetLt() != nil) x->Getsize() += x->GetLt()->Getsize();
    65. if(x->GetRt() != nil) x->Getsize() += x->GetRt()->Getsize();
    66. tmp->Getsize() = x->Getsize() + 1;
    67. if(tmp->GetRt() != nil) tmp->Getsize() += tmp->GetRt()->Getsize();
    68. }
    69. void RRotate(Node* x){
    70. Node* tmp = Lt(x);
    71. x->SetLt(Rt(tmp));
    72. if(Rt(tmp) != nil) Rt(tmp)->Setpre(x);
    73. tmp->Setpre(pre(x));
    74. if(pre(x) == nil) root = tmp;
    75. else{
    76. if(Rt(pre(x)) == x) pre(x)->SetRt(tmp);
    77. else pre(x)->SetLt(tmp);
    78. }
    79. tmp->SetRt(x);
    80. x->Setpre(tmp);
    81. x->Getsize() = 1;
    82. if(x->GetLt() != nil) x->Getsize() += x->GetLt()->Getsize();
    83. if(x->GetRt() != nil) x->Getsize() += x->GetRt()->Getsize();
    84. tmp->Getsize() = x->Getsize() + 1;
    85. if(tmp->GetLt() != nil) tmp->Getsize() += tmp->GetLt()->Getsize();
    86. }
    87. void transplant(Node* u,Node* v){
    88. if(pre(u) == nil) root = v;
    89. else if(u == Lt(pre(u))) pre(u)->SetLt(v);
    90. else pre(u)->SetRt(v);
    91. v->Setpre(pre(u));
    92. }
    93. Node* minimum(Node* x){
    94. if(x == nil) return x;
    95. while(Lt(x) != nil){
    96. x->Getsize()--;
    97. x = Lt(x);
    98. }
    99. return x;
    100. }
    101. Node* maximum(Node* x){
    102. if(x == nil) return x;
    103. while(Rt(x) != nil){
    104. x = Rt(x);
    105. }
    106. return x;
    107. }
    108. Node* Lt_node(Node* x){ //前驱
    109. Node* tmp = x;
    110. if(tmp->GetLt() == nil){
    111. while(tmp->Getpre() != nil && tmp->Getpre()->GetLt() == tmp){
    112. tmp = tmp->Getpre();
    113. }
    114. tmp = tmp->Getpre();
    115. }
    116. else{
    117. tmp = tmp->GetLt();
    118. while(tmp->GetRt() != nil){
    119. tmp = tmp->GetRt();
    120. }
    121. }
    122. return tmp;
    123. }
    124. Node* Rt_node(Node* x){ //后继
    125. Node* tmp = x;
    126. if(tmp->GetRt() == nil){
    127. while(tmp->Getpre() != nil && tmp->Getpre()->GetRt() == tmp){
    128. tmp = tmp->Getpre();
    129. }
    130. tmp = tmp->Getpre();
    131. }
    132. else{
    133. tmp = tmp->GetRt();
    134. while(tmp->GetLt() != nil){
    135. tmp = tmp->GetLt();
    136. }
    137. }
    138. return tmp;
    139. }
    140. public:
    141. Node* root;
    142. Node* nil;
    143. redblacktree(){
    144. nil = new Node({-1,-1},0,BLACK,nil,nil,nil);
    145. root = nil;
    146. }
    147. class iterator{
    148. private:
    149. Node* _node;
    150. public:
    151. Node* Getnode(){ return _node;}
    152. iterator& operator++(){
    153. _node = _node->Rt_node();
    154. return *this;
    155. }
    156. iterator& operator--(){
    157. _node = _node->Lt_node();
    158. return *this;
    159. }
    160. iterator& operator=(iterator a){
    161. _node = a._node;
    162. return *this;
    163. }
    164. friend bool operator ==(iterator a,iterator b){
    165. return a.Getnode() == b.Getnode();
    166. }
    167. friend bool operator !=(iterator a,iterator b){
    168. return a.Getnode() != b.Getnode();
    169. }
    170. pair<T,V>* operator->(){ return _node->Getpair();}
    171. iterator(Node* __node = nullptr):_node(__node){}
    172. iterator(iterator const& iter):_node(iter._node){}
    173. };
    174. Node* find(Node* x,T key){
    175. Node* tmp = x;
    176. while(tmp != nil){
    177. if(tmp->Getdata() > key){
    178. tmp = tmp->GetLt();
    179. }
    180. else if(tmp->Getdata() < key){
    181. tmp = tmp->GetRt();
    182. }
    183. else break;
    184. }
    185. return tmp;
    186. }
    187. Node* Lfind(T key){
    188. Node* parent = nil;
    189. Node* current = root;
    190. Node* t = nil;
    191. while(current != nil){//当当前节点不为空时
    192. if(current->Getdata() > key){
    193. parent = current;
    194. current = current->GetLt();
    195. }
    196. else if(current->Getdata() < key){
    197. parent = current;
    198. current = current->GetRt();
    199. }
    200. else{
    201. t = Lt_node(current);
    202. break;
    203. }
    204. }
    205. if(current->Getdata() == key){
    206. while(t->Getdata() == key){
    207. t = Lt_node(t);
    208. }
    209. return t;
    210. }
    211. if(parent->Getdata() > key){
    212. return Lt_node(parent);
    213. }
    214. else{
    215. return parent;
    216. }
    217. }
    218. Node* Rfind(T key){
    219. Node* parent = nil;
    220. Node* current = root;
    221. Node* t = nil;
    222. while(current != nil){//当当前节点不为空时
    223. if(current->Getdata() > key){
    224. parent = current;
    225. current = current->GetLt();
    226. }
    227. else if(current->Getdata() < key){
    228. parent = current;
    229. current = current->GetRt();
    230. }
    231. else{
    232. t = Rt_node(current);
    233. break;
    234. }
    235. }
    236. if(current->Getdata() == key){
    237. while(t->Getdata() == key){
    238. t = Rt_node(t);
    239. }
    240. return t;
    241. }
    242. if(parent->Getdata() < key){
    243. return Rt_node(parent);
    244. }
    245. else{
    246. return parent;
    247. }
    248. }
    249. void Insert(pair<T,V> p)
    250. {
    251. Node* y = nil;
    252. Node* x = root;
    253. Node* z = new Node(p,1,RED,nil,nil,nil);
    254. while(x != nil){
    255. y = x;
    256. x->Getsize()++;
    257. if(p.first < x->Getdata()) x = Lt(x);
    258. else x = Rt(x);
    259. }
    260. z->Setpre(y);
    261. if(y == nil) root = z;
    262. else if(z->Getdata() < y->Getdata()){
    263. y->SetLt(z);
    264. }
    265. else y->SetRt(z);
    266. z->SetLt(nil);
    267. z->SetRt(nil);
    268. z->Setcolor(RED);
    269. FixAfterInsert(z);
    270. }
    271. void FixAfterInsert(Node* z){
    272. while(color(z->Getpre()) == RED){
    273. if(pre(z) == Lt(pre(pre(z)))){
    274. Node* y = Rt(pre(pre(z)));
    275. if(color(y) == RED){
    276. pre(z)->Setcolor(BLACK); //case1
    277. y->Setcolor(BLACK); //case1
    278. pre(pre(z))->Setcolor(RED); //case1
    279. z = pre(pre(z)); //case1
    280. continue;
    281. }
    282. if(z == Rt(pre(z))){ //case2
    283. z = pre(z); //case2
    284. LRotate(z); //case2
    285. }
    286. pre(z)->Setcolor(BLACK); //case3
    287. pre(pre(z))->Setcolor(RED); //case3
    288. RRotate(pre(pre(z))); //case3
    289. }
    290. else{
    291. Node* y = Lt(pre(pre(z)));
    292. if(color(y) == RED){
    293. pre(z)->Setcolor(BLACK); //case1
    294. y->Setcolor(BLACK); //case1
    295. pre(pre(z))->Setcolor(RED); //case1
    296. z = pre(pre(z)); //case1
    297. continue;
    298. }
    299. if(z == Lt(pre(z))){ //case2
    300. z = pre(z); //case2
    301. RRotate(z); //case2
    302. }
    303. pre(z)->Setcolor(BLACK); //case3
    304. pre(pre(z))->Setcolor(RED); //case3
    305. LRotate(pre(pre(z))); //case3
    306. }
    307. }
    308. root->Setcolor(BLACK);
    309. }
    310. bool Delete(Node* z){
    311. Node* y = z;
    312. int yoc = color(y);
    313. Node* x = z;
    314. if(Lt(z) == nil){
    315. x = Rt(z);
    316. transplant(z,Rt(z));
    317. }
    318. else if(Rt(z) == nil){
    319. x = Lt(z);
    320. transplant(z,Lt(z));
    321. }
    322. else{
    323. y = minimum(Rt(z));
    324. yoc = color(y);
    325. x = Rt(y);
    326. if(pre(y) == z){
    327. x->Setpre(y);
    328. }
    329. else{
    330. transplant(y,Rt(y));
    331. y->SetRt(Rt(z));
    332. Rt(y)->Setpre(y);
    333. }
    334. transplant(z,y);
    335. y->Getsize() = z->Getsize();
    336. y->SetLt(Lt(z));
    337. Lt(y)->Setpre(y);
    338. y->Setcolor(color(z));
    339. }
    340. delete z;
    341. if(yoc == BLACK){
    342. FixAfterDelete(x);
    343. }
    344. return true;
    345. }
    346. bool Delete(T val){
    347. Node* tmp = this->root;
    348. while(tmp != nil){
    349. tmp->Getsize()--;
    350. if(tmp->Getdata() > val){
    351. tmp = tmp->GetLt();
    352. }
    353. else if(tmp->Getdata() < val){
    354. tmp = tmp->GetRt();
    355. }
    356. else break;
    357. }
    358. if(tmp == nil){
    359. return false;
    360. }
    361. else{
    362. return Delete(tmp);
    363. }
    364. }
    365. void FixAfterDelete(Node* x){
    366. while(x != root && color(x) == BLACK){
    367. if(Lt(pre(x)) == x){
    368. Node* w = Rt(pre(x));
    369. if(color(w) == RED){
    370. w->Setcolor(BLACK); //case1
    371. pre(x)->Setcolor(RED); //case1
    372. LRotate(pre(x)); //case1
    373. w = Rt(pre(x)); //case1
    374. }
    375. if(color(Lt(w)) == BLACK && color(Rt(w)) == BLACK){
    376. w->Setcolor(RED); //case2
    377. x = pre(x); //case2
    378. }
    379. else{
    380. if(color(Rt(w)) == BLACK){
    381. Lt(w)->Setcolor(BLACK); //case3
    382. w->Setcolor(RED); //case3
    383. RRotate(w); //case3
    384. w = Rt(pre(x)); //case3
    385. }
    386. w->Setcolor(color(pre(x))); //case4
    387. pre(x)->Setcolor(BLACK); //case4
    388. Rt(w)->Setcolor(BLACK); //case4
    389. LRotate(pre(x)); //case4
    390. x = root; //case4
    391. }
    392. }
    393. else{
    394. Node* w = Lt(pre(x));
    395. if(color(w) == RED){
    396. w->Setcolor(BLACK); //case1
    397. pre(x)->Setcolor(RED); //case1
    398. RRotate(pre(x)); //case1
    399. w = Lt(pre(x)); //case1
    400. }
    401. if(color(Lt(w)) == BLACK && color(Rt(w)) == BLACK){
    402. w->Setcolor(RED); //case2
    403. x = pre(x); //case2
    404. }
    405. else{
    406. if(color(Lt(w)) == BLACK){
    407. Rt(w)->Setcolor(BLACK); //case3
    408. w->Setcolor(RED); //case3
    409. LRotate(w); //case3
    410. w = Lt(pre(x)); //case3
    411. }
    412. w->Setcolor(color(pre(x))); //case4
    413. pre(x)->Setcolor(BLACK); //case4
    414. Lt(w)->Setcolor(BLACK); //case4
    415. RRotate(pre(x)); //case4
    416. x = root; //case4
    417. }
    418. }
    419. }
    420. x->Setcolor(BLACK);
    421. }
    422. Node* findkth(int k,Node* p){
    423. if(p->GetLt() == nil){
    424. if(k == 1){
    425. return p;
    426. }
    427. else{
    428. return findkth(k-1,p->GetRt());
    429. }
    430. }
    431. else{
    432. if(p->GetLt()->Getsize() == k-1) return p;
    433. else if(p->GetLt()->Getsize() >= k) return findkth(k,p->GetLt());
    434. else return findkth(k-p->GetLt()->Getsize()-1,p->GetRt());
    435. }
    436. }
    437. int find_rank(T v,Node* p){
    438. if(p == nil) return 1;
    439. else if(p->Getdata() >= v) return find_rank(v,p->GetLt());
    440. else return (1 + (p->GetLt() ? p->GetLt()->Getsize() : 0 ) + find_rank(v,p->GetRt()));
    441. }
    442. Node* begin(){
    443. Node* t = root;
    444. while(t->Lt_node() != nil){
    445. t = t->Lt_node();
    446. }
    447. return t;
    448. }
    449. Node* end(){
    450. Node* t = root;
    451. while(t->Rt_node() != nil){
    452. t = t->Rt_node();
    453. }
    454. return t;
    455. }
    456. };
    457. int main(){
    458. ios::sync_with_stdio(false);
    459. cin.tie(0),cout.tie(0);
    460. redblacktree<int,int>* mp = new redblacktree<int,int>();
    461. int n,m,num;
    462. cin >> n;
    463. for(int i = 0;i < n; i++){
    464. cin >> m >> num;
    465. if(m == 1){
    466. mp->Insert({num,1});
    467. }
    468. else if(m == 2){
    469. mp->Delete(num);
    470. }
    471. else if(m == 3){
    472. cout << mp->find_rank(num,mp->root) << "\n";
    473. }
    474. else if(m == 4){
    475. cout << mp->findkth(num,mp->root)->Getdata() << "\n";
    476. }
    477. else if(m == 5){
    478. cout << mp->Lfind(num)->Getdata() << "\n";
    479. }
    480. else if(m == 6){
    481. cout << mp->Rfind(num)->Getdata() << "\n";
    482. }
    483. }
    484. return 0;
    485. }

    treap版本的

    在二叉搜索树中,在某些情况下有可能会让二叉搜索树退化成一条链,为了解决这样的问题,在树的结点里加一个修正值,由树的结点的val和fix共同决定树的结构。fix值是随机的。

    treap就是tree + heap,其中key值满足二叉搜索树的性质,fix值满足堆的性质。

    插入结点操作:先从根节点开始,一步步寻找插入位置,一旦发现空结点就插入,插入之后,判断当前的结点的fix值是否满足堆的性质,如果不满足就进行旋转调整,旋转时并不破坏树的二叉搜索树的性质,旋转之后有可能上一层的结点的fix值的堆性质被破坏,所以要一步步向上判断。

    删除结点操作:先寻找删除结点的位置,找到后如果该结点没有叶结点就直接删除,如果该结点有一个叶结点就用叶结点去替代。如果有两个叶结点就进行适当的旋转,直到该结点有一个叶结点或没有叶结点。

    完整代码:指针实现版本

    1. #include <bits/stdc++.h>
    2. using namespace std;
    3. class Treap{
    4. private:
    5. class Node{
    6. private:
    7. Node* Lt;
    8. Node* Rt;
    9. int val;
    10. int fix;
    11. int size;
    12. public:
    13. Node(int val,int fix):Lt(nullptr),Rt(nullptr),val(val),fix(fix),size(1){}
    14. Node*& GetLt(){ return Lt;}
    15. Node*& GetRt(){ return Rt;}
    16. void SetLt(Node* t){ Lt = t;}
    17. void SetRt(Node* t){ Rt = t;}
    18. int Getval(){ return val;}
    19. int Getfix(){ return fix;}
    20. int& Getsize(){ return size;}
    21. };
    22. Node*& Lt(Node* node){
    23. return node->GetLt();
    24. }
    25. Node*& Rt(Node* node){
    26. return node->GetRt();
    27. }
    28. int Getsize(Node*& p){
    29. if(p == nullptr) return 0;
    30. else return p->Getsize();
    31. }
    32. void update(Node*& p){
    33. p->Getsize() = Getsize(p->GetLt()) + Getsize(p->GetRt()) + 1;
    34. }
    35. void LRotate(Node*& p){
    36. Node* tmp = p->GetRt();
    37. p->SetRt(tmp->GetLt());
    38. tmp->SetLt(p);
    39. update(p);
    40. p = tmp;
    41. update(p);
    42. }
    43. void RRotate(Node*& p){
    44. Node* tmp = p->GetLt();
    45. p->SetLt(tmp->GetRt());
    46. tmp->SetRt(p);
    47. update(p);
    48. p = tmp;
    49. update(p);
    50. }
    51. public:
    52. Treap():root(nullptr){}
    53. Node* root;
    54. void Insert(Node*& p,int num){
    55. if(p == nullptr){
    56. p = new Node(num,rand());
    57. return;
    58. }
    59. if(num < p->Getval()){
    60. Insert(p->GetLt(),num);
    61. update(p);
    62. if(p->GetLt()->Getfix() < p->Getfix()) RRotate(p);
    63. }
    64. else{
    65. Insert(p->GetRt(),num);
    66. update(p);
    67. if(p->GetRt()->Getfix() < p->Getfix()) LRotate(p);
    68. }
    69. }
    70. void Delete(Node*& p,int num){
    71. if(p == nullptr) return;
    72. if(p->Getval() == num){
    73. if(p->GetLt() != nullptr && p->GetRt() != nullptr){
    74. if(p->GetLt()->Getfix() < p->GetRt()->Getfix()){
    75. RRotate(p);
    76. Delete(p->GetRt(),num);
    77. }
    78. else{
    79. LRotate(p);
    80. Delete(p->GetLt(),num);
    81. }
    82. update(p);
    83. }
    84. else{
    85. if(p->GetLt() == nullptr && p->GetRt() == nullptr){
    86. p = nullptr;
    87. }
    88. else{
    89. if(p->GetLt()) p = p->GetLt();
    90. else p = p->GetRt();
    91. }
    92. }
    93. }
    94. else{
    95. if(num < p->Getval()) Delete(p->GetLt(),num);
    96. else Delete(p->GetRt(),num);
    97. update(p);
    98. }
    99. }
    100. int GetRank(Node *&p,int num)//找到x的排名,=找有多少个元素小于x
    101. {
    102. if(p == nullptr) return 0;//空节点不计数
    103. if(p->Getval() >= num) return GetRank(p->GetLt(), num);//去左子树算排名
    104. int Lsize = 0;
    105. if(p->GetLt() != nullptr) Lsize = p->GetLt()->Getsize();
    106. return Lsize + GetRank(p->GetRt(), num) + 1;//返回排名
    107. }
    108. int Getkth(Node *&p,int k)//找排名为k的元素
    109. {
    110. int Lsize = 0 ;
    111. if(p->GetLt()) Lsize = p->GetLt()->Getsize();//左子树大小
    112. if(k == Lsize + 1) return p->Getval();//找到返回
    113. if(k <= Lsize ) return Getkth(p->GetLt(),k);//是其左子树的第k名
    114. else return Getkth(p->GetRt(),k-Lsize-1);//是其右子树的k-lsize-1名
    115. }
    116. int Getpre(Node* p,int num){
    117. if(p == nullptr) return 0;
    118. if(p->Getval() >= num) return Getpre(p->GetLt(),num);
    119. int tmp = Getpre(p->GetRt(),num);
    120. if(tmp != 0) return tmp;
    121. else return p->Getval();
    122. }
    123. int Getsuc(Node* p,int num){
    124. if(p == nullptr) return 0;
    125. if(p->Getval() <= num) return Getsuc(p->GetRt(),num);
    126. int tmp = Getsuc(p->GetLt(),num);
    127. if(tmp != 0) return tmp;
    128. else return p->Getval();
    129. }
    130. };
    131. int main()
    132. {
    133. ios::sync_with_stdio(false);
    134. cin.tie(0),cout.tie(0);
    135. Treap* t = new Treap;
    136. int n,m,opt;
    137. cin >> n;
    138. for(int i = 0;i < n; i++){
    139. cin >> m >> opt;
    140. if(m == 1){
    141. t->Insert(t->root,opt);
    142. }
    143. else if(m == 2){
    144. t->Delete(t->root,opt);
    145. }
    146. else if(m == 3){
    147. cout << t->GetRank(t->root,opt)+1 << '\n';
    148. }
    149. else if(m == 4){
    150. cout << t->Getkth(t->root,opt) << '\n';
    151. }
    152. else if(m == 5){
    153. cout << t->Getpre(t->root,opt) << '\n';
    154. }
    155. else if(m == 6){
    156. cout << t->Getsuc(t->root,opt) << '\n';
    157. }
    158. }
    159. return 0;
    160. }

  • 相关阅读:
    node12-node的 get请求
    C语言学习笔记02
    2024年度“阳江市惠民保”正式发布!阳江市专属补充医疗保险全新升级
    3基于MATLAB的齿轮啮合仿真,可根据需要调节齿轮参数,实现齿轮啮合转动动态过程。程序已调通,可直接运行。
    【OpenCV】-图像的矩
    5分钟理解什么是卷积的特征提取
    在很多公司里面会使用打tag的方式保留版本
    《Python入门到精通》time模块详解,Python time标准库,time库函数大全
    [附源码]计算机毕业设计JAVA电影影评网
    应用OPC解决方案实现控制系统数据的安全交换
  • 原文地址:https://blog.csdn.net/DaoCaoRen___/article/details/124873582