5 of the study notes series in STL source code profiling-associated containers (1)

Source: Internet
Author: User

RB-tree (Red/black tree)

1.1Introduction

RB-tree is a binary search tree. The key value of a node must be greater than the key value of its left child node and smaller than the key value of its right child node. RB-tree has the following four features:

1. Each node is not black or red.

2. The root node is black.

3. If the node is red, its subnodes must be black.

4. Any path from any node to null (that is, tail end) must contain the same number of black nodes.

The rules above show that the newly added node must be red and the parent node of the newly added node must be black. If the newly inserted node does not meet the preceding conditions, you need to adjust the tree to conform to the RB-tree conditions. As shown in:

 

1.2 data structure of nodes

The RB-tree node and Its iterator are divided into two layers, each of which has a base class and a subclass. The Node Code is as follows:

Typedef bool _ rb_tree_color_type;

Const _ rb_tree_color_typen _ rb_tree_red = false;

Const _ rb_tree_color_typen _ rb_tree_black = true;

Struct _ rb_tree_node_base

{

Typedef _ rb_tree_color_typecolor_type;

Typedef _ rb_tree_node_base * base_ptr;

Color_type color;

Base_ptr parent;

Base_ptr left;

Base_ptr right;

}

 

Template <class value>

Struct _ rb_tree_node: Public _ rb_tree_node_base

{

Typedef_rb_tree_node <value> * link_type;

Value value_field;

}

 

A distinctive feature of Rb-tree is its header node and root node. The two are parent and child nodes. As shown in:


1.3 Basic Function Methods

The main operations of Rb-tree are insert, delete, and search. There are two insert cases. The difference is whether the same key value is allowed. Tree adjustment, left-hand and right-hand operations exist at the same time.

1. insert_unique (): insert operations. Duplicate node key values are not allowed.

2. insert_equal (): insert operations. Duplicate node key values are allowed.

3. _ rb_tree_rebalance (): change the color and rotate the tree.

4. _ rb_tree_rotate_left () \ _ rb_tree_rotate_right (): Left-hand and right-hand functions.

5. leftmost (): returns the leftmost element, the element with the smallest key value in the tree, header-> left

6. rightmost (): returns the rightmost element, the element with the largest key value in the tree, header-> right

 

1.4. Set \ Multiset

Both set and Multiset use RB-tree as the underlying data structure, and both use RB-tree related interface functions to implement their own functions. In addition, the two have the automatic sorting function ..The key value and real value are the same.

The only difference between the two is that the operation of the Set element inserts uses the underlying mechanism RB-tree insert_unique (), while the latter uses insert_equal ().

1.5 map \ multimap

Both map and multimap use RB-tree as the underlying data structure, and both use RB-tree related interface functions to implement their own functions. The two have the automatic sorting function. The default value is incremental sorting.The key value and real value are different.

The only difference between the two is that map inserts elements using the underlying mechanism RB-tree insert_unique (), while the latter uses insert_equal ().

5 of the study notes series in STL source code profiling-associated containers (1)

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.