Red and black tree Insert and delete nodes from beginning to end full demo

Source: Internet
Author: User

Red and black tree Insert and Delete nodes full demo


July, Saturnman.
Date: March 28, 2011.
Source:Http://blog.csdn.net/v_JULY_v.
Disclaimer: All rights reserved, infringement must be traced.
-----------------------------------

Introduction:

At present, the domestic book market, or online explanation of the red and black tree data level is not clear, confusion, not a complete and unified exposition. And I red black Tree series four articles (see the article at the end of the reference), although from the beginning to the substantiated, the level of clarity, and then the reader to really do the red and black trees in the chest, but also the shortcomings of what.

And we know, even in the classic algorithm introduction of the book, did not put all the insertion, deletion of all the way, directly led to a lot of readers of the confusion, and my red-Black Tree Series 4th article: one Step map a code, it must let you really thoroughly understand the red and black trees , although already put all the insertion, The deletion of the situation is all the way, but also missing something.

What are the disadvantages? Right, what is missing is a complete, all insert, delete all the process of the entire demonstration, that is, missing an example, missing a full picture to explain all of it from beginning to end.

OK, this article, that is, 40 pictures to demonstrate all the insertions of this red and black tree, and delete the situation. I'm sure it will help you understand the red and black trees.

Words do not verses, below, this article to this article: a step by piece code, must let you really thoroughly understand the red and black tree as the outline, from inserting a node to the end of inserting all the nodes, and then to a later one to all the situation of the deletion of the node explained.

In order to have a complete unification, the red and black tree inserts and deletes the situation in this cooperation into an article. At the same time, because of my red and Black Tree series of four articles have been the red and black tree Insert, delete the situation are detailed elaborated, so, about the principle of red and black tree, this article no longer repeat, only focus on the whole process of using a diagram to demonstrate the insertion and deletion of nodes. If you have any questions, please correct me.


Red and black tree Insert situation Full process Demo

Through the 4th article of my red and Black Tree series, we already know that all insertions of red and black trees have the following five kinds:

Scenario 1: The new node n is located on the root of the tree without a parent node
Scenario 2: The parent node of the new node P is black
Scenario 3: parent node P, Uncle node U, all red,
[corresponding to the second article, the case 1:Z's uncle is red. ]
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.
[corresponds to my second article in case 2:Z's uncle is black, and z is the right child]
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.
[corresponding to my second article, Case 3:Z's uncle is black, and Z is the left child. ]

Detailed, can refer to this red Black Tree series 4th article: one step diagram a code, must let you really thoroughly understand the red black tree .

First of all, each node is inserted with the above various insertion cases, one by one,

The following 20 images are inserted into these nodes in turn: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 The full presentation, all of the 5 insert cases have been covered:

One by one of the red and black trees insert each node: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 The full presentation is finished.

Red and black tree Delete the whole process of the demonstration
all deletions of red and black trees
are as follows:

Case 1: N is the new root.
Case 2: sibling node S is red
[corresponds to my second article, Case 1: The Brother W of the x is red. ]
case 3: brother node S is black, and S's two sons are black. But N's parent node p, is black.
[corresponds to my second article, Case 2: X's brother W is black, and brother W's two sons are black.
(here, the parent of N is black)]
case 4: brother node S is black, S's son is also black, but N's father P, is red.
[ still corresponds to 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, 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.
[corresponds to my second article, Case 3: X's brother W is Black, W's left child is red, W's right child is black. ]
case 6: brother node S is black, S's right son is red, and N is its father's left son.
[corresponds to my second article, Case 4: X's brother W is black, and W's right child is red. ]

Next, it is one by one to delete these points 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 For example, the red and black tree deletion scenario is demonstrated throughout:

Each node is deleted with the above six cases, 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, the resulting red and black trees are:

Then, the following 20 pictures, is one by one delete these nodes 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 The entire presentation of the deletion situation:

Red black tree One by one delete each node: 12 1 9 2 0 11 7 19 4 15 18 5 14 13 10 16 6 3 8 17 The full presentation is finished.

References, one of my masterpiece: Red Black Tree Series:

1. Teach you a thorough understanding of red and black trees
2. Implementation and analysis of red and black tree algorithm
3. Realization and analysis of C source code of red and black tree
4. One step, one map and one code, R-b Tree
5, red black tree insertion and deletion of the full presentation of the node
6, Red black tree C + + complete realization of the source code
7, http://saturnman.blog.163.com/.

Complete the full text.

Red and black tree Insert and delete nodes from beginning to end full demo

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.