Red/Black (2), red/Black

Source: Internet
Author: User

Red/Black (2), red/Black
1. Insert Introduction

First, add a node using the binary sorting tree method and mark it as red. (Why not red? If it is set to black, it will cause the root to all the paths of the leaves, and there will be an extra black node in one path, which is difficult to adjust ). However, if a red node is set, two consecutive red nodes may conflict. you can adjust it by re-coloring and rotating. The specific adjustment operation depends on the color of other neighboring nodes.

Next, we will analyze the potential impact of inserting a new node on the property of the red/black tree:
Property 1-the node is red or black. And nature 3-all leaves are black. These two parameters can always be retained.
Property 4-the two subnodes of each Red node are black. It is only threatened when Red nodes are added, when black nodes are re-painted, or when rotation is performed.
Property 5-all paths from each leaf to the root contain the same number of black nodes. It is only threatened when black nodes are added, red nodes are repainted, or rotated.

2. Insert Algorithm

In the insert operation of AVL, we will use the rotation tool to maintain the balance after insertion. In the red and black trees, we use two tools to maintain a balance:
(1) re-coloring
(2) Rotating

First, the system tries to re-color it. If it cannot work, it uses rotation. In addition, the color of the empty node is black by default.
Based on the color of the uncle node, this algorithm has two main scenarios. If the node is red, you need to color it again. If it is black, use rotation and re-coloring.
Insert Process:
Assume that x represents the new node to be inserted. P is the parent node of x, u is the uncle node of x, and g is the grandfather node of x.
Insert the node of the standard binary sorting tree (BST) and set x to red.
At this time, three main scenarios may be formed. For details, refer to the following section.

3. Scenario classification (Scenario)
3.1 scenario 1: If the parent node of X is black, no further processing is required.

Analysis: In this case, the tree is still valid. Property 4 is not invalid (the new node is red ). Property 5 is not under threat. Although the new node x has two black leaf nodes, since x is red, the path of each sub-node of the sub-node has the same number of black nodes as the path of the Black leaves replaced by the sub-node, so it still satisfies this nature.

3.2 Scenario 2: If x is the root node, set it to black.

Analysis: Set it to black to meet the requirements of Nature 2. For a full binary tree, the height of the Black node increases by 1.

3.3 scenario 3: if the parent node of x is red or x is not the root node, perform the following operations:

Scenario 3.3.1: if the primary node of x is red (the grandfather node must be black based on the attribute 4)
(A) Set the parent node and Uncle node to black.
(B) Set the grandfather node to red.
Now, our new node x has a black parent node p. Because any path through parent node p or Uncle node u must pass through grandfather node g, the number of black nodes on these paths has not changed.
However, the red grandfather node g's parent node may also be red, which violates Nature 4. To solve this problem, we need to perform recursive scenario analysis on the grandfather node g (that is, we should consider g as a newly added node for various checks ).
(C) execute the value assignment operation "x = g", assign the grandfather node to x, and then perform scenario analysis on the new x recursively.
The preceding steps can be used for demonstration.



Scenario 3.3.2: if the primary node of x is black, there are four scenarios (similar to AVL Tree) for x, p, and g ).
(A) Left Case (p is the Left child of g and x is the Left child of p)
(B) Left Right Case (p is the Left child of g and x is the Right child of p)
(C) Right Case (p is the Right child of g and x is the Right child of p)
(D) Right Left Case (p is the Right child of g and x is the Left child of p)

You can describe these four images in the following situations.
(A) Left Case (refer to g, p, and x)


(B) Left Right Case (refer to g, p, and x)


(C) Right Case (refer to g, p, and x)


(D) Right Left Case (refer to g, p, and x)


4. Example of an insert operation

The example below demonstrates the process of inserting a new node in the red/black tree, covering multiple scenarios described above.

5. Code Implementation

The following code strictly complies with the scenarios described in section 3rd.

