Introduction to Algorithms-------------red and black trees

Source: Internet
Author: User

A red-black tree is a binary lookup tree, but adds a storage bit to the node's color at each node, either red or black. The red-black tree ensures that no path will be twice times longer than the other paths, and thus is nearly balanced, by limiting the shading of any one path from root to leaf. This chapter mainly introduces the nature, rotation, insertion and deletion of red and black trees. The process of inserting and deleting elements in red and black trees is analyzed in detail. A two-fork search tree with a height of H can implement any kind of basic dynamic set operation, such as search, predecessor, successor, Mimmum, Maxmum, INSERT, delete, etc. These operations perform faster when the height of the binary lookup tree is low, but the performance of these operations may not be as good as the list when the tree height is high. The Red-black tree is a balanced two-fork lookup tree that ensures that, in the worst case scenario, the basic dynamic operation set runs at O (LGN). The content of this chapter is a bit complicated, it takes two days to get a clear idea of the insertion and deletion process, and it needs to be reviewed frequently to get it completely digested. The use of red and black trees is very extensive, such as the map in the STL is implemented with red and black trees, the efficiency is very high, there is a chance to study the source code of STL.

Did not say, looked at two days of the algorithm introduction, finally the red and black tree realized, for the rest of my life better, I also pretty spell.

5 important properties of the red and black trees:

(1) Each knot is either red or black.

(2) The root node is black.

(3) Each leaf node (NIL) is black.

(4) If one of the nodes is red, then its two sons are black.

(5) For each node, the same number of black nodes are included on all paths from the node to its grandson node.



Lemma: The height of a red-black tree with N nodes is 2lg (n+1).

There is also an important property: The red-black tree will ensure that no path is twice times longer than other paths.

Some of the red and black tree operations in the introduction of the algorithm has a detailed explanation, and the reasoning process, speak very carefully, it takes a certain amount of time to understand, I struggled for 2 days, weekend holidays are staying at home did not go out, this is to do a yard life.

Write a good night for a few hours in the debugging code, because of their carelessness, in the code symmetry operation, there is a place around did not change to cause me to change a few hours, NND,.

