Red-black tree - self-balancing process

The concept of red-black tree, do not make elaborate. Online grasping a lot. This article only personal understanding tell us about the process of self-balanced red-black tree.

Red-black tree properties:

  • Property 1: Each node is either black or red.
  • Property 2: the root is black.
  • Property 3: each leaf (NIL) is black.
  • Property 4: two red sub-nodes each node necessarily black.
  • Nature 5: any node to a leaf node of each path contains the same number of black

Since operation of the balancing required, it is that color, L, D. Needless to say color, red black blackening. About L, I have enclosed a map found on the Internet

 

Source: http://www.360doc.com/content/18/0904/19/25944647_783893127.shtml

As the red-black tree itself is a binary search tree, that is, the value of all the nodes in the left subtree are less than their own, all nodes in the value of the right subtree are greater than their own. Thus, when the rotation, y mention the book as the root of the tree, because the y turned out to be the right child of x, y> x, x becomes the left child y. b y had left the children would have to make room for x, x exactly right node empty out. b formerly a member of the right subtree of x, b> x, x become the right child is very reasonable.

About right hand, the same principle.

 

Source: http://www.360doc.com/content/18/0904/19/25944647_783893127.shtml

 

Description From now on balanced red-black tree.

First, the insertion node is colored. All about red-black tree from a balanced presentation say, inserted node is red. As to why, I believe, due to the nature of the 3 and 5 nature, any path to a node to each leaf node contains the same number of black, and each leaf node (NIL) is black. The new node will be inserted into the initial position to replace a leaf node, while his own would produce about two new leaf nodes. If the new node is inserted black, it will cause the new node from the root node to the node number of the black path more than one other path. Inserted red node, there will not be this case, but there may violate property 4, two sub-nodes each red node necessarily black, two consecutive red nodes appear, but not inevitable. Therefore, the newly inserted node is red, may be such that the probability of self-balancing operation needs to be reduced.

Then, if it is really there has been continuous two red nodes, without affecting the overall adjustment in black high, can greatly reduce the number of adjustments.

When a new node is inserted into a red, in addition to the current tree is empty outer possible are:

1, its parent node is black. High this situation will not change its parent at the black, it will not break any nature. Do not do any treatment

2, its parent node is red. Since the red-black tree is self-balancing, before inserting a new node, it is already balanced, satisfying all the properties. So the grandparent node must be black.

a. his uncle node is a leaf node. If you insert a new node (RL) left sub-tree in the right child of the root node. Simply modify the color of its parent node and can not make it balance.

 

 If carried out directly on the 20, 10 L, is obtained

 

 And left-handed in the same manner, we will obtain an initial state, an infinite loop. If the first of the 15, 20 were right-handed, it will be RR:

This time to 10, 15 to be left-handed, you will get:

 

 10, 15 and then to be discolored

 

 

 At this equilibrium. The same as the case of the LR contrast, the opposite operation, will not be described separately. The situation has been described above, RR, LL and RR same case, the same operation, nor do described.

b. uncle node whose node red

 

 At this same time directly with the parent node discoloration uncle

 

 By this time will result in a high black +1 grandparent node 15. Due to the uncertainty of whether there is a parent node, in order to unnecessary trouble, 15 to red to black to maintain constant high 15's.  

 

 Description: here only 15 sub-tree root node as a tree, not to break the nature 2.

c. it is black uncle node node (non-leaf) does not exist in this case just insert. The following reasons: the number of black nodes must be the same for each path. If the newly inserted parent node is red, and the uncle node is black, only shows that more than a black high-uncle node than the parent.

 

Now with a simple example:

A red-black tree to sequentially insert numbers 1,2,3,4,5

First, insert a red node.

 

Depending on the nature 2, the root node is black, the black can be directly replaced. At this time, a height of 2 Black

 

 

Then insert 2. Did not break the five major nature, do not do any treatment. At this time, the height of all paths Black 2.

 

 

After insertion 3 (RR)

 

 

Father node 3 with 2 L grandparent node, and color

 

 

 

 And then continue to insert 4

 

At this time, the adjacent red nodes again. Father, uncle node 4 and node 3 1 are red, and the color will both be operating, holding properties may be a 4, two nodes does not change the difference in black.

 

 

 After this operation, the grandfather node 4 + 1 2 Black high. If it is a sub-tree of a red-black tree, it will destroy the balance. It will be subjected to color treatment.

 

 

然后继续以2为基础做自平衡。发现其已经是根节点,没有父节点,自己的子树已达到平衡,不用继续处理。然后根据性质2,将其设置为黑色。此时黑高为3。

 

继续插入5

 

   再次出现相邻红色节点,情况与插入3时相同,做相同处理

 

 现分析下列红黑树插入节点21的情况。插入之前,根节点的黑高为3。

 

 

图片来源:http://www.360doc.com/content/18/0904/19/25944647_783893127.shtml

说明:前几步与来源讲解的有点类似,最后并不像来源处将13 17左旋了。

 

 节点21 22为相邻红色节点,要对新插入的节点21的父节点22进行变色。21的叔叔节点27同为红色,与22同时变色,可保证21的祖父节点25保持平衡。

 

 

 

而此操作之后,将会导致25子树黑高+1,使得其黑高比其兄弟节点多1。为了防止这种情况,25需要跟着色,才能保证25节点子树的黑高不变,从而不影响整个红黑树的黑高。

 

 虽然整体黑高没有发生变化,但是再次出现了相邻红色节点17 25。用相同策略,把8,17,13同时进行变色。

 

此时13节点的左右子树的黑高均是3,保持平衡。且13为根节点,没有父节点,13的平衡代表整个红黑树的平衡,不用再继续处理。  然后根据性质2,将根节点13改为黑色。

 

 

 若此时再插入节点20,则

由于20的叔叔节点为黑色节点,单纯修改父节点为黑色,只会导致祖父节点变得不平衡。旋转21,22,并各自变色,就可以达成平衡

 

 

Guess you like

Origin www.cnblogs.com/chinxi/p/12174162.html