// C ++ program: inserts a red/black tree # include <iostream> // The Red/black tree Node struct Node {int data; // The Node data char color; // color attribute // left child, right child, and parent Node pointer Node * left, * right, * parent ;}; // Left-hand/* y x/\ Right Rotation/\ x T3--> T1 y/\ <--/\ T1 T2 Left rotation T2 T3 */void LeftRotate (Node ** root, node * x) {// y Save the right child Node * y = x-> right of x; // use the left child of y as the right child of x-> right = y-> left; // update the parent pointer of the right child of x if (x-> right! = NULL) x-> right-> parent = x; // update the parent pointer y of y-> parent = x-> parent; // if the parent node of x is null, then, y becomes the new root node if (x-> parent = NULL) (* root) = y; // save y to the original position of x. else if (x = x-> parent-> left) x-> parent-> left = y; else x-> parent-> right = y; // use x as y's left child y-> left = x; // update the parent node of x to yx-> parent = y;} // right (opposite to left) /* y x/\ Right Rotation/\ x T3--> T1 y/\ <--/\ T1 T2 Left Rotation T2 T3 */void rightRotate (Node ** roo T, Node * y) {// x stores the right child Node of y * x = y-> left; // use the right child of x as y's left child y-> left = x-> right; // update the parent pointer of the right child of x if (x-> right! = NULL) x-> right-> parent = y; // update x's parent pointer x-> parent = y-> parent; // if the parent node of x is null, then x becomes the new root node if (x-> parent = NULL) (* root) = x; // else if (y = y-> parent-> left) y-> parent-> left = x; elsey-> parent-> right = x; // use y as the right child of x. x-> right = y; // update y's parent node to xy-> parent = x;} // function. After the standard BST is inserted, adjust the red/black tree. Void insertFixUp (Node ** root, Node * z) {// traverses the tree until z is not the root Node and z's parent Node is red. [scenario 3] while (z! = * Root & z-> parent-> color = 'R') {Node * y; // find the uncle Node, save as yif (z-> parent = z-> parent-> left) y = z-> parent-> right; elsey = z-> parent-> left; // If the node is red, perform the following operations. [Scenario 3.1] // (a) set the parent node and Uncle node to black // (B) set the grandfather node to Red // (c) assign the grandfather node to z and continue to check if (y-> color = 'R') {y-> color = 'B '; z-> parent-> color = 'B'; z-> parent-> color = 'R'; z = z-> parent ;} // If the uncle node is black, there are four situations: LL, LR, RL, RR. [Scenario 3.2] else {// Left-Left (LL) case // (a) swap the color of the parent node and the grandfather node // (B) right-hand if (z-> parent = z-> parent-> left & z = z-> parent-> left) {char ch = z-> parent-> color; z-> parent-> color = z-> parent-> color; z-> parent-> color = ch; rightRotate (root, z-> parent);} // Left-Right (LR) case // (a) change the color of the current node and the grandfather node // (B) perform left-hand operations on the parent node // (c) right-handed if (z-> parent = z-> parent-> left & z = z-> parent-> Right) {char ch = z-> color; z-> color = z-> parent-> color; z-> parent-> color = ch; leftRotate (root, z-> parent); rightRotate (root, z-> parent);} // Right-Right (RR) case // () color of the parent node and the grandfather node // (B) left-side if (z-> parent = z-> parent-> right & z = z-> parent-> right) {char ch = z-> parent-> color; z-> parent-> color = z-> parent-> color; z-> parent-> color = ch; LeftRotate (root, z -> Parent);} // Right-Left (RL) case // (a) change the color of the current node and the grandfather node // (B) parent node right-hand // (c) left-side if (z-> parent = z-> parent-> right & z = z-> parent-> left) {char ch = z-> color; z-> color = z-> parent-> color; z-> parent-> color = ch; rightRotate (root, z-> parent); LeftRotate (root, z-> parent) ;}} (* root)-> color = 'B '; // The root Node is always black} // function: insert a new Node to the red/black tree void insert (Node ** root, int data) {// allocate memory N for the new Node Ode * z = new Node; z-> data = data; z-> left = z-> right = z-> parent = NULL; // if the root Node is null, then z becomes the root node if (* root = NULL) {z-> color = 'B'; // The root node is always black (* root) = z ;} else {Node * y = NULL; Node * x = (* root); // Follow standard BST insert steps to first insert the nodewhile (x! = NULL) {y = x; if (z-> data <x-> data) x = x-> left; elsex = x-> right ;} z-> parent = y; if (z-> data> y-> data) y-> right = z; elsey-> left = z; z-> color = 'R'; // The newly inserted node is always red. // call insertFixUp to fix the attribute of the red/black tree, because it may be damaged due to the insert operation. InsertFixUp (root, z) ;}// function: traverses the red/black tree void inorder (Node * root) {if (root = NULL) return; inorder (root-> left); std: cout <root-> data <"-->"; inorder (root-> right);} int main () {Node * root = NULL; insert (& root, 5); insert (& root, 3); insert (& root, 7); insert (& root, 2 ); insert (& root, 4); insert (& root, 6); insert (& root, 8); insert (& root, 11); std :: cout <"Inorder Traversal Is: \ n"; inorder (root); std: cout <"null" <std: endl; return 0 ;}
Running result:
Inorder Traversal Is:
2 --> 3 --> 4 --> 5 --> 6 --> 7 --> 8 --> 11 --> null

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.