Balance binary tree and red/black tree

Source: Internet
Author: User

AVL Tree definition:
An AVL Tree meets the following conditions:
1> both the left and right subtree are AVL trees.
2> the height difference between the left subtree and the right subtree cannot exceed 1
It can be seen from condition 1 that it is a recursive definition, like GNU.

Nature:
1> the height of an AVL Tree with N nodes is kept at 0 (log2 (N) and does not exceed 3/2log2 (n + 1)
2> the average search length of an AVL Tree with N nodes is kept at 0 (log2 (n )).
3> the AVL Tree with N nodes deletes a node. The time required for balancing the rotation is 0 (log2 (n )).

 

So how can we make the generated binary tree a balanced binary tree? In fact, there is only one sentence: adjust each node to make it even. When it comes to adjustment policies, there are four adjustment methods for different reasons for the imbalance of the binary sorting tree. Next, we will analyze them one by one: First, we assume that the root node of the smallest subtree in the binary sorting tree that is out of balance due to the insertion of nodes is a, that is, the absolute value of the balance factor of A is greater than 1, and no other node is out of balance between node A and the root node (because there is only one inserted node, so there is no balance between the child tree of the brother node ).
First case: ll-type balanced rotation
As a node is inserted in the left Tree of the Left subtree of A, the balance factor of A is increased from 1 to 2, and the tree is out of balance. The adjustment method is to rotate subtree a clockwise once.
Case 2: RR balanced rotation
In fact, this is symmetric with the first LL type, because the insertion of nodes in the right subtree of a increases the balance factor of a from 1 to 2; the solution is similar, as long as a counter-clockwise rotation is performed.
Case 3: LR balanced rotation
This situation is a little complicated. A node is inserted on the right subtree C of the Left subtree of A, causing imbalance. However, whether the left or right subtree inserted in C does not affect the solution. You only need to perform two rotations (clockwise first) to restore the balance.
Case 4: RL balanced rotation
It is also LR-type image symmetry, which is caused by the knots inserted on the left Tree of the right subtree of A. It also needs to be rotated twice (clockwise first and then counterclockwise) to restore the balance.

 

Similar to the AVL Tree mentioned earlier, the red/black tree maintains a balance between the binary search tree through specific operations during the insert and delete operations to achieve high search performance. Since the emergence of the red/black tree, the AVL tree has been put in the museum. It is said that the red/black tree has better efficiency and higher statistical performance. However, after I understand the implementation principles of the red and black trees, I don't believe this is true. We will discuss this in the future.

The difference between a red-black tree and an AVL Tree is that it uses color to identify the height of a node. It pursues a local balance rather than a very strict balance in the AVL Tree. We have learned about the complexity of the AVL Tree before, but the complexity of the AVL Tree is nothing more than that of the red and black trees. The red/black tree is a real abnormal data structure.

Balance of red and black trees
The red/black tree is a binary search tree. Each node is marked with a color (red or black). The red/black tree meets the following five properties:

1. The color of each node can only be red or black.

2. The root node is black.

3. Each leaf node has two empty black nodes (known as the Black Sentinel). If one node N has only one left Child, the right child of N is a black Sentinel; if node N has only one right child, the left child of node N is a black Sentinel.

4. If a node is red, both of its sons are black. That is to say, there cannot be two adjacent red nodes in a path.

5. For each node, all paths from the node to its child leaf node contain the same number of black nodes.

Of the five properties of the red and black trees, the 3rd points are hard to understand, but they are very necessary. We can see the picture on the left in Figure 1. If the black Sentinel is not used, it is completely red-black, nodes 50 to two leaf nodes 8 and leaf node 82 have two black knots. However, if the number of leaf nodes is changed to 8 after the black Sentinel is added (the small black dot in the right figure, the black height of the root node 50 to the eight leaf node paths is different, so it is not a red-black tree.

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.