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

Source: Internet
Author: User

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://www.bkjia.com/kf/201104/87322.html

Article 2: layer-by-layer analysis and gradual implementation of the Red-black Tree Algorithm

Http://www.bkjia.com/kf/201104/87321.html

Article 3: How to thoroughly implement the red/black tree: how to implement and analyze the c source code of the red/black tree

Html> http://www.bkjia.com/kf/201104/87323.html

---------------------------------------------
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, all the Left or Right subnodes with the newly inserted node N as P belong to the above case 3, and only show that N is the left subnode of P.

 

Case 4: the parent node P is red, and the uncle node U is black or NIL;
Insert node N is the right child of its parent node P, while parent node P is the left child of its 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,
The node n to be inserted is the left child of its parent node, while 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
{
& Nbs

Related Article

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.