What is the difference between a red and black tree and a balanced binary tree? _ Two Fork Tree

Source: Internet
Author: User
If the balanced binary tree is a class, then the red-black tree is an example of the class.
Algorithm of the book I lost a long time, I can not find, I was based on memory said. The red-black tree algorithm is troublesome, but its thought is very good, if understands its thought also understands its algorithm, I also only remembers the thought, the concrete algorithm remembers. I'm just going to talk about thinking here.

The red-black tree has two important properties:
1, the child node of the red node can not be a red node;
2, as many as the number of black nodes on any path from the root to the front-end node.

These two properties ensure that the height of the tree is Logn, so it is a balanced tree.

Red and black trees are the color of each node of the tree, node color or red or black, red-black tree is a lookup tree. The red-black tree has an important character, the longest path from the root node to the leaf node is no more than twice times the length of the shortest path. For red and black trees, inserting, deleting, and finding the complexity are all O (log N).

The inserted node is always set to a red node, when its complex node is a red node, this has a destructive nature of 1, you need to adjust. The insertion node is used as a study node to examine his uncle, if it is also a red node, to change his father and uncle to a black node, and his grandfather to a red node, and then to his grandfather as a study node. If his uncle is a black node to do a rotation transformation can be done, no picture is not very good description, you can think about it, in short, its idea is to "more out" of the red to the upper layer, to ensure that the following layer of red-black nature, finally pushed to the root, if still violate the nature of 1, you can directly to the red to Black can be changed, It is the equivalent of pushing this "extra out" red to a node outside the tree.

When you delete a node, you need to find the replacement node, and if the deleted node is black, it destroys the property 2 and needs to be adjusted. The idea of adjustment is similar to the previous one, which gives the replacement node the black, then the replacement node has a double black, then pushes its double black up, pushes it to the root and pushes it from the root to the outside.


The adjustment algorithm of the balanced binary tree almost all the data structure of the book has Ah, according to the destruction of the balance of the insertion point, will be inserted into the operation of four kinds, are ll, LR, RR, RL. Each type of insertion position that causes an imbalance has a corresponding adjustment algorithm.




The AVL tree, also known as the highly balanced two-fork search tree, was proposed in 1962 by two Russian mathematicians G.m.adel ' Son-vel,sky and E.m.landis. The purpose of introducing binary tree is to improve the efficiency of two-fork tree searching, and to reduce the average searching length of the tree. You must adjust the tree's structure every time you insert a node into a binary tree so that the binary tree search remains balanced, which may reduce the height of the tree and reduce the search length of the average tree.

Definition of AVL tree:

An AVL tree satisfies the following conditions:

1> its Saozi right subtree is an AVL tree.

The height difference of the 2> Saozi right subtree cannot exceed 1 from the condition 1 may be seen as a recursive definition, as in GNU.


Properties:

The 1> of an AVL tree of n nodes remains at 0 (log2 (n)) and does not exceed 3/2log2 (n+1)

2> The average search length of an AVL tree of n nodes remains at 0 (log2 (n)).

3> an N-node AVL tree Deletes a node to do the balancing rotation takes 0 (log2 (n)).


# The red-black tree is a very interesting balanced retrieval tree. Its statistical performance is better than that of a balanced binary tree (some books are called the avl-tree according to the author's name, Adelson-velskii and Landis), so the red-black tree is used in many places. In C + + STL, many parts (currently including set, Multiset, map, Multimap) apply the variant of the red-black tree (there are some changes in the red-black tree in the SGI STL, which provide better performance and support for set operations).
#
# The red-black tree is defined as follows:
#
#
# Two-fork search tree is a red-black tree that satisfies the following conditions
#
# * Each node is either "Red" or "black" (which will be explained later).
# * All the leaf nodes are empty nodes and are "black"
# If a node is ' red ', then its two sub nodes are ' black '
# * (Note: That is, if the end is black, its sub nodes can be red or black).
# * Each simple path to its descendants ' nodes contains the same number of "black" nodes.
# * The root knot is always "black"



AVL trees are highly balanced, frequently inserted and deleted, causing frequent reblance, resulting in decreased efficiency
Red-black tree is not a highly balanced, is a compromise, insert up to two rotation, delete up to three times rotation

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.