Step by step, Figure 1 shows the code, which must make you fully understand the red and black trees.

Source: Internet
Author: User

Step by step, Figure 1 shows the code, which must make you fully understand the red and black trees.

 

Author: July January 9, 2011

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

Reference:
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. My first three articles on the red and black trees:

Article 1: teach you a thorough understanding of the red and black trees:
Http://blog.csdn.net/v_JULY_v/archive/2010/12/29/6105630.aspx
Article 2: layer-by-layer analysis and gradual implementation of the Red-black Tree Algorithm
Http://blog.csdn.net/v_JULY_v/archive/2010/12/31/6109153.aspx
Article 3: How to thoroughly implement the red/black tree: how to implement and analyze the c source code of the red/black tree
Http://blog.csdn.net/v_JULY_v/archive/2011/01/03/6114226.aspx

---------------------------------------------
Preface:
1. Some readers have responded that I have read my previous articles and I still have a thorough understanding of the red and black trees.
2. I personally think that, if I use diagrams + code to describe various insertion and deletion situations step by step, they may be more intuitive and easy to understand.
3. Since I have written a red/black tree, I must write it well to make readers fully understand it.

Compared with the three articles related to the red and black trees, this article mainly provides the following improvements:
1. Maps graphs, text descriptions, and code writing, which are clear and clear.
2. Macro Summary: understanding of the nature and insertion and deletion of the red and black trees.
3. The code is more direct. It combines graphs to give you the most intuitive feeling and thoroughly understand the red and black trees.

OK. First of all, you should be clear about the following points:
I. Five PROPERTIES OF THE RED/black tree:
1) each node is either red or black.
2) The root node is black.
3) each leaf node, that is, the null node (NIL) is black.
4) if a node is red, the two sons are black.
5) for each node, all paths from the node to its child node contain the same number of black nodes.

II. Situations of red/Black Tree insertion:
Case 1: Uncle y of z is red.
Case 2: Uncle y of z is black and z is the right child
Case 3: Uncle y of z is black and z is left child

III. Deletion of red/black trees.
Case 1: x's brother w is red.
Case 2: Brother w of x is black, and both children of w are black.
Case 3: x's brother w is black, w's left child is red, and w's right child is black.
Case 4: x's brother w is black and w's right child is red.

In addition, it must be clear that:
IV. We know that after a red/black tree inserts or deletes a node,
It may violate or damage the original property of the red/black tree,
Therefore, in order to make the tree after the node is inserted or deleted remain a new red/black tree,
We need to do the following:
1. Partial node color, re-coloring
2. Adjust the direction of some pointers, that is, left and right.

V:
1) red and black tree insert, delete node operation, RB-INSERT (T, z), RB-DELETE (T, z)
2) After the red/black tree has been inserted or deleted,
In order to maintain the original red and black properties of the red and black trees, the restoration and maintenance of the red and black properties are performed.
Such as RB-INSERT-FIXUP (T, z), RB-DELETE-FIXUP (T, x)

I have already elaborated on the above five points in my previous two articles many times. I hope that you have understood them thoroughly.

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

This article focuses on Graphic analysis of various situations in which the Red/black tree inserts and deletes nodes to maintain the red/black nature.
[The insertion and deletion situations below correspond to the implementation and analysis of the Red-black tree algorithm in my second article.]

OK.
1. In the following analysis, we agree:
The node to be inserted is, N
Father's Day, P
Grandfather node, G
Uncle node, U
Sibling node, S

As shown in, find the grandfather and Uncle nodes of a node:
Node grandparent (node n) // grandfather

{
Return n-> parent;
}
 
Node uncle (node n) // uncle

{
If (n-> parent = grandparent (n)-> left)
Return grandparent (n)-> right;
Else
Return grandparent (n)-> left;
}

 

Ii. Situations of red/Black Tree Insertion
Scenario 1: The new node N is located on the root of the tree and has no parent node.
Void insert_case1 (node n ){
If (n-> parent = NULL)
N-> color = BLACK;
Else
Insert_case2 (n );
}

