Red and black tree Insert detailed

Source: Internet
Author: User

To find a binary tree Insert node:

It is known that the nature of the binary tree is that the value of the root node is greater than the value of the left subtree, less than the value of the right subtree, and according to this rule, find the appropriate insertion position for the value to be inserted, and finally insert it:

Tree-insert (t,z) {x = root (T); y = null;while (x! = NULL) {y = x;if (Key[z] < key[x]) x = Left[x];elsex = Right[x];} P[z] = y;if (y = = NULL) Root[t] = Z;else if (Key[z] < key[y]) left[y] = z;elseright[y] = z;}

Red-Black Tree Insert node:

Red-black tree inserting a node is similar to finding a binary tree, but it is important to note that when a new node is inserted into a red-black tree, it may not be possible to maintain the nature of the red-black tree, so it needs to be modified.

The function rb-insert-fixup (t,z) keeps it red and black.

Rb-insert (t,z) {x = root (T); y = nil[t];while (x = nil[t]) {y = x;if (Key[z] < key[x]) x = Left[x];elsex = Right[x];} P[z] = y;if (y = = Nil[t]) root[t] = Z;else if (Key[z] < key[y]) left[y] = z;elseright[y] = z;left[z] = nil[t];right[z] = ni L[t];color[z] = RED; Rb-insert-fixup (t,z);}

Unlike the previous search for binary tree inserts, the red-black binary tree is inserted in the process mainly in the last few steps with its differences: the color of the inserted node red, patch red black tree nature.

Next, when the new node is inserted, it breaks down the nature of the red-black tree.

The nature of the red and black trees is:

1. Each node is either red or black.

2. The root node is black

3. The leaf node is black

4. If a node is red, then its two sons are black.

5. For each node, include the same number of black nodes on all paths from the node to its descendant nodes

Easy to know the insertion of new nodes does not affect the properties 1, 3, 5

The question now is how to maintain the nature of 2 and 4.

When z is inserted into a red-black tree, consider the parent node of z, and when the parent is black, the properties 2 and 4 remain, so when considering while (color[p[z] = = RED), how to preserve the properties of 2 and 4.

There are two cases of inserting z: z = = Left[p[z]] or z = = Right[p[z]]. Here first consider z = = Left[p[z]].

Known Color[p[z]] = = Red, then by the red black tree Nature 4 know, Color[p[p[z]] "must be black. Because if the parent node of P[z] is red, then p[z] needs to be black and contradictory. and Z's uncle (set to Y) color may be red, it may be black. If Y is red, to preserve the property 4, p[z], y shall be set to Black, P[p[z]] set to red. then let z = p[p[z]], continue the loop, and if Y is black, at this point z may be the left or right subtree of p[z]. When z is the right subtree, you can perform the following steps: Z = p[z]; Left-rotate (t,z), which can be turned into a case where z is Zuozi. At this point the right-hand p[z], then P[p[z]] "becomes the right subtree of p[z], and Z is still the left sub-tree of P. To preserve the nature of the red and black trees, place the p[z] on the right sub-tree of Black, p[z, red.

Finally, color[p[z] "= red, for guaranteed properties 2,color[root[t]] = BLACK;


Red and black tree Insert detailed

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.