Layer-by-layer analysis and gradual implementation of the Red-black Tree Algorithm

Source: Internet
Author: User

This article mainly references: Introduction to algorithm Version 2
The main code of this article: see Introduction to algorithms.
Source of the image in this article: the original book of artificial painting and introduction to algorithms.
Recommended reading: A paper on the red and black trees written by Leo J. Guibas and Robert Sedgewick in 1978.

Introduction:

I drew red and black trees for several hours yesterday afternoon, with a total of 10 pages.
Here, we will further analyze the Algorithm Implementation of the Red-black tree and teach you how to thoroughly implement the red-black tree algorithm.

After my previous blog post "teach you a thorough understanding of the red and black trees", I believe everyone has a certain understanding of the red and black trees.
I personally think this red and black tree is easy to understand.
Whether it is insertion or deletion, or left or right, the ultimate goal is only one:
That is to say, the five properties of the red and black trees should not be violated.

Again, repeat the five properties of the red and black trees:
Generally, the red-black tree satisfies the nature, that is, only the tree that satisfies the nature is called 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.


It is much easier to grasp the five properties of the red and black trees.
For example,
1. Red/Black, either red or black;
2. The root node is black;
3. Each leaf node is black;
4. A red node, its two sons must be black;
5. The number of black nodes in each path is the same.
Five properties, in combination, are: (1) Black (2) Black (3) Black (4 & 5) Red-> black.

 

All the text in this article is drawn by reference to my workshop in the afternoon.Ten sheets of paper(That is, my photos) and introduction to algorithms.

I hope you can follow this article at to learn more about the algorithm of the red and black trees.

 

Okay. Now let's take a deeper look at the algorithm of the red/black tree and teach you how to implement this algorithm step by step.

This tutorial consists of 10 parts, each of which serves as a section. Each section corresponds to the ten photos I have given.

 

I. Left and Right

 First, make it clear: Why do we need to be left-handed?

Because the structure of the red/black tree changes after the nodes are inserted or deleted, the properties of the tree may be damaged.

In order to maintain that the tree after the insertion or deletion of the node is still a red/black tree, it is necessary to adjust the structure of the tree partially to restore the original property of the red/black tree.

The actions to restore the black and red include:

Node color change (re-coloring) and Node Adjustment.

This part of the Node Adjustment work, changing the pointer structure, that is, through the left or right hand to achieve the goal.

In this way, the inserted or deleted node tree becomes a new red/black tree.

 

OK, please refer:

As shown in, 'failed'

If you understand the differences between the two images, you will know what is left-handed or right-handed ".

 

Here, we will focus on the analysis of left-hand algorithms:

Left-hand, (left-> right), with the chain between x-> y as the "axis,

Make y the root of the new subtree, x the left child of y, and y the right child of y.

The algorithm is very simple. Note that the size of each node ranges from left to right, from left to right.

 

Implement the left-hand code in three steps (note the comments I have given ):

The pseudo code for LEFT-ROTATE assumes that right [x] =nil [T] and that the roots parent is nil [T].

LEFT-ROTATE (T, x)
1 y fill right [x] fill Set y.
2 right [x] left [y] // start to change. The left child of y becomes the right child of x.

3 if left [y]! = Nil [T]

4 then p [left [y] <-x

5 p [y] <-p [x] // y is the parent of x.
6 if p [x] = nil [T]

7 then root [T] <-y

8 else if x = left [p [x]
9 then left [p [x] ← y
10 else right [p [x] ← y
11 left [y] Then x // x becomes the left child of y (January 3)

12 p [x] ← y
// Note: This code is different from the English version of the first and second editions of the original books.

// I personally think the second version is more accurate. Therefore, this Code is subject to the Chinese version of the second edition.

 

The left and right sides are symmetric and completed in O (1) time. Because only the pointer is changed during rotation, and all the fields in the node remain unchanged.

Finally, I posted a diagram of the right-hand algorithm in the afternoon:

Left-hand (2nd images ):

// This figure has a bug. The comments of rows 4th are moved to rows 11th. As shown in the above Code. (Revised on April 9, January 3)

 

2. Left-handed instance

If you do not describe it too much, you can see the secondary graph at a glance.

Operation Procedure of LEFT-ROTATE (T, x) (3rd images ):

 

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

Reminder,Before reading the following, be sure to clarify the differences between the following two operations:

1. Insert and delete nodes in the red/black tree

// Such as insert, red and black tree insert node operation: RB-INSERT (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.

// For example, insert in, in order to restore and maintain the original redblack nature, do the work: RB-INSERT-FIXUP (T, z ).

OK. Continue.

 

Iii. Implementation of the red/Black Tree Insertion Algorithm

RB-INSERT (T, z )//Note the notes I have given...
1 y branch nil [T] // y always points to the parent node of x.
2 x leading root [T] // x points to the root node of the current tree,
3 while x =nil [T]
4 do y records x
5 if key [z] <key [x] // left to right ..
6 then x left [x]
7 else x route right [x] // to locate the appropriate insert point, x explores the path of the trace until x becomes NIL.
8 p [z] ← y // y is set to insert the parent node of node z.
9 if y = nil [T]
10 then root [T] ← z
11 else if key [z] <key [y]
12 then left [y] ← z
13 else right [y] Then z // This 8-13 row sets the z-related pointer.
14 left [z] returns nil [T]
15 right [z] locale nil [T] // set to null,
16 color [z] inserted RED // use the newly inserted node z as the RED
17 RB-INSERT-FIXUP (T, z) // because the z is red, may violate a red black nature,

// So you need to call the RB-INSERT-FIXUP (T, z) to maintain the redblack nature.

17 rowsRB-INSERT-FIXUP (T, z), Which will be analyzed in the following sections.

Remember what I said at the beginning,

Yes, time

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.