"Java in-depth study" 10, red and black trees

Source: Internet
Author: User

I. Introduction of red and black trees

Red and black trees are two forks to find trees, the time complexity of red and black trees is: O (LGN)

Characteristics of red and black trees:
(1) Each node is either black or red.
(2) The root node is black.
(3) Each leaf node (NIL) is black. [Note: Here the leaf node is the leaf node that is empty (nil or null)! ]
(4) Each red node must have two black sub-nodes.

(5) All paths from any node to each of its leaf nodes contain the same number of black nodes

Red and black trees are widely used, mainly used to store ordered data, its time complexity is O (LGN), the efficiency is very high.
For example, the set, map, and Nginx, Linux virtual memory management in TreeSet and treemap,c++ STL in the Java collection are implemented by red-black trees.

Two, red black tree operation 1, L -And right-handed

After the red and black trees were added or deleted, the red and black trees changed, may not meet the red and black tree 5 properties, it is no longer a red black tree, but an ordinary tree. By rotating it, you can make the tree a red-black tree again. To put it simply, the purpose of rotation is to keep the tree characteristic of the red and black trees. The rotation consists of two types: Left and right .

After rotation, it is still a two-fork sort tree

2. Add

First, the red and black tree as a binary search tree, the node is inserted, then the node is colored red, and finally, by rotation and re-coloring and other methods to correct the tree, so that it re-become a red black tree.

Detailed steps

    • The first step: the Red and black tree as a binary search tree, the node inserted.
    • Colorize the inserted node as "red" (in order not to violate the fifth attribute)
    • When the parent node of the inserted node is red, it is re-made into a red-black tree through a series of operations such as rotation or coloring.

3. Delete

Delete one of the nodes in the red and black tree. The actions that need to be performed are: first, the red-black tree is treated as a binary lookup tree, and the node is removed from the two-fork lookup tree, and then the tree is corrected by a series of "rotation and recolor" to make it a red-black tree again. Detailed descriptions are as follows:

    • If you delete a leaf node, you can delete it directly.
    • If the deleted element has a child node, you can move the child node directly to the location of the deleted element
    • If there are two sub-nodes, this time can be deleted elements of the right branch of the smallest node (the right side of the deleted element is the leftmost node) and the deleted elements are swapped, we will be deleted element right branch of the leftmost node is called the successor (successor element)
    • When the deleted element (the element after swapping) is black, and a right child node is red, the right sub-node is blackened to the location of the deleted element
iii. comparison with AVL trees1, AVL tree is the first invention of the self-balanced binary search tree. An AVL tree satisfies the following conditions
    • Its Saozi right subtree is the AVL tree.
    • The height difference of Saozi right subtree cannot exceed 1
AVL's structure is more balanced than rb-tree, where inserting and deleting node is more likely to cause tree unbalance, so the AVL needs to rebalance more frequently when large amounts of data need to be inserted or deleted. As a result, rb-tree is more efficient in scenarios that require a lot of insertion and deletion of node. Naturally, AVL's search is more efficient due to the high balance of AVL. In general, the statistical performance of Rb-tree is higher than that of AVL.

Reference article:

Https://www.cnblogs.com/George1994/p/6903437.html

65445754

https://www.zhihu.com/question/20545708

Java in-depth study 10, 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.