One step at a code, must let you really thoroughly understand the red and black tree "turn"

Source: Internet
Author: User

Transferred from: http://blog.csdn.net/chenhuajie123/article/details/11951777

One- step diagram One code , make sure you really and completely understand Red Black Tree

July January 2011 Nine Day

-----------------------------

This article refers to:
I, the Art of computer programming Volume I
II, Introduction to algorithms, Second Edition
III, the annotated STL Sources
IV, Wikipedia
V, algorithms in C third Edition

VI, I wrote about the red and black trees in the first three articles:

First: teach you a thorough understanding of the red and black trees:
Http://blog.csdn.net/v_JULY_v/archive/2010/12/29/6105630.aspx
The second chapter: the analysis and progressive realization of the red and black tree algorithm layer
Http://blog.csdn.net/v_JULY_v/archive/2010/12/31/6109153.aspx
The third: teach you to fully realize the red and black tree: Red and black tree C source realization and analysis
Http://blog.csdn.net/v_JULY_v/archive/2011/01/03/6114226.aspx

---------------------------------------------
Objective:
1, have the reader response, said to read my previous articles, on the red and black tree understanding is not thorough.
2, personally, I think, if I step-by-step, with the figure + code to explain the various insertions, deletions, may be more intuitive to understand.
3, since wrote Red black tree, then I must make it really write well, let the reader really thoroughly understand red black tree.

This article is relative to my front red black tree related 3 articles, mainly has the following several improvements:
1. Diagram, text narration, code writing, corresponding to each other, clear and clear.
2. Macro summary, the nature of the red and black trees and the insertion, deletion of the understanding of the situation.
3. The code to the more direct, combined diagram, give you the most intuitive feeling, thoroughly understand the red and black trees.

OK, first of all, here are a few things you should be clear about now:
I, five properties of red and black trees:
1) Each node is either red or black.
2) The root node is black.
3) Each leaf node, or null node (NIL), is black.
4) If a junction is red, then both of its sons are black.
5) For each node, the same number of black nodes are included on all paths from the node to its descendants node.

II. Several cases of red-black tree insertion:
The situation 1,z Uncle Y is red.
Situation 2:z Uncle Y is black, and z is the right child
The situation 3:z Uncle Y is black and z is the left child

III. Several cases of red and black tree deletion.
The situation 1:x's brother W is red.
The case of 2:x's brother W is black, and the two children of W are black.
The case of 3:x's brother W is black, and W's left child is red, W's right child is black.
The case of 4:x's brother W is black, and W's right child is red.

In addition, it is important to be clear:
IV, we know that the red and black trees after inserting, or deleting nodes,
May violate, or destroy, the original nature of the red and black trees,
So in order for the tree to be inserted, or deleted, to remain a new red-black tree,
That's going to be a two-way job:
1. Color of some nodes, recolor
2, adjust the direction of part of the pointer, that is, left and right rotation.

V, and distinguish between the following two operations:
1) Red-black tree Insert, delete node operation, Rb-insert (T, z), Rb-delete (T, z)
2). After the red and black tree has been inserted and deleted, the node
In order to keep the red black tree original red and black nature and do to restore and maintain the red and black nature of the operation.
such as Rb-insert-fixup (T, z), Rb-delete-fixup (t, x)

Above these 5 points, I already in front of me 2 articles, have elaborated many times, hoped, you now already thorough clear.

---------------------------------------------------------------------

In this paper, we focus on the analysis of the red and black tree insertion and deletion of the node to maintain the red and black properties of the various cases of repair work.
[The following various insertions, deletions, and my second article, Red and Black tree algorithm implementation and analysis of the corresponding]

OK, start.
One, in the following analysis, we agreed:
The node to be inserted is, N
Father node, P
Grandfather node, G
Uncle Node, U
Brother node, S

As shown, find a node for the grandfather and Uncle nodes:
Node grandparent (node N)//grandfather

{
Return n->parent->parent;
}

Node uncle (node N)//uncle

{
if (n->parent = = Grandparent (n)->left)
return grandparent (N)->right;
Else
return grandparent (N)->left;
}

Two, red and black tree insertion of several cases
Scenario 1: The new node n is located on the root of the tree without a parent node
void Insert_case1 (node N) {
if (n->parent = = NULL)
N->color = BLACK;
Else
INSERT_CASE2 (n);
}

Scenario 2: The parent node of the new node P is black
void Insert_case2 (node N) {
if (N->parent->color = = BLACK)
Return /* Tree still valid */
Else
INSERT_CASE3 (n);
}


