Introduction to algorithms CLRS algorithm C ++ implementation (11) p163 red/black tree

Source: Internet
Author: User

Chapter 2 red and black trees

First, let's take a look at the incorrect graph in this chapter. The correct graph on the p168 page is shown below (the Chinese version of case3 is incorrect ):

 

In Linux kernel and STL, the data structure of the red and black trees is widely used, because the search, insertion, and deletion efficiency of the red and black trees are high.

The red/black tree is a binary search tree, but a storage bit is added to each node to indicate the color of the node, which can be red or black ).

A red/black tree has the following properties:

(1) Each node is either red or black.
(2) The root node is black.
(3) Each leaf node, that is, the null node (NiL) is black.
(4) If a node is red, the two sons are black.
(5) For each node, all paths from the node to its child node contain the same number of black nodes.

Each node in the tree has five fields: color, key, left, right, and P. The height of a red-black tree with N inner nodes is 2lg (n + 1 ).

Operations on the red/black tree:

1. Rotate

When you insert or delete a tree in the red/black tree, the red/black nature of the tree will be damaged. To maintain these properties, we need to change the color of some knots in the tree and the pointer structure. The pointer structure is modified by rotation. Rotation is a local operation that maintains the infix order of nodes in the tree. There are two rotation methods: left and right.

2. Insert

Case 1: As shown in, X is the newly inserted node and red. P is the parent node of X, which is also red. G is the grandfather node of X, and s is the uncle node of X. They are all black. At this time, X, P, and g are arranged in a straight line. In this case, the processing method is relatively simple. You only need to use a single rotation operation and adjust the color properly. First, perform a right single rotation, and then modify the G color.

Case 2: As shown in, X is the newly inserted node and red. P is the parent node of X, which is also red. G is the grandfather node of X, and s is the uncle node of X. They are all black. This is a line of X, P, and G. In this case, the processing method is to use a double rotation operation and adjust the appropriate color. First, perform a first-left-right double rotation. During the rotation, modify the colors of x and G.

Case 3: Case 1 and Case 2 are relatively simple, but if the new inserted node X's uncle node S is red, there may always be a "red-red node" regardless of how it is rotated. At this time, the adjustment and coloring layers are used.

As shown in, it is still relatively simple. A single right rotation solves the problem.

 

The situations shown are more complex. Figure A shows the process of inserting node 16. Because 16 and its parent node 17 are both red, it violates nature 3. And because 16's uncle node 22 is also red, it is case 3. The nodes are re-colored, and the pointer rises along the tree. The resulting tree is shown in B. In this way, node 21 and its father node are both red, but node 30 of node 21 is black. Because 21 is the right child of 15, you can use case 2 for processing. after performing a left rotation, the resulting tree C is returned. Execute a right-hand operation to obtain the tree in Figure D. It is a valid red/black tree, And the insert operation is complete.

 

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.