[] Red-black tree algorithm

When studying red-black tree, I found a lot of blog did not clear the red-black tree, so I went to see the wikipedia definition of red-black tree. Chinese website ( link ) describes the total organic red-black tree turned flavor, presentation is not particularly clear, I am here to take English website ( link ) matching to do some introduction.

Red-black tree:

Is a self-balancing binary search tree, it's to find, insert and delete time complexity are O (logn). Many people start to learn to take the red-black tree is a balanced binary tree for comparison, so difficult to understand, in fact, red-black tree is equivalent to the 2-3-4 tree, is inserted in the nuclear 2-3-4 tree delete operation is also equivalent to the color of red-black tree inverted core rotation.

Red-black tree properties:

Red-black tree is still a binary search tree, but each node has a color attribute, the node is either red or black.

Under the premise of nature has a binary search tree, red-black tree properties are as follows:

1. The node is red or black

2. The root is black

3. All leaves are black leaves (the leaves are all empty NIL)

4. From each leaf to the root of all the paths can not have two consecutive red nodes (nodes not directly connected to red)

5. from any node to all simple paths each leaf contains the same number of black nodes.

As shown below:

 

 

 

Efficient red-black tree causes:

Although my legend red-black tree is red and black, and a lot of people at the beginning of the study believe that the color of the red-black tree hierarchically and white, but in fact, this view is wrong. 5 Properties of red-black tree to ensure that only one property: from root to leaf longest possible path from the distance not more than twice as long as the shortest possible path. This property is different from a balanced binary tree, red-black tree that is substantially balanced. Since the insert, delete, find a nuclear worst case is worth the time required was proportional to the height of the tree, this highly theoretical upper limit allowed in the worst case it is efficient.

 

Why leaf node is empty node:

In order to facilitate insertion and deletion, making the red-black tree leaf node is empty node (Null), it will be mentioned when talking about the insert.

 

operating

 

Guess you like

Origin www.cnblogs.com/guangluwutu/p/12070858.html