time:2014.12.8 0:22//author:ugly_chen (Stallman)//csdn:http://blog.csdn.net/chenxun_2010//********************************************************** /#include <iostream> #include <stack>using namespace std;static const int RED = 0;static const int Black = 1;template <class t>class red_black_tree_node{public:red_black_tree_node (T k): Key (k), Color (black), p ( NULL), Left (null), right (null) {}t key;int color; red_black_tree_node<t>* p; Red_black_tree_node<t>* left; red_black_tree_node<t>* right;}; Template <class T>class red_black_tree{public:red_black_tree (); red_black_tree_node<t>* get_root () const;//Get root node red_black_tree_node<t>* search_tree_node (const T& k ) const;//Find the node with the keyword K red_black_tree_node<t>* tree_maxmum (red_black_tree_node<t> *root) const;// Find the node with the largest keyword red_black_tree_node<t>* tree_minmum (red_black_tree_node<t> *root) const;//Find the mostnode of the small keyword red_black_tree_node<t>* tree_successor (red_black_tree_node<t> *pnode) const;//Find subsequent nodes Red_black_ tree_node<t>* tree_predecessor (red_black_tree_node<t> *pnode) const;//Find money secondary node void Left_rotate (Red_Black _tree_node<t> *pnode);//left rotation operation void Right_rotate (red_black_tree_node<t> *pnode);//Right rotation operation void Rb_insert_ Fixup (red_black_tree_node<t> *pnode);//fix the behavior of red-black tree violation caused by insert operation void Rb_delete_fixup (red_black_tree_node<t > *pnode);//Modify the behavior of the delete operation caused by the violation of the nature of the red-black tree void Rb_insert (red_black_tree_node<t>* z);//Insert node void Rb_delete (Red_black_ tree_node<t>* z);//delete node void Inorder_tree_walk () const;//in sequence traversal red-black tree: This is the order from small to large output red black tree void Make_empty (red_black_ tree_node<t>* root);//~red_black_tree ();p ublic:staticred_black_tree_node<t> *NIL;private:Red_Black_ tree_node<t>* root;};/ /Initialize niltemplate <class t>red_black_tree_node<t>* red_black_tree<t>::nil = new Red_Black_Tree_Node <T> (-1);//Initialize the root node with the default constructor template <class T>red_black_tree<t>::red_black_tree () {root = nil;};/ /---------------------------------------------------------------------------------------Template<class t> red_black_tree_node<t>* red_black_tree<t>::get_root () const//Gets the root node {return root;}  ------------------------------------------------------------------------------------------//Find the node template for the keyword key in the tree <class t>red_black_tree_node<t>* red_black_tree<t>::search_tree_node (const T& key) Const{Red_ black_tree_node<t>* p = root;while (P! = NIL) {if (P->key = = key) Break;else if (P-key > key) p = P->lef T;elsep = P->right;} return p;} ----------------------------------------------------------------------------------------//Find the node template for the maximum keyword <class t>red_black_tree_node<t>* red_black_tree<t>::tree_maxmum (Red_Black_Tree_Node<T> * root) const{red_black_tree_node<t>* p = root;while (p->right! = NIL) {p = p->right;} return p;} //------------------------------------------------------------------------------------------//Find the node of the minimum keyword in the number template<class t>red_black_tree_node< T>* red_black_tree<t>::tree_minmum (red_black_tree_node<t>* root) const{red_black_tree_node<t >* p = root;while (p->left! = NIL) {p = p->left;} return p;} ----------------------------------------------------------------------------------------------//Find successor nodes: The smallest point greater than x Template<class t>red_black_tree_node<t>* red_black_tree<t>::tree_successor (Red_Black_ tree_node<t>* x) const{if (x->right! = NIL) return tree_minmum (x->right); Red_black_tree_node<t>* y = x->p;while (Y! = NIL &&x = = y->right) {x = Y;y = y->p;} return y;} --------------------------------------------------------------------------------------------------//Find precursor node:// Find the precursor of x in the middle sequence, that is, the maximum value less than x template<class t>red_black_tree_node<t>* red_black_tree<t>::tree_ Predecessor (red_black_tree_node<t> *x) const{if (x->left! = NIL) return treE_maxmum (X->left); Red_black_tree_node<t>* y = x->p;while (Y! = Nil&&x = = y->left) {x = Y;y = y->p;} return y;} ----------------------------------------------------------------------------------------------------//Left rotate template <class t>void red_black_tree<t>::left_rotate (red_black_tree_node<t> *x) {Red_Black_Tree_Node< t> *y = X->right;x->right = y->left; if (y->left! = nil) Y->left->p = X;y->p = x->p;if (X->p = nil) root = Y;else if (x = = x->p->left) x >p->left = Y;elsex->p->right = Y;y->left = X;x->p = y;} Right rotation//------------------------------------------------------------------------------------------------------ Template<class t>void red_black_tree<t>::right_rotate (red_black_tree_node<t> *x) {Red_Black_Tree _node<t> *y = X->left;x->left = Y->right;if (y->right! = NIL) y->right->p = X;y->p = X->p;if (X->p = NIL) root = Y;else if (x = = X->p-> left) X->p->left = Y;elsex->p->right = Y;y->right = X;x->p = y;} --------------------------------------------------------------------------------//Insert sub-procedure: Fix a node template that might violate a red-black tree after insertion <class t>void red_black_tree<t>::rb_insert_fixup (red_black_tree_node<t>* z) {Red_Black_Tree_Node <T> *y;while (Z->p->color = = RED) {if (z->p = = z->p->p->left)//Z.P is Z grandfather's left child {y = z->p->p- >right;if (Y->color = = red)//case1 Z's tert-node is red {z->p->color = Black;y->color = black;z->p->p-> color = Red;z = z->p->p;} else//case 2 or Case 3: The tertiary node is black {if (z = = z->p->right)//z is right child {z = Z->p;left_rotate (z);} Case3 Z for left child z->p->color = Black;z->p->p->color = Red;right_rotate (z->p->p);}} else if (z->p = = Z->p->p->right)//Z.P is the right child of Z-grandfather, and there are three cases in which the same is the case {y = z->p->p->left;if (Y->color = = RED) {Z->p->color = Black;y->color = Black;z->p->p->color = Red;z = z->p->p;} Else{if (z = =Z->p->left) {z = Z->p;right_rotate (z);} Z->p->color = Black;z->p->p->color = Red;left_rotate (z->p->p);}}} Root->color = BLACK;} -------------------------------------------------------------------------------------------//fix Delete rb_ The behavior of a tree node that may violate the nature of the red-black tree Template<class t>void red_black_tree<t>::rb_delete_fixup (red_black_tree_node <T> *x) {red_black_tree_node<t> *w;//If this extra black is on a root node or a red junction, the knot will absorb the extra black and become a black node while (x! = root& &x->color = = BLACK) {//If x is the left node of its parent (relative to the right node) if (x = = X->p->left) {//Make W x brother, according to W, divided into three cases to handle// Before performing the delete operation X is definitely no brother, after performing the delete operation X must have brother W = x->p->right;if (W->color = = red)//Case 1 W for red {w->color = black;x->p- >color = Red;left_rotate (x->p); w = x->p->right;//In this case can enter 2 3 4 any kind of situation}if (W->left->color = BLACK && W->right->color = = black)//case 2:w, W's two children are black {w->color = Red;x = x->p;} Else//case 3 W is black, W->left is red, W->right is black {if (w->right->cOlor = = BLACK) {W->left->color = Black;w->color = Red;right_rotate (w); w = x->p->right;} Case 4:w is black, w->left can be red and can be black, w->right is red//modify W and x.p color W->color = X->p->color;x->p->color = Black;w->right->color = Black;left_rotate (x->p); x = root;}}  else if (x = = X->p->right)//x is the right child of its parent node {w = x->p->left;  if (W->color = = red)//case1,w.color is red {w->color = Black;x->p->color = red; right_rotate (x->p); W = x->p->left;//The brother of X to 2.3.43 cases} if (W->right->color = = BLACK && W->left->color = = BLA CK)//case2:w is black, two children of W are also black {w->color = Red;x = x->p;} Else//case3:w is black, w->left black, w->right is red (contrary to above: W->left is red, W->right is black) {if (W->left->color = =  BLACK) {W->right->color = Black;w->color = RED; Left_rotate (w); W = x->p->left;////The new brother of X, enter Case4}//case4:w is black, W->left is red, w->right can be red or black.//Modify W and p[x] Color W >color = X->p->color;x->p->coLor = Black;w->left->color = BLACK; Right_rotate (x->p);//When the adjustment is finished, the X is set to the root node in order to end the loop x = root;}} X->color = When the Black;//while loop ends, the color of x is red to absorb black.} ---------------------------------------------------------------------------------------------//insert operation: rb_ Inserttemplate<class t>void Red_black_tree<t>::rb_insert (red_black_tree_node<t>* z) {Red_Black_ Tree_node<t>* y = NIL;  red_black_tree_node<t>* x = root;while (x! = NIL)//Find the inserted position {y = x;if (Z->key < x->key) x = X->left;elsex = X->right;} Z->p = y;if (y = NIL) root = Z;else if (Z->key < Y->key) Y->left = Z;elsey->right = Z;z->left = nil;z- >right = Nil;z->color = Red;rb_insert_fixup (z);} -------------------------------------------------------------------------------//Node delete operation: Rb_deletetemplate <class t>void red_black_tree<t>::rb_delete (red_black_tree_node<t>* z) {Red_Black_Tree_Node<T >* y; red_black_tree_node<t>* x;if (Z->left = = NIL | | z-> right = nil) y = z;else y = tree_successor (z); if (y->left! = nil) x = y->left;else x = y->right;x->p = Y->p if (y->p = = NIL) root = X;else if (y = = y->p->left) Y->p->left = X;elsey->p->right = X;if (Y! = z) Z-&G T;key = y->key;//If the deleted node is black, you need to adjust if (Y->color = = Black) rb_delete_fixup (x);} -----------------------------------------------------------------------------------------//Middle sequence traverse red black tree, Use stack's function from small to large output red black tree template <class t>void red_black_tree<t>::inorder_tree_walk () const{if (NULL! = root) { stack<red_black_tree_node<t>* > S; red_black_tree_node<t> *p;p = Root;while (nil! = P | |!s.empty ()) {if (nil! = p) {s.push (p);p = P->left;} Else{p = S.top (); S.pop () cout << p->key << ":"; if (P->color = black) cout << "Black" << Endl ; Elsecout << "Red" << endl;p = P->right;}}} int main () {red_black_tree<int> Tree; int s[6] = {8, 0, +, 6,,};int i;for (i =; i<; i++) {Red_black_tree_node<int> *z = new red_black_tree_node<int> (s[i]); Tree.rb_insert (z);} cout << "root node is:" << tree.get_root ()->key << endl;tree.inorder_tree_walk (); int s2[10] = {8, 12, 20, 38 , 48,666,999,1,100};for (i = 0; i< 6; i++) {red_black_tree_node<int> *z = Tree.search_tree_node (S2[i]); if (Z ! = tree. NIL) Tree.rb_delete (z);} cout << "The red-black root node after the delete operation is:" <<tree.get_root ()->key << endl;tree.inorder_tree_walk (); return 0;}

Finally, thanks to the two-bit Daniel:

Http://www.cnblogs.com/Anker/archive/2013/01/30/2882773.html

http://blog.csdn.net/mishifangxiangdefeng/article/details/7718917



Introduction to Algorithms-------------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.