The rotation of the red and Black tree (C language)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.