"Introduction to Algorithms" Study notes--13th chapter red and black Trees

Source: Internet
Author: User

The red and black trees (red-black tree) are one of many balanced search trees, so the time complexity of basic operations (query, delete, search) and so on in the worst case is O (LGN).
13.1 The nature of red and black trees
The red and black tree is a binary search tree, and an attribute is added to each node to indicate the color: red or black. By constraining the color of each node on a simple path from the root to the leaf,
The red and black tree ensures that no path is twice times longer than the other path. Thus is approximate to the equilibrium.
A red and black tree is satisfied belowRed Black NatureTwo-fork Search tree:
1. Each node is red or black;
2. The root node is black;
3. Each leaf node (NIL) is black;
4. If a node is red, its two sub-nodes are black;
5. For each node, the same number of black nodes are included on the simple path from the node to all of its descendant leaf nodes. (black multiplying, etc.)
The number of black nodes on any simple path that reaches a leaf node from a node x is called the Black Height (black-height) of the node.

13.1-1
is not, if the node is red does not satisfy the nature of 4, if the node is black is not satisfied with the nature of 5.

13.1-2
Still a red and black tree, satisfying nature 1-5

13.1-3
This topic is not clear, in fact, the red node and the black Knot is merged together to form a new node. So
When the child nodes of the black parent node are all black, the degree is 2;
When the black parent node contains only one black sub-node, the degree is 3;
When the two child nodes of the black parent node are all red, the degree is 4.
Since the red nodes are all absorbed, the remaining black nodes are the only ones. By the nature of the red-black tree 5, the leaf node depth is the same.

13.1-4
This topic is not clear, in fact, the red node and the black Knot is merged together to form a new node. So
When the child nodes of the black parent node are all black, the degree is 2;
When the black parent node contains only one black sub-node, the degree is 3;
When the two child nodes of the black parent node are all red, the degree is 4.
Since the red nodes are all absorbed, the remaining black nodes are the only ones. By the nature of the red-black tree 5, the leaf node depth is the same.

13.1-5
It is possible to formalize the validation by considering that the black height of the node x is BH (x), the red height is RH (x), and the RH (x) <=BH (x). node x to descendant leaf knot
The longest path of the point is BH (x) +rh (x), and the shortest path is BH (x). Therefore, the maximum is twice times. Think about what the path would look like again.
Set the black height of x to H, when the color of x is black, the longest path is colored black-red-black-...-red-black-red-black, Length (2h-2), the shortest path is shaded black-black-...-black-black, length is (h-1), the ratio is 2.
When the color of x is red, the longest path is shaded as red-black-red-black-...-red-black/red-dark, Length (2h-1), the shortest path coloring is red-black-black-...-black-black, the length is H, the ratio tends to be nearly 2.
Therefore, the maximum ratio is 2.

13.1-6
The internal node is a maximum of 2^ (2k)-1, apparently a complete number, with each layer having a black-red alternating color.
The internal node is at least 2^k-1, apparently an all-black tree.

13.1-7
The ratio is maximum 2:1, and the ratio is minimum 0 (Park).

13.2 Rotation

"Introduction to Algorithms" Study notes--13th chapter 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.