Case 2: The parent node P of the new node is black.
Void insert_case2 (node n ){
If (n-> parent-> color = BLACK)
Return;/* the tree is still valid */
Else
Insert_case3 (n );
}

 
Case 3: the parent node P and Uncle node U are all red,
[In the second article, 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); // because the grandfather node may be red, it violates Nature 4, recursion 1.
}
Else
Insert_case4 (n); // otherwise, Uncle should be black and go to the following scenario 4 for handling.

At this time, the newly inserted node N is the Left or Right subnode of P.Case 3Only show N as the left sub of P.

 

Case 4: the parent node P is red, and the uncle node U is black or NIL;
Insert NodeN is the right child of parent node P.The parent node P is the left child of the parent node.
[For my second article, the Case 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 for processing.

 

Case 5: The parent node P is red, while the uncle node U is black or NIL,
Node TO BE INSERTEDN is the left child of its parent node.The parent node P is the left child of its parent G.
[For my second article, Case 3: z's uncle is black and z is a 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 {
/* Reversed. N is the right child of its parent node, and P is the right child of its parent G */
Rotate_left (grandparent (n ));
}
}

 

 

Iii. Deletion of red/black trees
As agreed above, if the sibling node is set to S, we use the following function to find the sibling node:
Struct node * sibling (struct node * n) // find the sibling node
{
If (n = n-> parent-> left)
Return n-> parent-> right;
Else
Return n-> parent-> left;
}

Case 1: N is the new root.
Void
Delete_case1 (struct node * n)
{
If (n-> parent! = NULL)
Delete_case2 (n );
}

 
Case 2: Brother node S is red
[In 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); // left-hand
Else
Rotate_right (n-> parent );
}
Delete_case3 (n );
}

 

Case 3: Brother node S is black, and both sons of S are black. But N's parent node P is black.
[In my second article, the Case 2: The brother w of x is black, and both sons of brother w are black.
(Here, the 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 );
}

 

Case 4:Sibling NodeS is black, and S's son is black, but N's father P is red.
[In my second article, the Case 2: The brother w of x is black, and the two children of w are black.
(Here, the 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, his left son is red, his right son is black, and N is his father's left son.
// In this case, it is finally converted to the following situation 6.
[Corresponding to my second article, Case 3: x's brother w is black, w's left child is red, and 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 is 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 is trivial too due to cases 2-4.
S-> color = RED;
S-> right-> color = BLACK;
Rotate_left (s );
}
}
Delete_case6 (n); // go to scenario 6.

 

Case 6: Brother node S is black, S's right son is red, and N is his father's left son.
[In 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 );
}
}

// You can draw the 12 images from noon to evening. Hopefully, this article will help you understand.

 

Iv. Analysis of the time complexity of insertion and deletion of red/black trees
Because every red/black tree is also a special binary search tree,
Therefore, the read-only operation on the red/black tree is the same as that on the common Binary Search Tree.
However, the insert and delete operations on the red/black tree will no longer conform to the nature of the red/black tree.

Restoring the properties of the red/black tree requires a small amount of (O (log n) color changes (actually very fast) and
No more than three tree rotations (two insert operations ).
Although insertion and deletion are complex, the operation time can still be O (log n) times.


OK.

Postscript:
This red/black tree series has written four articles before and after. If the readers have read these four articles,
I have a thorough understanding of the red and black trees,
I will spend so much space and hours painting the red and black trees.

To truly understand a Data Structure and algorithm, the most important thing is to understand what is actually available and practical.
Thank you for sharing your experience in using the red-black tree structure and Algorithms in your current or future studies and work.
Thank you. : D.
----------------------------------------

 

Author's statement:
I have copyright to all the articles and materials in this blog. Please indicate the source for reprinting or referencing any content.
To pay tribute to you. Thank you. May January 9, 2011.

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.