Two forks The basic operation of the search tree can be done within O (h), however the height of the number is higher, and may not be faster than executing on the linked list. red and black trees are a guarantee in the search tree of the worst case time complexity O (LG (n))
The nature of red and black trees
- Each node is either red or black.
- The root node is black
- Each leaf node is black.
- If a node is red, then two child nodes are black
- Each node, on the path to the descendant leaf node, contains the same number of black nodes
Because the leaf node is null, there is no way to store the color, so with a sentinel nil, other attributes p, left, right, key are any value (NULL) as well.
By the nature of the red and black tree, H max is 2LG (n+1)
Rotation operation
Picture source, where the introduction of red and black trees is more detailed
leftrotate (T, h) {x=H.right; //the left sub-tree of x becomes the right subtree of HH.right =X.left; if(X.left! =t.nil) x.left.parent=h; //the parent of X is the parent of HX.parent =h.parent; //The parent of H corresponds to the subtree of H, and becomes the X if(H.parent = =t.nil) T.root=x; Else if(H = =h.parent.left) H.parent.left=x; ElseH.parent.right=x; //set the left subtree of x to HX.left =h; H.parent=y;}
rightrotate (T, x) {h=X.left; //the right sub-tree of H becomes the left subtree of xX.left =H.right; if(H.right! =t.nil) h.right.parent=x; //The parent of H is the parent of XH.parent =x.parent; //the parent of x corresponds to the sub-tree of x, which becomes H if(X.parent = =t.nil) T.root=h; Else if(x = =x.parent.right) X.parent.right=h; ElseX.parent.left=h; //set the right subtree of H to xH.right =x; X.parent=y;}
Insert operation
The insert operation is essentially the same as the search binary tree operation. It is important to note that
- Null replaced by nil
- The sub-tree of the inserted element is set to nil
- Set the inserted element to red
- Rbinsertfixup the inserted elements to ensure compliance with the red and black tree properties
Treeinsert (T, z) {y=T.nil; X=T.root; while(X! =t.nil) {y=x; if(Z.key <x.key) x=X.left; Elsex=X.right; } x.parent=y; if(Y = =t.nil) T.root=Z; Else if(Z.key <y.key) Y.left=Z; ElseY.right=Z; Z.left= Z.right =T.nil; Z.color=RED; Rbinsertfixup (T, z);}
Understanding Rbinsertfixup
- Inserting z, because Z and parent node z.p are red, violate the nature of the red-black tree. Since the tertiary node y of z is red, corresponds to condition 1. The node is re-shaded, z rises one, becomes the second picture
- Z and parent node violate again, Z's tertiary node is black. Since z is the right child of the Z parent node, corresponds to condition 2. Perform a left spin once to get the third picture
- Z is the parent node of the left child, recolor, perform right-handed to get the fourth picture
- Become a legitimate red-and-black tree
Rbinserfixup (T, z) { while(Z.parent.color = =RED) { if(Z.parent = =z.parent.parent.left) {y=Z.parent.parent.right; if(Y.color = =RED) {Z.parent.color=BLACK; Y.color=BLACK; Z.parent.parent.color=RED; Z=z.parent.parent; } Else if(Z = =z.parent.right) {z=z.parent; Leftrotate (T, z); } Z.parent.color=BLACK; Z.parent.parent.color=RED; } Else//change the right and left of the medical condition.{y=Z.parent.parent.left; if(Y.color = =RED) {Z.parent.color=BLACK; Y.color=BLACK; Z.parent.parent.color=RED; Z=z.parent.parent; } Else if(Z = =z.parent.left) {z=z.parent; Leftrotate (T, z); } Z.parent.color=BLACK; Z.parent.parent.color=RED; } }}
Delete operation
Situation with a two-fork search tree analogy. First you need a transplant to apply to the red-black tree.
rbtransplant (T, U, v) { if (u.parent = = T.nil )= v ; Else if (U = = U.parent.left ) = V; Else = v; = u.parent;}
And then perform the delete
Rbtreedelete (T, z) {y=Z; Yoriginalcolor=Y.color; if(Z.left = =t.nil) {x=Z.right; Rbtransplant (T, z, z.right); } Else if(Z.right = =t.nil) {x=Z.left; Rbtransplant (T, z, z.left); } Else{y= Treeminimum (z.right);//Z with Gemini, the successor is the smallest rightYoriginalcolor =Y.color; X=Y.right; if(Y.parent = =z) {x.parent=y; } Else{brtransplant (T, y, y.right); Y.right=Z.right; Y.right.parent=y; } brtransplant (T, z, y); Y.left=Z.left; Y.LEFT.P=y; Y.color=Z.color; } if(Yoriginalcolor = =BLACK) Rbdeletefixup (T, x);}
Rbdeletefixup (T, x) { while(x! = T.root && X.color = =BLACK) { if(x = =x.parent.left) {W=X.parent.right; if(W.color = =RED) {W.color=BLACK; X.p.color=RED; Leftrotate (T, x.parent); W=X.parent.right; } if(W.left.color = = BLACK && W.right.color = =BLACK) {W.color=RED; X=x.parent; } Else{W.left.color=BLACK; W.color=RED; Rightrotate (T, W); W.x.parent.right; } W.color=X.parent.color; X.parent.color=BLACK; W.right.color=BLACK; Leftrotate (T, X.P); X=T.root; } Else // Change Right and left{W=X.parent.left; if(W.color = =RED) {W.color=BLACK; X.p.color=RED; Leftrotate (T, x.parent); W=X.parent.left; } if(W.right.color = = BLACK && W.left.color = =BLACK) {W.color=RED; X=x.parent; } Else{W.right.color=BLACK; W.color=RED; Rightrotate (T, W); W.x.parent.left; } W.color=X.parent.color; X.parent.color=BLACK; W.left.color=BLACK; Leftrotate (T, X.P); X=T.root; }} X.color=BLACK;}
The extent of the red and black trees
:: Dynamic Sequential statistics
Add a Size property to each node of the red-black tree, representing the number of nodes in the tree, where the number of Sentinel nodes is size 0. The
X.size=x.left.size+z.right.size+1
Red and black Trees