Understanding of the red-black tree rotation

Outline

Left and right hand red-black tree is more difficult to understand, a lot of information online that is very complicated, and here I use the words too literally thinking to explain the left and right hand. It may help users search for information.

The definition of a binary search tree

Binary search tree is empty tree or a binary tree having the following properties:
1, if the left subtree is not empty, then the value of the left sub-tree, all the nodes are less than the value of the root node;
2, if the right child tree is not null, then the value of the right sub-tree, all the nodes are greater than the value of the root node;
3, left and right sub-trees are binary sort tree;
4, no node equivalent key.

The definition of red-black tree

What is the red-black tree?

Red-black tree (English: Red-black tree) is an unbalanced binary search tree,
without the need to ensure that the left and right sub-tree height is less than or equal to 1.

Is a data structure used in computer science, typical use is to realize an associative array.
It is in 1972 by the Rudolf Bell invented, called "symmetric binary B-tree",
its modern name derives from an essay written by Leo J.Guibas and RobertSedgewick in 1978.
Red-black tree structure complicated, but it has a good worst-case operating run time,
and efficient in practice: it can be done to find, insert and delete in O (logN) time,
n-where the elements in the tree number.

Characteristic red-black tree

Red-black tree is a binary search tree each node has a color attribute, the color is red or black.
In a binary search tree outside the mandatory general requirements for any valid red-black tree we have added additional requirements as follows:

1, the node is red or black.
2, the root is black.
3, all the leaves are black (leaf node is NIL).
4, each red node must have two black child nodes. (From leaf to root you can have between two consecutive nodes red)
5, from any node to all simple paths each leaf contains the same number of black nodes.

image

这些约束确保了红黑树的关键特性:
从根到叶子的最长的可能路径不多于最短的可能路径的两倍长。

Rotation

L

左旋图解(x左边节点逆时针旋转):
         a                
        / \                               
       b   x      ->        x
          / \              / \
         c   d            a   d
                         / \  
                        b   c

FIG centered at x, parent node is a, the left sibling node is b, the left child is c, d is the right child node

L x is to ensure constant d and the right child node, is directly related to the counterclockwise rotation of the left node of the node x, that is, a, b, C
L process is the parent of x nodes a, b is rotated counterclockwise left sibling ,
the parent node of the original left node x c x is an alternative to
the original left child node x c x becomes the right child of the original parent node a counterclockwise direction after translation.

 

Right-handed

右旋图解(x右边节点顺时针旋转):
         a            
        / \             
       x   b      ->      x  
      / \                / \                  
     d   c              d   a
                           / \
                          c   b

FIG centered at x, is a parent node a, the right sibling nodes is b, d is the left child, right child node c is
right-handed and left is to ensure that the child node x d constant, is directly related to the x-clockwise rotation right node, i.e. a, B, c
dextrose process x is the parent node a, the right sibling node B, as well as the right child node c is rotated clockwise.
The original parent right node x c x is an alternative,
into a parent node x original right sub-posterior translation of the original x c clockwise left child node.

Guess you like

Origin www.cnblogs.com/geektcp/p/11520014.html