Introduction to Algorithms reading notes-13th-red and black trees

Source: Internet
Author: User

Introduction to Algorithms 13th Chapter Red black Tree

Red-Black Trees (red-black tree) are one of many balanced search trees that ensure that the time complexity of basic dynamic set operations is O (LGN) in the worst case scenario.

13.1 Nature of red and black trees

Red and black trees (red-black tree) : Two-fork search tree satisfying the following properties:

    1. Each node is red or black.
    2. The root node is black.
    3. The leaf nodes are all black.
    4. The child nodes of the red node are all black.
    5. For each node, the same number of black nodes are included from the node to all of its descendant leaf nodes.

Leaf nodes do not store keywords.

Hegau (black-height) : Property 5 Defines the concept of black height. From the node \ (x\) , the number of black nodes on any simple path reaching a leaf node is called the black height of the node and is recorded as \ (BH (x) \) .

lemma 13.1 : The height of a red-black tree with a \ (n\) inner node is at most ( 2lg (n+1) \) .

By the lemma easy to get, dynamic set operation Search, MINIMUM, MAXIMUM, successor, predecessor can be executed in O (LGN) time on the red black tree.

13.2 rotation

If the insert and delete operations of the binary search tree are used directly on the red-black tree, the run time is O (LGN), but because the tree has been modified, it may cause the tree to no longer satisfy the red-black nature, so it also needs to maintain the operation of these properties. The operation is divided into two parts, one for maintaining the pointer and the other for maintaining the color. The work of maintaining pointers is done by rotation (rotation).


# in tree T, left rotation with x node as axis . Span class= "Co" ># all the leaf nodes and the parent nodes of the root node are represented by the Sentinel T.nil, which means that the  # assumes the X right subtree is not empty, that is X.right!=t.nil  Left_ Rotation (t,x): Y =  x.right # y left subtree becomes x right subtree  x.right =<   /span> y.left if  y.left !=  t.nil:y.left.p =  x # position with y instead of x  y.p =  x.p if  x.p  = =  t.nil:t.root =  y else  if  x ==  x.p.left:x.p.left =  y else : X.p.right =  y # x turn left subtree of y  y.left =  x x.p =  Y  
# 在树T中,以x结点为轴进行右旋转操作# 这里将所有叶子结点以及根结点的父结点,都用哨兵T.nil表示# 假设x左子树不为空, 即x.left!=T.nilright_rotation(T,x):  = x.left  # y的右子树变成x的左子树  = y.right  if!= T.nil:    = x  # 用y代替x的位置  = x.p  if== T.nil:    = y  elseif== x.p.left:    = y  else:    = y  # x变成y的右子树  = x  = y
13.3 inserting
# Insert node Z in red black tree T# Insert the T as a normal binary search tree into the node z and place z in red# Then call the helper rb_insert_fixup to recolor and rotate the node.Rb_insert (t,z): Y=T.nil x=T.root# The previous section is the same as the insertion of the binary search tree   whileX!=T.nil:y=XifZ.key<X.key:x=X.leftElse: X=X.right Z.P=YifY==T.nil:t.root=ZElse ifZ.key<Y.key:y.left=ZElse: Y.right=Z# Here's how to keep the tree structureZ.left=T.nil Z.right=T.nil Z.color=RED Rb_insert_fixup (t,z)
Rb_insert_fixup (t,z): whileZ.p.color==RED:ifZ.p==Z.p.p.left:y=Z.p.p.right# The situation 1:z's tertiary junction is red .      ifY.color==Red:z.p.color=BLACK Y.color=BLACK Z.p.p.color=RED Z=Z.p.p# situation 2:z's tertiary node is black and z is a right child      # turns into a situation 3      Else ifZ==Z.p.right:z=Z.P left_rotate (t,z)# The situation 3:z's tertiary node is black and Z is a left childZ.p.color=BLACK Z.p.p.color=RED right_rotate (T,Z.P.P)Else:# with front symmetry, i.e. right and left swapT.root.color=BLACK

An analysis of what the rb_insert_fixup is doing: inserting this red node can only destroy Properties 2 (if inserted as root node) and property 4 (if inserting stepfather node is red).

The while loop in Rb_insert_fixup is analyzed: Each loop is divided into three cases, and case 1 increases the position of Z in T by two layers, and when 2 and 3 are executed, the loop exits, so the operation is O (h), O (LGN).

    • For Case 1, the tertiary node of z is red, first note that Z's grandfather node must be black, because the red black tree before inserting is satisfied with the red and black nature, and at this point the need to solve is z and Z of the parent node is red. So now Z's father, grandfather, tert-junction is the case of red black, only need to become black black, solved the previous local problem, now just pass the problem up, because Z's grandfather turned red, its parent node may also be red. At this point, the parent node of Z is assigned to Z, and a new loop is made.
    • For the case 2, Z of the Tertiary node is black, and z is a right child, for the convenience of processing, can be directly converted to case 3, the practice is to Z's parent node for the axis to do a left rotation.
    • For Case 3, the tree node of z is black, and Z is a left child, now Z, Z of the parent node, Z's grandfather knot is red and black, and all is left child relations, contradiction lies in two red nodes of the parent-child relationship violates the red and black nature, the basic idea is to move a red knot to the right subtree of the grandfather node, By a right-rotating operation with the grandfather node as the axis, and then turning the color into a red black, the previous local problem was solved. And we did not introduce any new properties to the damage because, compared to case 1, the current axis color is black. The loop should therefore terminate at the next time.
