zoukankan      html  css  js  c++  java
  • 红黑树的旋转(C语言)

    1.红黑树为什么要执行旋转操作

    原因:红黑树在执行Insert和Delete对二叉搜索树进行操作时,结果可能会违反红黑树的性质,需要改变树中某些结点的颜色和指针结构。

    指针结构的修改:通过左旋、右旋来改变的。

    特点:旋转操作保持二叉搜索树性质的局部性操作。

    2.算法代码

     

    
    
     1 //左旋
     2 static void rbTreeLeftRotate(RBTree *rbTree, RBTreeNode *x)
     3 {
     4     RBTreeNode *y;
     5 
     6     y = x->right;        //设置一个指针型变量来存放x右孩子的地址,
     7     x->right = y->left;    //此处将x的右孩子替换成y的左孩子
     8 
     9     if (y->left !=NULL)
    10         y->left->parent = x;
    11     
    12     y->parent = x->parent;
    13 
    14     if (x->parent == NULL)
    15         *rbTree = y;
    16     else 
    17     {
    18     if (x->parent->left == x)
    19         x->parent->left = y;
    20     else
    21         x->parent->right = y;    
    22     }
    23     y->left = x;
    24     x->parent  = y;
    25 }
     1 //右旋
     2 static void rbTreeRightRotate(RBTree *rbTree, RBTreeNode *x)
     3 {
     4     RBTreeNode *y;
     5 
     6     y = x->left;
     7     x->left = y->right;
     8 
     9     if (y->right !=NULL)
    10         y->right->parent  = x;    //link  y->right to x->left
    11 
    12     y->parent = x->parent ;        
    13     if (x->parent == NULL)        //link  x->parent to y
    14         *rbTree = y;
    15     else
    16         {
    17         if (x->parent->left = x)
    18             x->parent->left = y;
    19         else20           x->parent->right = y;
    21         }
    22     y->right = x;
    23     x->parent = y;
    24 
    25 }
  • 相关阅读:
    线性dp打鼹鼠
    区间dp(能量项链)
    留言板
    友链
    CodeForces 1270I Xor on Figures
    agc020_d Min Max Repetition
    Codeforces 1270G Subset with Zero Sum
    CodeForecs 1270E Divide Points
    MybatisPlus学习笔记10:公共字段自动填充
    MybatisPlus学习笔记9:自定义全局操作
  • 原文地址:https://www.cnblogs.com/super90/p/4067210.html
Copyright © 2011-2022 走看看