1. Insert Introduction
the first way to sort trees with two forks Add the node and mark it red. (Why not red?) Because if set to black, it will cause the root to the leaves of all paths, one path will have an extra black node, this is difficult to adjust. However, when set to a red node, it may cause two consecutive red nodes to conflict, which can be adjusted by re-coloring and rotation . The specific adjustment action depends on the color of the other neighboring nodes.
The following analysis will affect the nature of the red-black tree after inserting a new node:
Property 1-The node is red or black. and Nature 3-all leaves are black. These two articles can always be kept intact.
Property 4-Two child nodes of each red node are black. is threatened only when the red node is added, the black node is redrawn red, or rotated.
Property 5-all paths from each leaf to the root contain the same number of black nodes. is threatened only when you increase the black node, redraw the red node as black, or rotate.
2. The algorithm when inserting
In the AVL insert operation, we use the Rotate tool to maintain the balance after insertion. In a red-black tree, we use two tools to maintain balance:
(1) Re-coloring
(2) Rotation
First, you'll try to recolor it first, and if it doesn't work, use rotation. Also, the color of the empty node defaults to black.
Based on the color of the tertiary node, there are two main scenarios for this algorithm. If the tertiary node is red, it needs to be re-colored. If it is black, use rotation and recolor.
Insert procedure:
Suppose x represents the new node that will be inserted. P is the parent node of x, U is the Tertiary node of x, and G is the grandfather node of X.
Performs a standard two-fork sort tree (BST) node insert operation, and the X is red.
At this point, there are 3 main scenarios that can be formed, as described in the following section.
3. Scene classification (Scenario)3.1 Scene 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 does not expire (the new node is red). Property 5 is also not threatened, although the new node x has two black leaf nodes, but since x is red, the path through each of its child nodes has the same number of black nodes as the path through which the black leaves are replaced, so this property is still satisfied.
3.2 Scene 2:if x is the root node, it is set to black
Analysis: Set to Black is to satisfy the nature of 2. For a complete binary tree, the height of the black node increases by 1.
3.3 Scene 3: If the parent node of x is red, or x is not the root node, do the following
scenario 3.3.1: If the tertiary node of x is red (then the grandfather node must be black according to the nature of the 4)
(a)set the parent node and the tertiary node to black.
(b)Place the grandfather node in red.
now our new node X has a black parent node p. Because any path through parent node p or tert-node u must pass through grandfather node G, the number of black nodes on these paths has not changed.
However, the parent node of the red Grandfather node G may also be red, which violates the nature of 4. To solve this problem, we need to perform the scene analysis recursively on the grandfather Node G (that is, the G as the newly added node for various scenarios).
(c)performs the assignment operation "x = g", assigns the grandparent node to X, and then recursively analyzes the new x for the scene.
the above steps can be used to demonstrate.
scene 3.3.2: If the tertiary node of x is black, then for X, p and G there are 4 scenarios (similar to the AVL tree).
(a) left left case (p is child of G, and X is P's left child)
(b) left Right Case (p is the left child of G, and X is P's right child)
(c) (d) right left case (p is the right child of G, and X is the child of P)
These 4 scenarios can be described in the 4 images below.
(a) Left left Case (refer to G, p and X)
(b) Left Right case (refer to G, p and X)
(c) Right -case (reference G, p, and X)
(d) Right-left case (refer to G, p, and X)
4. Example of insert Operation demo
The following example illustrates the process of inserting a new node in a red-black tree, covering a variety of scenarios described above.
5. Code implementation
The following code is written in strict accordance with what scenarios are described in section 3rd.
C + + Program: Implement red-black tree insert operation # include <iostream>//red-black tree node struct node{int data; node data char color; Color Properties//left child, right child and parent node pointer node *left, *right, *parent;};/ /L/* y X/\ Right rotation/x t3–––––––> T1 y/\ <-------/T1 T2 left Rotation T2 t3*/void leftrotate (node **root, node *x) {//y save X's right child Node *y = x->right;//the left child of Y as X's right child x->right = y->left;//Update x's right child's parent pointer if (x->right! = NULL) X->right->parent = x;//Update Y's parent pointer 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;//saves y in the original position of x else if (x = = x->parent->left) X->parent->left = Y;else X->parent->right = y;//left child x as y Y->left = x;//update The parent node of x is Yx->parent = y;} Right-hand (opposite to left)/*/y X/\ rotation/x t3–––––––> T1 y /\ <-------/T1 T2 left Rotation T2 t3*/void rightrotate (Node **root, node *y) {//x Save Y's right child Node *x = y->left;//will X's right child as Y's left child y->left = x->right;//Update x's right child's parent pointer 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;//saves x in the original position of Y, else if (y = = y->parent->left) Y->parent->left = X;elsey->parent->right = x;/ /y as x right child x->right = y;//Update y parent node is xy->parent = x;} function function, after performing standard BST inserts, adjusts the red-black tree. void Insertfixup (node **root, node *z) {//traversal tree, until Z is not the root node, and the parent node of z is red [corresponding case 3]while (z! = *root && z->parent-> color = = ' R ') {node *y;//found tert-node, saved as yif (z->parent = = z->parent->parent->left) y = z->parent->parent- >right;elsey = z->parent->parent->left;//If the tertiary node is red, perform the following operation. [Corresponding case 3.1]//(a) Set the parent node and the tertiary node to black//(b) Place the grandfather node in red//(c) Assign the grandparent node to Z and continue to detect if (Y->color = = ' R ') {y->color = ' B '; z->parent- >color = ' B '; z->parent->parent->color = ' R '; z = z->parent->parent;} If the tertiary node is black, there are 4 cases, LL,LR,RL,RR. [Correspondence Case 3.2]else{//left-left (LL) case//(a) Swap parent node with grandfather node color//(b) Grandfather node for right-handed if (z->parent = = Z->parent->parent->left &&z = = z-> parent->left) {Char ch = Z->parent->color;z->parent->color = z->parent->parent->color;z-> Parent->parent->color = Ch;rightrotate (root, z->parent->parent);} Left-right (LR) case//(a) Swap the current node with the color of the Grandparent node//(b) parent node for left-handed//(c) Grandfather node to right-click if (z->parent = = z->parent->parent-> Left &&z = = z->parent->right) {Char ch = Z->color;z->color = z->parent->parent->color;z- >parent->parent->color = ch; Leftrotate (Root, Z->parent); Rightrotate (root, z->parent->parent);} Right-right (RR) case//(a) Swap the parent node with the grandfather node color//(b) Grandfather node for left-handed if (z->parent = = Z->parent->parent->right & &z = = z->parent->right) {Char ch = Z->parent->color;z->parent->color = z->parent->parent- >color;z->parent->parent->color = ch; Leftrotate (Root, z->parent->parent);} Right-left (RL) case//(A) Swap the color of the current node with the Grandfather node//(b) parent node for right-turn//(c) Grandfather node for left-handed if (z->parent = = Z->parent->parent->right &&z = = z-> parent->left) {Char ch = Z->color;z->color = z->parent->parent->color;z->parent->parent-> color = ch;rightrotate (root, z->parent); Leftrotate (Root, Z->parent->parent);}}} (*root)->color = ' B '; The root node is always black}//function: Insert new node to red black tree void Insert (node **root, int data) {//Allocate new nodes memory node *z = 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 new Insert node always red//calls Insertfixup to fix the properties of the red-black tree, because it may be corrupted because of the insert operation. Insertfixup (root, z);}} function function: Middle sequence traversal 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, one);std::cout<< "inorder Traversal is: \ n "; inorder (root); Std::cout <<" null "<<std::endl;return 0;}
Operation Result:
Inorder traversal is:
2-->3-->4-->5-->6-->7-->8-->11-->null
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Red-black Tree (2)-insert operation