13.4 Delete
# 将红黑树中的结点u用v代替# 与二分检索树中的transplant并不逻辑上的不同br_transplant(t,u,v):  if== T.nil:    = v  elseif== u.p.left:    = v  else:    = v  = u.p
# remove node z from red-black tree T species# Basic structure similar to binary search tree# If the node to be removed is black, it is possible to destroy the red-black nature# Calling the helper program Rb_delete_fixupRb_delete (t,z): Y=Z Y_original_color=Y.colorifZ.left==T.nil:x=Z.right rb_transplant (t,z,z.right)Else ifZ.right==T.nil:x=Z.left rb_transplant (T,z,z.left)Else: Y=Tree_minimum (z.right) Y_original_color=Y.color x=Y.rightifY.p==Z:x.p=YElse: Rb_transplant (t,z,y) y.right=Z.right Y.RIGHT.P=Y rb_transplant (t,z,y) y.left=Z.left Y.LEFT.P=Y Y.color=Z.colorifY_original_color==Black:rb_delete_fixup (t,x)
Rb_delete_fixup (t,x): whileX!=T.root andX.color==BLACK:ifX==X.p.left:w=X.p.right# X's sibling knot      # situation 1:x's brother knot is red .      ifW.color==Red:w.color=BLACK X.p.color=RED left_rotate (t,x,p) W=X.p.right# situation 2:x's brother knot is black, and X's two nephews ' knots are black .      ifW.left.color==BLACK andW.right.color==Black:w.color=RED x=X.p# situation 3:x's brother knot is black, and X's left nephew is red, right nephew is black      Else ifW.right.color==Black:w.left.color=BLACK W.color=RED right_rotate (t,w) W=X.p.right# situation 4:x's brother knot is black, and X's right nephew is redW.color=X.p.color X.p.color=BLACK W.right.color=BLACK left_rotate (T,X.P) x=T.rootElse:# like above, swap right and leftX.color=BLACK

Rb_delete_fixup Basic idea: If the node y in Rb_delete is a black node, the newly substituted node is x, which brings up the following problems: If Y is the original root node, and x is the red node, it violates the nature 2; If x and X.P are red nodes, violate the nature of 4; Moving y in the tree causes the number of black nodes on the simple path that previously contained y to be less than 1, and any ancestor of Y is not satisfied with the property 5. The idea is to think of X as an extra black. Under this hypothesis, the nature of 5 is established. And the problem becomes that X is double black or red black, violating the nature of 1.

The while loop in rb_delete_fixup does such a thing by moving an extra layer of black along the tree or eliminating it in a step in the loop. X is a constant (rising in the direction of the tree) of the amount of change, x refers to the node represents "more layer of black." The loop terminates in several cases:

    • X points to the red and black nodes, that is, the Color field itself is the red node, when the red is directly removed, that is, the color becomes black.
    • X point to the root node, and simply remove the extra black at this point.
    • Perform proper rotation and recolor to exit the loop.

The while loop handles the following four scenarios:

    1. X's sibling junction W is Red, at this point the rotation operation is converted into 2,3,4 processing, that is, the Brother node for a black junction.

    2. X's brother Knot W is black, and W's two sub-nodes are black, indicating that W changed to red does not affect the nature of 4, when the W into red, will be X of the extra layer of black and w black at the same time off, to their parent node, does not affect the nature of 5. This allows the extra black to go up a layer, and the x is also tracked up into the next loop.

Note that if case 1 goes into Case 2, the parent node must be a red node, that is, the transformed X is the red and black node, and the next step is to exit the loop.

    1. X's brother Knot W is black, and W's left child is red, right child is black, at this time by Rotary discoloration directly converted to case 4.

    2. X's brother Knot W is black, and W's right child is red, regardless of the X and W Common parent node is what color, it may be set to C, then [X, X's parent node, W, W's right child] at this time is the Black X black, by the parent node and W color Exchange, W's right child turns black, and then the parent node for the axis left , you can give the extra black of X to the original right child, still guarantee that the nature of 5 is established, the cycle can end.

It is easy to know from the above analysis that the Rb_delete run time is O (h) i.e. O (LGN).

Introduction to Algorithms reading notes-13th-red and black trees

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.