Data structures and algorithms-red and black trees

Source: Internet
Author: User

Objective

Red and black tree is the most commonly used in the engineering of a self-balanced binary sorting tree, which is similar to the AVL tree, are in the insertion, deletion through a certain adjustment operation to maintain a relatively stable tree height, so as to obtain better query performance.

Properties

1. The node is red or black.

2. The root node is black.

3 each leaf node (the null node) is black.

4 The two child nodes of each red node are black . (no two consecutive red nodes can be found on all paths from each leaf to the root)

5. all paths from any node to each of its leaves contain the same number of black nodes .

Maintain red and black tree shape (tree height), mainly is 4, 52 properties, Nature 4 determines the longest path is the red black interval path, the nature of 5 determines the shortest path is the whole black path, and each path on the number of black nodes are the same ( very critical ), Together, they determine that the longest path is up to twice times the shortest path.

Insert operation

the inserted node should be red because inserting a red node in the leaf node does not violate the nature of 5, and the Nature 5 is the hardest to maintain. Red and black trees need to be maintained 2, 4, and 5 after inserting the operation.

Case 1: The root node is inserted.

The original tree is an empty tree, this situation will only violate the nature of 2, directly turn this node back to black.

Case 2: The parent node of the inserted node is black.

This will not violate the nature of 2 and Nature 4, the red-black tree is not destroyed and nothing is done.

Case 3: The parent node of the insertion node is red and the Uncle node is red.

This is a violation of the nature of 4 (The new insertion node and the parent node constitute a continuous two red nodes), because the parent node and Uncle node is the same red, so the two can be set to black (at this time violated the nature of 5), and the grandfather node is red (before the grandfather node must be black) (followed by the nature of 5)

n is the insertion node

Before adjustment

After adjustment

Case 4: The parent node of the current node is red, the Uncle node is black, and the current node is the right child node of its parent node.

This situation is the most difficult to solve, because no matter how it can not be adjusted with a discoloration or a single rotation, this situation is usually used in conjunction with Case 5, first of all, the parent node of the insertion node is the axis left-handed.

Before adjustment

After adjustment

Case 5: The parent node of the current node is red, the Uncle node is black, and the current node is the left child node of its parent node.
In case 4, the parent node of the insertion node is black, the grandfather node is red, and the grandfather node is the right axis.

Before adjustment

After adjustment

Summarize

The red-black tree does not pursue "full balance"-it requires only a relative balance (while AVL is very strict about the balance, and the more effort is made), reducing the need for rotation and thus improving performance. The red-black tree can search, insert, and delete operations with the time complexity of O (log2 N). In addition, due to its design, any imbalance will be resolved within three rotations . Of course, there are better, but more complex data structures that can be balanced within one step of the rotation, but red-black trees can give us a "cheap" solution.

Reference: http://www.imooc.com/article/11715

Https://www.ibm.com/developerworks/cn/java/j-lo-tree/index.html

Data structures and algorithms-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.