Red and black Trees

Source: Internet
Author: User

The red-black tree is a self-balancing binary lookup tree that has the complexity of finding, inserting, and deleting O (log2n) In the worst case scenario. The longest path from the root node to any leaf node in a red-black tree does not exceed twice times the shortest path , so it is an approximate balanced two-fork tree.

Node Information

The nodes of the red and black tree have the following information:

struct rbnode{int data;//data int color;//color, red or black rbnode* parent;//parent node rbnode* child[2];int child_dir;// Indicates whether the current node is the left or right child node of its parent node rbnode (int d) {data = D;color = RED; parent = null;child[0] = child[1] = NULL;}};

Red black Tree Nature

Red and black trees meet the following properties:

  1. Each node is either black or red
  2. The root node of the tree is black
  3. The leaf node (nil) of the tree is black (in order to indicate that all valid leaf nodes in the red-black tree < leaf nodes that contain data > have added additional leaf nodes to nil, this means nil is black)
  4. If a node is red, its two child nodes are black
  5. For each node, the same number of black nodes are included on all paths from the node to its descendant leaf nodes

For red-black trees that meet the above conditions, the following lemma is available:

The height of a red black tree with n nodes is up to < Span id= "mathjax-span-12" class= "Mrow" >l o G2 (n+1)

Therefore the operation complexity of the red and black tree is the worst O (log2n)

the rotation of the red and black trees

Like the AVL tree, splay tree, and treap, the red-black tree realizes its balance through the rotation of the nodes (and the re-staining of the nodes). The rotation of the red and black tree has both left and right, and the rotation in the stretching tree is the same, see the rotation of the tree.

insertion and maintenance of red and black trees

The insertion of the red and black tree is the same as the insertion of the two-fork search tree, and it is found by the nature of the binary tree where the inserted node should be inserted and then inserted. However, due to the five-point nature of the red-black tree, it is necessary to perform additional maintenance operations on the red-black tree.

After insertion, it may violate the nature of 2, 4, so maintenance is required:
(1) If the node being inserted is the root node, it violates the Nature 2, so change the color of the root node directly to black .
(2) If the parent node of the node being inserted is black, no rules are violated and the direct return
(3) If the parent node of the inserted node is red, and the grandfather node (positive) is black, subdivide three cases at this time:
* (a) Another child node of the grandparent node that is inserted into the node (that is, the Uncle node that is inserted into the node) is red
* (b) The Uncle node of the inserted node is black, and the node is inserted into the right child node of its parent node
* (c) The Uncle node of the inserted node is black, and the node is inserted into the left child node of its parent node

for (3.a), the

Another child node of the grandparent node that is inserted into the node (that is, the Uncle node that is inserted into the node) is red
Changes the current node's grandfather node to red, the parent and Uncle nodes of the current node to black, and the current node to its grandparent node, recursively executes Insertfix

before

after

for (3.b), the

The uncle of the inserted node is black, and the node is inserted into the right child node of its parent node
The current node and its parent node are left-handed, the parent node is rotated below, and the current node is updated to the parent node below

before

after

for (3.C), the

The uncle of the inserted node is black, and the node is inserted into the left child node of its parent node
Parent node turns black, grandfather node turns red, grandfather node and parent node are right-handed

before

after

In this way after the rotation dyeing operation, can ensure the nature of red and black Trees 2, 4, 5 was established.

removal and maintenance of red and black trees

Red black tree deleted into the same as the deletion of the two-fork search tree, all through the nature of the binary tree to find the location of the node to be deleted, if the node to be deleted has no child nodes, then directly deleted, and empty nodes to replace the node; If the node to be deleted has only one non-empty node, replace the node for deletion If the two nodes of the node to be deleted are not empty, then the successor node of the node is found, the data of the node and its successors are exchanged, and the delete operation is called again. In this way, after calling again, the successor node of the original node is found, at which point the successor must have at most one non-empty node, so it can be solved in the case listed earlier.
However, since the red-black tree has the five-point nature requirement mentioned earlier, additional maintenance operations are required on the red-black tree, which starts at the node where the node was removed.
If the deleted node is black , after deletion, it may violate the nature of the red-black tree 5, so maintenance is required: Set the node that replaces the deleted node as the current node
(1) If the current node is red, turn the node directly into black and return to
(2) If the current node is black and is the root node, return directly
(3) If the current node is black, and not the root node, subdivided into the following four cases:
(a) The current node is black and the sibling node is red (at which point the child nodes of the parent and sibling nodes must be black)
(b) The current node is black and the sibling node is black, and the two child nodes of the sibling node are black
(c) The current node is black, and the sibling node is black, the left Dial hand node of the sibling nodes is red, the right child node is black
(d) The current node is black and the sibling node is black, the right child node of the sibling node is red, and the left Dial hand node color of the sibling nodes is arbitrary .

For scenarios (3.a)

The current node is black, and the sibling node is red (at which point the child nodes of the parent and sibling nodes must be black), the
turns the parent node red, the sibling node turns black, and then performs a left or right rotation on the parent and sibling nodes and then re-enters the algorithm.

before

after

For scenarios (3.b)

The current node is black, and the sibling node is black, and the two child nodes of the sibling node are all Black
Turns the sibling node of the current node red, and then the current node becomes its parent node

before

after

Since the deleted node is black, the number of black nodes on the current node's path is reduced by 1, not satisfying the nature 5, at which point the sibling nodes of the current node are dyed red and the current node is promoted to its parent node. At this point, the number of black nodes is the same on the path down from the current node, but the number of black nodes is still 1 less than the path from the higher node to the leaf node.

For scenarios (3.C)

The current node is black, and the sibling node is black, the left Dial hand node of the sibling nodes is red, the right child node is black
Turn the sibling nodes into red, the left dial hand nodes of the brothers become black, and then perform a right-hand operation on the sibling node and its left dial hand nodes.

before

after

For scenarios (3.D)

The current node is black, and the sibling node is black, the right sub-node of the sibling node is red, the left Dial hand node color of the sibling nodes dye the sibling nodes to the current node's parent node color, the current node parent node dyed black, Brother node right child node dyed black, Then perform a left-hand operation on the parent node of the current node

before

after

implementation (pseudo code)
    Rb-insert-fixup (T, z) while z.p.color = = RED do if z.p = = Z.p.p.left then Y←z.p.p.right if Y.color = = RED then z.p.color←black? Case 1 Y.color←black? Case 1 z.p.p.color←red? Case 1 Z←Z.P.P? Case 1 else if z = = Z.p.right then Z←Z.P? Case 2 left-rotate (T, z)? Case 2 Z.p.color←black? Case 3 z.p.p.color←red? Case 3 Right-rotate (T, Z.P.P)? Case 3 Else (same as, clause with "right" and "left" exchanged) T.root.color←black while X≠root[t] and color[x] = BLACK do if x = left[p[x] [then w←right[p[x]] if color[w] = R        ED               Then Color[w]←black?  Case 1 color[p[x]]←red?  Case 1 Left-rotate (T, p[x])?  Case 1 W←right[p[x]]?                           Case 1 if COLOR[LEFT[W]] [black and color[right[w]] = Black then color[w]←red  ?  Case 2 x←p[x]?          Case 2 else if color[right[w]] = BLACK then Color[left[w]]←black  ?  Case 3 color[w]←red?  Case 3 Right-rotate (T, W)?  Case 3 W←right[p[x]]?  Case 3 Color[w]←color[p[x]]? Case 4 COlor[p[x]]←black?  Case 4 Color[right[w]]←black?  Case 4 Left-rotate (T, p[x])?  Case 4 X←root[t]?     Case 4 Else (same as then clause with "right" and "left" exchanged) Color[x]←black


Compare with AVL

The red-black tree does not seek "complete balance"-it only requires partial balancing requirements, reducing the need for rotation, thus improving performance.
Red-black trees cansearch, insert, and delete operations with the time complexity of O (log2n). In addition, due to its design, any imbalance will be resolved within three rotations. Of course, there are some better, but more complex data structures can achieve balance within one step of rotation, but red-black trees can give us a "cheap" solution. The red-black tree algorithm has the same time complexity as AVL, but has a higher statistical performance than the AVL tree.

Reference:

Teach you a thorough understanding of the red and black trees

Red-black tree

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.