Full presentation of nodes inserted and deleted from the beginning to the end of the red/black tree

Source: Internet
Author: User

Full demonstration of red/Black Tree insertion and deletion of nodes

Author: July and saturnman.
Time: January 1, March 28, 2011.
Source:Http://blog.csdn.net/v_JULY_v.
Disclaimer: All Rights Reserved. infringement is required.
-----------------------------------

Introduction:

At present, there is no complete and unified explanation of the different levels of data on the Chinese book market or on the Internet. I have four articles in the red/black tree series.Article(For details, see references at the end of this Article.) although the lecture is well-rooted and well-defined from the beginning to the end, what are the disadvantages.

We know that even in the classicAlgorithmIn the introduction book, I did not cover all the insertion and deletion situations one by one, which directly caused many readers to be confused. However, my 4th articles in the red/black tree series:Step 1 Figure 1CodeMake sure you fully understand the red and black treesAlthough we have already done everything about insertion and deletion, there is something missing.

What are the disadvantages? By the way, what is missing is a complete demonstration map that contains all the processes of insertion and deletion. That is, one example is missing, and a complete diagram is missing to illustrate all this from the beginning to the end.

OK. This article uses 40 images to demonstrate all the inserts, deletions, and deletions of the red/black tree. I believe it will certainly help you understand the red and black trees.

This article is as follows:Step by step, Figure 1 shows the code, which must make you fully understand the red and black trees.Specifically, from inserting a node to inserting all the nodes at the end, and then deleting all the nodes one by one.

In order to have a complete and unified structure, the insertion and deletion of the red and black trees were combined into an article here. At the same time, because I have elaborated on the insertion and deletion of the red and black trees in four articles in the red and black trees series, the principles of the red and black trees are as follows, this article will not go into details. It only focuses on the use of graphs to demonstrate the insertion and deletion of nodes. If you have any questions, please let me know.

Full Process of red/Black Tree Insertion

We already know from my 4th articles in the red/black tree series,There are five insertion conditions for the red/black tree:

Scenario 1:The new node N is located on the root of the tree and has no parent node.
Case 2:The parent node P of the new node is black.
Case 3:The parent node P and Uncle node u are all red,
[Corresponding to scenario 1 in the second article:Z's uncle is red.]
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.
[Corresponding to scenario 2 in my second article:Z's uncle is black and Z's right child]
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.
[Corresponds to case 3 in my second article:Z's uncle is black and Z is the left child.]

For details, refer to the 4th articles in the red/black tree series:Step by step, Figure 1 shows the code, which must make you fully understand the red and black trees..

 

First, each node insertion corresponds to the preceding insertion conditions one by one,

The following 20 images insert these nodes in sequence: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 full demonstration diagram, all the five insert cases have been involved:

One-to-one insertion of nodes in the red and black trees: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 the whole demonstration graph is complete.

 

Entire Process of red/black tree deletion
All deletion conditions of the red/black tree,As follows:

case 1: N is the new root.
case 2: the sibling node S is red.
[corresponding to my second article, Case 1: X's brother w is red.]
case 3: brother node S is black, and both sons of S are black. But n's parent node P is black.
[This corresponds to scenario 2 in my second article: brother w of X is black, and both sons of brother w are black.
(here, N's parent node P is black)]
scenario 4: the brother node S is black, and the son of S is black, but the father P of N is red.
[ in my second article, Case 2: X's brother w is black, and w's two children are black.
(here, N's parent node P is red)]
case 5: brother S is black, and his left son is red, s's right son is black, and N is his father's left son.
// in this case, it is finally converted to scenario 6 below.
[This corresponds to scenario 3 in my second article: X's brother w is black, W's left child is red, and W's right child is black.]
case 6: the brother node S is black, the right son of S is red, and N is his father's left son.
[corresponds to scenario 4 in my second article: X's brother w is black, and W's right child is red.]

Next, we will delete these points 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 as an example, that is, the whole process of the red/black tree deletion is demonstrated:

The deletion of each node corresponds to the preceding six situations one by one,

First, insert 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 nodes, and then the red and black trees are:

 

Then, the following 20 images, is a full demonstration of how to delete these nodes 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17:

The red and black trees Delete nodes one by one: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 the full demonstration graph is complete.

 

References: One of my masterpiece: red/black tree series:

1. teach you a thorough understanding of the red and black trees
2. Implementation and Analysis of the Red-black Tree Algorithm
3. Implementation and Analysis of the C source code of the red/black tree
4, step 1 figure a code, R-B tree
5. Full demonstration of red/Black Tree insertion and deletion of nodes
6. complete source code for C ++ of the red/black tree
7, Http://saturnman.blog.163.com/.

The full text is complete.

 

Copyright. Repost any article in this blog. Please use a hyperlink to indicate the source.
Otherwise, once discovered, it will be permanently condemned + held legally responsible. Thank you.

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.