Std::set/std::map (hereinafter represented by Std::map) is a commonly used associative container, also a ADT (abstract data type). In other words, its interface (not OO interface) not only stipulates the function of the operation, but also stipulates the complexity of the operation (cost/cost). For example Set::insert (iterator, iterator last) is usually O (n log n), n is the length of the interval, but if [I, last] is sorted (in key_compare sense), then the complex The degree of impurity will be O (N).
Although the C + + standard does not require std::map the underlying data structure, but according to its prescribed time complexity, now all of the implementation of the STL using a balanced binary tree to achieve the std::map, and the use of red and black trees. Introduction to the Algorithm (2nd edition), 12th, 13 chapter introduced the two-fork search tree and red-black tree principle, nature, pseudo code, Mr. Houtie's "STL Source Analysis" 5th chapter of the SGI STL detailed analysis of the corresponding implementation. In this paper, the implementation of the red-black Tree (rb_tree) in STL asks a few questions, and gives my understanding. This article analyzes the g++ 4.7 from the implementation of the STL and its specific behavior, and the "STL Source analysis" version slightly different. For readability, the variable and class names in the text are slightly rewritten (for example, _rb_tree_node to Rb_tree_node). This article does not talk about the red and black tree balance algorithm, in my opinion this belongs to "the side branch distal tip" (see Chen Yu "Talk about network programming learning experience" to this definition), therefore also does not care about the node the specific color.
Data Structure Review
First, review the structure of the two-fork search tree in the data structure, nodes (node) typically have three data members (left, right, data), and trees (tree) have one or two members (root and Node_count).
In
Python:
class Node:
def __init__ (self, data):
Self.left = none
Self.right = None
Self.data = Data
class Tree:
def __init__ (self):
self.root = None
self.node_count = 0
In fact, the structure of the STL Rb_tree is slightly more complicated than this, and I've sorted the code to see HTTPS://GIST.GITHUB.COM/4574621#FILE-TREE-STRUCTURE-CC.
Node
Node has 5 members, except left, right, data, and color and parent.
C + + implementation, located in Bits/stl_tree.h
/**
* non-template code
**/
enum Rb_tree_color {kred, kblack};
struct Rb_tree_node_base
{
rb_tree_color color_;
rb_tree_node_base* Parent_;
rb_tree_node_base* Left_;
rb_tree_node_base* right_;
};
/**
* Template code
**/
template<typename value>
struct rb_tree_node:public rb_tree_node_ Base
{
Value value_field_;
};
See the figure below.
The existence of color is well understood, red-black tree Each node is not red or black, you need to preserve its color (color only needs 1-bit data, a memory-saving optimization measure is to embed color into the highest or lowest bit of a pointer, the Linux kernel rbtree is embedded to the lowest bit of parent); PA The existence of rent makes the non recursive traversal possible, and it will be discussed later.