Red and Black Tree Rb_tree

Source: Internet
Author: User

Red black tree is also a fork search tree, so binary search tree properties of red and black trees have, at the same time, we know that in order to avoid the worst case of the two-fork search tree (is the case of high imbalance) derived from the AVL tree, so that any node of the left and right sub-tree height difference of up to 1, thereby achieving To ensure the worst-case search efficiency. Of course, the red and black trees in order to better search efficiency to reduce the demand for balance, but the red and black trees still have a good balance.

AVL tree and Rb_tree

The AVL tree is also called a height-balanced tree, its insertions, deletions, and lookups in the average and worst case are O (log n), adding and removing to rebalance the tree by one or more tree rotations.

Rb_tree does not pursue a complete balance, requiring only partial balance requirements, reduced rotation requirements, and improved performance, and the red-black tree can be inserted, deleted, and found with the time complexity of O (log 2 n), as well as any imbalance in its design will be resolved within 3 rotations. In contrast, the algorithms of red-black and AVL trees have the same time complexity, but the statistical performance is higher than the AVL tree. At the same time, the use of red and black trees is wider than AVL trees

For example: (1) Red-black tree is widely used in C + + STL, such as associative container set, Map,multiset, Multimap are rb_tree as the underlying mechanism.

(2) The implementation of Epoll in the kernel, with red black tree management event block.

(3) Linux process scheduling, with red black tree management Process Control block.

(4) using red and black tree to manage timer in Nginx

Analysis of the Stl_tree.h source code in SGI

#ifndef __sgi_stl_internal_tree_h
#define __sgi_stl_internal_tree_h
Node design of Rb-tree
Design ideas node design is divided into two layers __rb_tree_node inherit __rb_tree_node_base to achieve the separation of pointers and data
typedef BOOL __rb_tree_color_type;
Const __rb_tree_color_type __rb_tree_red = false;
Const __rb_tree_color_type __rb_tree_black = true;

struct __rb_tree_node_base
{
typedef __rb_tree_color_type COLOR_TYPE;
typedef __rb_tree_node_base* BASE_PTR;
Color_type color;
Base_ptr parent;
Base_ptr left;
Base_ptr right;
Find the minimum stored value
Static base_ptr minimum (Base_ptr x)
{
Wile (X->left! = 0) {
x = x->left;
}
return x;
}
Static Base_ptr maximum (Base_ptr x)
{
while (x->right! = 0) {
x = x->right;
}
return x;
}
};

Template<class value>
struct __rb_tree_node:public __rb_tree_node_base
{
typedef __rb_tree_node<value>* LINK_TYPE;
Value Value_field;
};
The structure design of the Rb_tree iterator is also a double-layered iterative structure
struct __rb_base_iterator
{
typedef __RB_TREE_NODE_BASE::BASE_PTR BASE_PTR;
typedef bidirectional_iterator_tag Iterator_category;
typedef ptrdiff_t DIFFERENCE_TYPE;
BASE_PTR node;
The forward operation of the iterator operator++ () invokes the increment () of the base iterator
(1) If node has a right subtree, but the right subtree does not have a left child node, then node advances to node->right. If the right subtree has a left dial hand tree, the node behind nodes is the leftmost node in the left subtree.
(2) If node does not have a right subtree, and node is the left subtree of node->parent, then node behind nodes is node->parent. If node is the right subtree of node->parent, it is always up until it is no longer the right child node, such as Y, and the node behind nodes is the Y->parent node.

void Increment ()
{
if (node->right! = 0) {
node = node->right;
while (Node->left! = 0) {
node = node->left;
}
}
else{
Base_ptr y = node->parent;
while (node = = y->right) {
node = y;
y = y->parent;
}
if (node->right! = y) {
node = y;
}
}
}
///iterator back Operation operator--() call base iterator Ecrement ()
(1) node has a left dial hand tree, and the left subtree of node does not have a right subtree, so nodes in front of it are node->left. If node's left subtree has a right subtree, the node in front of it is the rightmost node of the right subtree.
(2) node does not have a left subtree, and node is the left node of its parent node, then it is the node->parent in front of nodes, which is not the left node of node->parent. If it is the right node of its parent node, then the node in front of it is node->parent.

void Decrement ()
{
To judge that node is the header, the virtual node
if (Node->color = = __rb_tree_red && node->parent->parent = = node) {
node = node->right;
}
else if (node->left! = 0) {
Base_ptr y = node->left;
while (y->right! = 0) {
y = y->right;
}
node = y;
}
else{
Base_ptr y = node->parent;
while (node = = Y->left) {
node = y;
y = y->parent;
}
node = y;
}
}
};