Scenario 3: Parent node P, Uncle node U, all red,
[corresponding to the second article, the case of 1:z's uncle is red.] ]
void Insert_case3 (node N) {
if (uncle (n) = NULL && uncle (n)->color = = RED) {
N->parent->color = BLACK;
Uncle (n)->color = BLACK;
Grandparent (n)->color = RED;
Insert_case1 (grandparent (n)); Since the grandfather node may be red, violate the nature of 4, recursive scenario 1.
}
Else
INSERT_CASE4 (n); Otherwise, uncle is black, go to the following situation 4 treatment.

At this point, the new insertion node n as the left child node of P or right child node belongs to the above case 3, only the case of N as P left dial hand is shown.

Case 4: Parent node P is red, Uncle node U is black or nil;
Insert node n is the right child of its parent node p , and the parent node p is the left child of its parent node.
[corresponding to my second article, the case of 2:z's uncle is black, and z is the right child]
void Insert_case4 (node N) {
if (n = = N->parent->right && N->parent = = Grandparent (n)->left) {
Rotate_left (n->parent);
n = n->left;
} else if (n = = N->parent->left && N->parent = = Grandparent (n)->right) {
Rotate_right (n->parent);
n = n->right;
}
INSERT_CASE5 (n); Go to the following scenario 5 processing.

Scenario 5: Parent node P is red, and the Uncle node U is black or nil,
The node N to be inserted is the left child of its parent node , and the parent node p is the left child of its parent G.
[Correspondence to my second article, the situation 3:z's uncle is black, and Z is the left child. ]
void Insert_case5 (node N) {
N->parent->color = BLACK;
Grandparent (n)->color = RED;
if (n = = N->parent->left && N->parent = = Grandparent (n)->left) {
Rotate_right (grandparent (n));
} else {
/* Counter-case, N is the right child of its parent node, and parent p is the right child of its parent G */
Rotate_left (grandparent (n));
}
}

Three, red and black tree deletion of several cases
As we agreed above, the sibling node is set to S, and we use the following function to find the sibling node:
struct node * Sibling (struct node *n)//Find Brother node
{
if (n = = n->parent->left)
Return n->parent->right;
Else
Return n->parent->left;
}

The situation 1:n is the new root.
void
Delete_case1 (struct node *n)
{
if (n->parent! = NULL)
DELETE_CASE2 (n);
}


Scenario 2: Brother node S is red
[corresponding to my second article, the case 1:x's brother W is red. ]
void Delete_case2 (struct node *n)
{
struct node *s = sibling (n);

if (S->color = = RED) {
N->parent->color = RED;
S->color = BLACK;
if (n = = n->parent->left)
Rotate_left (n->parent); L
Else
Rotate_right (n->parent);
}
DELETE_CASE3 (n);
}

Case 3: The Brother node S is black, and the two sons of S are black. But N's parent node p, is black.
[corresponding to my second article, the case 2:x's brother W is black, and the two sons of Brother W are black.
(Here, parent node P is black)]
void Delete_case3 (struct node *n)
{
struct node *s = sibling (n);

if ((N->parent->color = = BLACK) &&
(S->color = BLACK) &&
(S->left->color = BLACK) &&
(S->right->color = = BLACK)) {
S->color = RED;
Delete_case1 (n->parent);
} else
DELETE_CASE4 (n);
}

Scenario 4:Brother NodeS is black, S's son is also black, but N's father P, is red.
[or to my second article, the case 2:x's brother W is black, and the two children of W are black.
(Here, parent node P is red)]
void Delete_case4 (struct node *n)
{
struct node *s = sibling (n);

if ((N->parent->color = = RED) &&
(S->color = BLACK) &&
(S->left->color = BLACK) &&
(S->right->color = = BLACK)) {
S->color = RED;
N->parent->color = BLACK;
} else
DELETE_CASE5 (n);
}

Case 5: Brother S is black, S's left son is red, S's right son is black, and N is its father's left son.
In this case, the final conversion to the following scenario 6.
[corresponding to my second article, the case 3:x's brother W is Black, W's left child is red, W's right child is black. ]
void Delete_case5 (struct node *n)
{
struct node *s = sibling (n);

if (S->color = = BLACK)
if ((n = = n->parent->left) &&
(S->right->color = BLACK) &&
(S->left->color = = RED)) {
This last test was trivial too due to cases 2-4.
S->color = RED;
S->left->color = BLACK;
Rotate_right (s);
} else if ((n = = n->parent->right) &&
(S->left->color = BLACK) &&
(S->right->color = = RED)) {
This last test was trivial too due to cases 2-4.
S->color = RED;
S->right->color = BLACK;
Rotate_left (s);
}
}
DELETE_CASE6 (n); Go to Condition 6.

Case 6: The Brother node S is black, S's right son is red, and N is its father's left son.
[corresponding to my second article, the case 4:x's brother W is black, and W's right child is red. ]
void Delete_case6 (struct node *n)
{
struct node *s = sibling (n);

S->color = n->parent->color;
N->parent->color = BLACK;

if (n = = n->parent->left) {
S->right->color = BLACK;
Rotate_left (n->parent);
} else {
S->left->color = BLACK;
Rotate_right (n->parent);
}
}

Hehe, draw these 12 pictures, draw directly from noon to the evening. Hopefully, this article will make you understand.

Analysis of time complexity of inserting and deleting red and black trees
Because each red and black tree is also a special two-fork search tree,
So the read-only operation on the red-black tree is the same as the read-only operation on the normal binary lookup tree.
However, inserting and deleting on a red-black tree will result in no longer conforming to the nature of the red-black tree.

Restoring the properties of a red-black tree requires a small amount (O (log n)) of color changes (actually very fast) and
No more than three tree rotations (two times for insert operations).
While insertions and deletions are complex, the operation time can still be maintained at O (log n) times.


OK, finish.

Postscript:
This red and Black Tree series, and the previous, has written 4 articles, if the reader finished reading these 4 articles,
There is a relatively thorough understanding of the red-black tree relative to the previous,
Well, it's not a waste, I spent so much space and spent hours painting red and black trees.

Really understand a data structure, algorithms, the most urgent or real to use, the practice of experience.
Welcome, everyone, will now, or later study, work in the use of this red and black tree structure, algorithm experience and I share.
Thank you. :D.
----------------------------------------

Author Statement:
I July to this blog all articles and materials enjoy copyright, reproduced, or quoted any content please indicate the source.
A tribute to your kindness. Thank you. January 9, 2011.

One step at a code, must let you really thoroughly understand the red and black tree "turn"

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.