Written algorithm questions (51): Introduction-Red and black trees (redblack tree)

Source: Internet
Author: User

Red and black Trees (red-black tree)

    • The red-black tree is a type of BST, but a storage bit on each node indicates the color of the node (R or B); The red-black tree ensures that the difference of all paths is not more than one-fold on any path from root to leaf, resulting in a nearly balanced BST;
    • Each node in the red-black tree contains five attributes: Color, key, left, right, and p,p represent pointers to the Father node; a BST needs to meet the following five properties in order to be called a red-black tree:

      Each node can only be one of the red or black nodes;

      The root node must be black;

      Each leaf node (NULL) must be black;

      If a node is red, then its two child nodes must be black;

      For any node in the tree, the node has the same number of black nodes on all paths to its leaf node

    • The spatial complexity of the red-black tree is O (N); supports three operations: search, INSERT, delete, and all operations have a time complexity of O (Logn), preferably with the worst case complexity. For a search operation, it relies on the properties of BST, so it does not need to rely on the coloring information of the nodes, and the insert and delete operations may damage the balance of BST, so the two operations need to adjust the coloring information of the nodes in the red and black tree.
1 //in a left-hand operation, the right child node of the oldroot becomes the right child node of the new Root,root and the left child node of the oldroot.2 //Oldroot becomes the left child node of the new root3Template <classKeytype>4    voidNode<keytype>::rotateleft (node<keytype> * &root) {5node<keytype> * Oldroot =Root;6root = root->Mysubtree[right];7Oldroot->mysubtree[right] = root->Mysubtree[left];8Root->mysubtree[left] =Oldroot;9    }Ten  One //in the right-hand operation, the left child node of Oldroot becomes the right child node of the new Root,root and becomes the left child node of Oldroot. A //Oldroot becomes the right child node of the new root -Template <classKeytype> -    voidNode<keytype>::rotateright (node<keytype> * &root) { thenode<keytype> * Oldroot =Root; -root = root->Mysubtree[left]; -Oldroot->mysubtree[left] = root->Mysubtree[right]; -Root->mysubtree[right] =Oldroot; +    } -  + //inserts a new node z into the red and black tree of the T index, uses the properties of BST to find the insertion position of z, and the new node Z-label A //note is red; atrb-INSERT (T, z) - Y←nil[t] - X←root[t] -     whileX≠nil[t] -        Doy←x -       ifKEY[Z] <Key[x] in Then X←left[x] -          ElseX←right[x] to P[z]←y +    ify =Nil[t] - Then root[t]←z the       Else ifKEY[Z] <Key[y] * Then left[y]←z $          Elseright[y]←zPanax Notoginseng Left[z]←nil[t] - Right[z]←nil[t] the color[z]←red +Rb-insert-fixup (T, z)

Inserting a node and labeling it as red may damage the properties of the red-black Tree 2 and properties 4, when the insertion node is the root node of the destruction of the nature of 2, at this time directly turn it into black can be restored, when the destruction of the nature of 4 requires a series of recovery operations;
case1: The original tree is empty, the new node is the root node, and the recovery strategy is to change it to black;
Case2: The parent node of the new node is black, and all red and black tree rules are met;
CASE3: The parent node of the new node is red, the sibling node of the parent node is red, and the recovery strategy is to change the parent and parent nodes of the new node to black, the grandparent node to red, and the method for the grandfather node to recall;
Case4: The parent node of the new node is red, the sibling node of the parent node is black, the new node is the right child of the parent node, and the recovery strategy is to pivot the parent node of the new node;
CASE5: The parent node of the new node is red, the sibling node of the parent node is black, the new node is the parent node of the left dial hand, and the recovery strategy is to change the parent node of the new node to black, the grandfather node to red, and the grandfather node to the right rotation;

1rb-insert-fixup (T, z)2     whileCOLOR[P[Z] =RED3        Do ifP[Z] =Left[p[p[z]]4 Then Y←right[p[p[z]]5             ifColor[y] =RED6Then Color[p[z]←black? Case37Color[y]←black? Case38Color[p[p[z]]←red? Case39Z←P[P[Z]? Case3Ten                Else ifz =Right[p[z] OneThen Z←p[z]? Case4 ALeft-rotate (T, z)? Case4 -Color[p[z]←black? Case5 -Color[p[p[z]]←red? Case5 theRight-rotate (T, p[p[z])? Case5 -          Else(Same asThen clause with" Right"and" Left"exchanged) - Color[root[t]←black -  + // -rb-DELETE (T, z) +    ifLEFT[Z] = nil[t] or right[z] =Nil[t] A Then y←z at       Elsey←tree-successor (z) -    ifLeft[y]≠nil[t] - Then X←left[y] -       ElseX←right[y] - P[x]←p[y] -    ifP[y] =Nil[t] in Then root[t]←x -       Else ify =Left[p[y] to Then left[p[y]←x +          Elseright[p[y]←x -    ifY3≠z the Then Key[z]←key[y] *Copy y's satellite data into Z $    ifColor[y] =BLACKPanax NotoginsengThen rb-delete-fixup (T, x) -    returnY

case1:X's brother W is red
Case2:the brother W of X is black, and two children of W are black
case3: The brother W of X is black, and the left child of W is Red, W's right child is black
Case4:the brother W of X is black, and the right child of W is red

1rb-delete-fixup (T, x)2     whileX≠root[t] and color[x] =BLACK3        Do ifx =Left[p[x]4 Then W←right[p[x]5             ifCOLOR[W] =RED6Then Color[w]←black? Case17Color[p[x]←red? Case18Left-rotate (T, p[x])? Case19W←RIGHT[P[X]? Case1Ten             ifCOLOR[LEFT[W] = BLACK and color[right[w] =BLACK OneThen color[w]←red? Case2 AX←P[X]? Case2 -                Else ifCOLOR[RIGHT[W] =BLACK -Then Color[left[w]←black? Case3 theColor[w]←red? Case3 -Right-rotate (T, W)? Case3 -W←RIGHT[P[X]? Case3 -COLOR[W]←COLOR[P[X]? Case4 +Color[p[x]←black? Case4 -Color[right[w]←black? Case4 +Left-rotate (T, p[x])? Case4 AX←ROOT[T]? Case4 at          Else(Same asThen clause with" Right"and" Left"exchanged) -Color[x]←black

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.