TEMPLATE&LT;CALSS Value, Class Ref, class ptr>
struct __ Rb_tree_iterator:public __rb_tree_base_iterator
{
typedef Value VALUE_TYPE;
typedef REF Reference;
typedef PTR Pointer;
typedef __rb_tree_iterator<value, Value&, value*> iterator;
typedef __rb_tree_iterator<value, const VALUE&AMP;, const value*> const_iterator;
typedef __rb_tree_iterator<value, REF, ptr> self;
typedef __rb_tree_node<value>* LINK_TYPE;

__rb_tree_iterator () {}
__rb_tree_iterator (Link_type x) {node = x;}
__rb_tree_iterator (const iterator& it) {ode = It.node;}

self& operator++ () {increment (); return *this;}
Self operator++ (int) {
Self tmp = *this;
Increment ();
return TMP;
}

self& operator--() {decrement (); return *this;}
Self operator--(int) {
Self tmp = *this;
Decrement ();
return TMP;
}
};

inline bool operator== (const __rb_tree_base_iterator& x,
Const __rb_tree_base_iterator& y)
{
return X.node = = Y.node;
}

inline bool operator!= (const __rb_tree_base_iterator& x,
Const __rb_tree_base_iterator& y)
{
return x.node! = Y.node;
}
Rb_tree Data structure
Template <class Key, class Value, Class Keyofvalue, class Compare,
Class Alloc = alloc>
Class Rb_tree {
Protected
typedef void* Void_pointer;
typedef __rb_tree_node_base* BASE_PTR;
typedef __rb_tree_node<value> Rb_tree_node;
typedef simple_alloc<rb_tree_node, alloc> Rb_tree_node_allocator;
typedef __rb_tree_color_type COLOR_TYPE;
Public
typedef Key KEY_TYPE;
typedef Value VALUE_TYPE;
typedef value_type* Pointer;
typedef const VALUE_TYPE* Const_pointer;
typedef value_type& Reference;
typedef const value_type& Const_reference;
typedef rb_tree_node* LINK_TYPE;
typedef size_t SIZE_TYPE;
typedef ptrdiff_t DIFFERENCE_TYPE;
Public
Rb_tree (const compare& comp = Compare ())
: Node_count (0), Key_compare (comp) {init ();}

~rb_tree () {
Clear ();
Put_node (header);
}
Rb_tree<key, Value, Keyofvalue, Compare, alloc>&
operator= (const Rb_tree<key, Value, Keyofvalue, Compare, alloc>& x);
Protected
Get_node () {return rb_tree_node_allocator::allocate ();}
void Put_node (Link_type p) {rb_tree_node_allocator::d eallocate (P);}

Link_type Create_node (const value_type& x) {
Link_type tmp = Get_node ();
Put_node (TMP);
return TMP;
}
Copy a node
Link_type Clone_node (Link_type x) {
Link_type tmp = Create_node (X->value_field);
Tmp->color = x->color;
Tmp->left = 0;
tmp->right = 0;
return TMP;
}

void Destroy_node (Link_type p) {
Destroy (&p->value_field);
Put_node (P);
}

Protected
Size_type Node_count; Keeps track of size of tree
Link_type header;
Compare Key_compare;
//These three functions make it easy to get a member of the header
link_type& root () const {return (link_type&) header->parent;}
Get the leftmost node
link_type& leftmost () const {return (link_type&) header->left;}
Get the rightmost node
link_type& rightmost () const {return (link_type&) header->right;}
///These six functions make it easy to get the members of Node X (self-made parameters) themselves to these 12 functions do not specifically understand why write two kinds of
Static link_type& Left (Link_type x) {return (link_type&) (x->left);}
Static link_type& Right (Link_type x) {return (link_type&) (x->right);}
Static link_type& parent (Link_type x) {return (link_type&) (x->parent);}
Static reference value (Link_type x) {return x->value_field;}
static const key& Key (Link_type x) {return keyofvalue () (Value (x));}
static color_type& color (Link_type x) {return (color_type&) (X->color);}
These six functions make it easy to get the members of Node X (parameters in the parent type's node)
Static link_type& Left (Base_ptr x) {return (link_type&) (x->left);}
Static link_type& Right (Base_ptr x) {return (link_type&) (x->right);}
Static link_type& parent (Base_ptr x) {return (link_type&) (x->parent);}
Static reference value (Base_ptr x) {return ((link_type) x)->value_field;}
static const key& Key (Base_ptr x) {return keyofvalue () (Value (Link_type (x)));}
static color_type& color (Base_ptr x) {return (color_type&) (Link_type (x)->color);}

Static Link_type minimum (Link_type x) {
Return (Link_type) __rb_tree_node_base::minimum (x);
}
Static Link_type maximum (Link_type x) {
Return (Link_type) __rb_tree_node_base::maximum (x);
}

Public
typedef __rb_tree_iterator<value_type, reference, pointer> iterator;
typedef __rb_tree_iterator<value_type, Const_reference, const_pointer>
Const_iterator;



};
#endif

Because the content is more, has not finished finishing, tomorrow will continue to carry out the source analysis.

Red and Black Tree Rb_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.