1. Why do the red and black trees perform rotation operations
Cause: Red and black trees when performing insert and delete operations on a two-fork search tree, the result may violate the nature of the red-black tree, changing the color and pointer structure of some nodes in the tree.
Modification of the pointer structure: changes by left and right rotation.
Features: rotation operation to keep binary search tree properties of local operations.
2. Algorithm code
1 //L -2 Static voidRbtreeleftrotate (Rbtree *rbtree, Rbtreenode *x)3 {4Rbtreenode *y;5 6y = x->right;//set a pointer-type variable to hold the X-right child's address,7X->right = y->left;//here, replace the right child of X with the left child of Y.8 9 if(Y->left! =NULL)TenY->left->parent =x; One AY->parent = x->parent; - - if(X->parent = =NULL) the*rbtree =y; - Else - { - if(X->parent->left = =x) +X->parent->left =y; - Else +X->parent->right =y; A } atY->left =x; -X->parent =y; -}
1 //right-handed2 Static voidRbtreerightrotate (Rbtree *rbtree, Rbtreenode *x)3 {4Rbtreenode *y;5 6y = x->Left ;7X->left = y->Right ;8 9 if(Y->right! =NULL)TenY->right->parent = x;//link y->right to X->left One AY->parent = x->parent; - if(x->parent = = NULL)//link x->parent to y -*rbtree =y; the Else - { - if(X->parent->left =x) -X->parent->left =y; + Else -X->parent->right =y; + } AY->right =x; atX->parent =y; - -}
The rotation of the red and Black tree (C language)