I. Concept
The red and black tree is a binary search tree, which adds a storage bit on each node to represent the color of the node, which can be red or black. By constraining any color from the root to the leaf's simple path, the red-black tree guarantees that the longest path does not exceed twice times the shortest path, thus approximating the balance.
2. Nature:
Every node, not red or black.
The root node is black
If a node is red, its two child nodes are black ( no contiguous red nodes )
For each node, the same number of black nodes are included on the simple path from the node to all of its descendant leaf nodes. (The number of black nodes per path is equal )
Two. Code implementation
#include <iostream>using namespace std;enum COL{RED,BLACK};template<class K, class v>struct rbtreenode{k _key; v _value; col _col; rbtreenode<k, v>* _left; rbtreenode<k, v>* _right; rbtreenode<k, v>* _parent; Rbtreenode (Const k& key,const v& value): _key (Key), _value (value), _col (RED), _left (null), _right (null), _parent (null) {}};template<class k,class v >class rbtree{typedef rbtreenode<k, v> node;public:rbtree (): _root (NULL) {}~RBTree ( {}bool insert (Const k& key, const v& value) {//Find the location of the insertion node if (_root == null) {_root = new node (key, value); _root->_col = black;return true;} node* cur = _root; node* parent = null;while (cur) {if (key > cur->_key) {Parent = cur;cur = cur->_right;} else if (Key < cur->_key) {parent = cur;cur = cur->_left;} Else{return false;}} Insert Cur = new node (key, value);if (key > parent->_key) {parent->_ Right = cur;cur->_parent = parent;} Else{parent->_left = cur;cur->_parent = parent;} Judging whether it complies with the red/black tree//Judgment Rule 3 (cannot have two consecutive red nodes) while (cur != _root&&parent->_col==red)//cur!=_ Root guarantees that the parent is not empty {node* grandparent = parent->_parent;//Find Uncle node if (grandparent->_left == parent) {node* uncle = grandparent->_right;//first case: Uncle node exists and is red if (uncle& &uncle->_col == red) {//convert parent and uncle to black, grandparent to red, then cur to grandparent, adjust up parent->_col = uncle->_col = BLACK; Grandparent->_col = red;cur = grandparent;parent = cur->_parent;} //Second case: Uncle node does not exist or for black (at this time grandparent, parent, cur constitute the condition of paddle) else {//the third situation: at this time grandparent, parent, cur constitute the conditions of the double spin if ( Parent->_right == cur) {//Zodan to parent, converted to the second case Rotatel (parent);//Note: The pointer position after rotation is swapped swap (parent, cur );} Rotater (grandparent);//Turn the parent black, grandparent Red parent->_col = black; Grandparent->_col = red;break;}} Contrary to the above else{node* uncle = grandparent->_left;//the first case if (uncle&&uncle->_col == red) {parent->_col = uncle->_col = black; Grandparent->_col = red;cur = grandparent;parent = cur->_parent;} The second case else{//the third case conversion to the second case if (parent->_left == cur) {rotater (parent); Swap (parent , cur);} Rotatel (grandparent);p arent->_col = black; Grandparent->_col = red;break;}}} _root->_col = black;return true;} Void inorder () {_inorder (_root); Cout << endl;} Bool isblance () {if (_root == null) return true;//2nd rule: The root node is black//Judgment root node if (_root->_col == RED) {return false;} node* cur = _root;int k = 0;while (cur) {//4th rule: equal number of black nodes per path //statistics The number of black nodes per left and right subtree if (cur->_col == black) {k++;} Cur = cur->_left;} Int count = 0;return _isblance (_root,k,count);} Protected:node* _root;void _inorder (Node* root) {if (root == null) Return;_InOrder (root->_left);cout << root->_key << " "; _inorder (root->_right);} Bool _isblance (Node* root,const int k,int count) {if (Root == NULL) return true;//3rd rule: There is no continuous red node if (root->_col == red) {if (root->_parent->_col == red) {cout << "color Wrong" << root->_key << endl; Return false;}} Statistic black node Else{++count;} If (root->_left == null&&root->_right) {if (count == k) return true;else{cout << "The number of black nodes is not the same" << root->_key << endl;return false;}} Return _isblance (Root->_left, k, count) && _isblance (Root->_right, k, count);} Void rotatel (node* parent) {node* subr = parent->_right; node* subrl = subr->_left;parent->_right = subrl;if (SubRL) {subRL->_ Parent = parent;} node* ppnode = parent->_parent;subr->_left = parent;parent->_parent = subR;if (ppnode == null) {_root = subr;subr->_parent = null;} else{subr->_parent = ppnode;if (ppnode->_left == parent) {ppNode->_left =&NBSP;SUBR;} ELSE{PPNODE->_RIGHT&NBSP;=&NBSP;SUBR;}}} Void rotater (node* parent) {node* subl = parent->_left; node* sublr = subl->_right;parent->_left = sublr;if (SubLR) {subLR->_ Parent = parent;} node* ppnode = parent->_parent;subl->_right = parent;parent->_parent = subL;if (ppnode == null) {_root = subl;subl->_parent = null;} else{subl->_parent = ppnode;if (ppnode->_left == parent) {ppNode->_left = subl;} else{ppnode->_right = subl;}}}}; Int main () {int a[] = { 16, 3, 7, 11, 9, 26, 18, 14 , 15 }; rbtree<int, int> rbt;for (Int i = 0; i < sizeof (a) / sizeof (A[0]); i++) {RBT. Insert (a[i],i);cout <<a[i]<< "&NBSP;IS&NBSP;BLANCE&NBSP;?" &NBSP;<<&NBSP;RBT. Isblance () << endl;} Rbt. Inorder ();cout<< "Is blance ? " <<RBT. Isblance () <<endl;system ("pause"); return 0; }
This article is from the "sunshine225" blog, make sure to keep this source http://10707460.blog.51cto.com/10697460/1828120
Red and black Trees