Red and black Trees

Source: Internet
Author: User

One about R-b Tree

R-b Tree, the full name is Red-black tree, also known as the "red black Tree", it is a special two-fork search trees. Each node of the red-black tree has a storage bit that represents the color of the node, either red (red) or black.

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.
(4) If a node is red, its child nodes must be black.
(5) The same number of black nodes are included on all paths from one node to the descendant nodes of the node.

Note :
1, the leaf node in the feature (3) is a node that is only empty (nil or null).
2, characteristics (5), ensure that no path will be two times longer than the other path. Thus, the red-black tree is relatively close to the balance of the two-fork tree.

Two the rotation of the red and black trees

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 Linux virtual memory management in TreeSet and treemap,c++ STL in the Java collection are implemented by red-black trees.

Theorem: A red-black tree with n nodes has a height of up to 2log (n+1).

The time complexity of the red-black tree is: O (LGN).

The basic operation of the red and black tree is to add, delete. The rotation method is used after the red-black tree is added or deleted. Why is it? The truth is simple, add or remove the red and black tree nodes, the red and black trees have changed, may not meet the red black Tree 5 properties, it is no longer a red black 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 . Each of them is described below.

1, l

A left-hand spin on X means "turn X into a left node".

For example:

2, right-handed

Right-clicking on X means "turning X into a right node".

For example:

Three Red and black tree additions and deletions 1. Add

What steps do you need to take to insert a node into a red-black tree?

The first step: the Red and black tree as a binary search tree, the node inserted.

Step Two: Colorize the inserted node as "red".

The third step: through a series of rotation or coloring and so on, so that it becomes a red black tree.

 

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.