The red-black tree is a two-fork search tree that satisfies the following properties
1. Each node, not red or black
2. The root node is black
3. If a node is red, its two child nodes are black
4. 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.
#pragma onceenum Color{ RED, BLACK };template<class K, class V> Struct rbtreenode{rbtreenode (const k& key, const v& v, color col = red): _left (null), _right (null), _parent (null), _key (key), _vlaue (v), _col (COL) {}rbtreenode<k, v>* _left; rbtreenode<k, v>* _right; rbtreenode<k, v>* _parent; k _key; v _vlaue; color _col;}; Template<class k, class v>class rbtree{typedef rbtreenode<k, v> node;public:rbtree (): _root (NULL) {}bool insert (const k& key, const v& &NBSP;V) {if (_root == null) {_root = new node (key, v, black); return true;} node* parent = null; node* cur = _root;while (cur) {if (key < cur->_key) {parent = cur;cur =&nbsP;cur->_left;} else if (Key > cur->_key) {parent = cur;cur = cur->_right;} Else{return false;}} Cur = new node (key, v, red);if (key < parent->_key) {parent->_ Left = cur;cur->_parent = parent;} Else{parent->_right = cur;cur->_parent = parent;} Color while (cur != _root && parent->_col == red) {Node* grandparent = parent->_parent;if (parent == grandparent->_left)//go to left subtree {Node* uncle = GrandParent->_right;if (uncle && uncle->_col == red) {uncle->_col = parent->_col = black; Grandparent->_col = red;cur = grandparent;parent = cur->_parent;} else{if (cur == parent->_right) {_rotatel (parent); swap (cur, parent);} _rotater (grandparent);p arent->_col = black; grandparent->_col = red;}} else//into the right subtree {node*uncle = grandparent->_left;if (uncle && uncle->_col == red) {uncle->_col = parent->_col = black; Grandparent->_col = red;cur = grandparent;parent = cur->_parent;} else{if (cur == parent->_left) {_rotater (parent); swap (cur, parent);} _rotatel (grandparent);p arent->_col = black; grandparent->_col = red;}}} _root->_col = black;return true;} Bool isbalancetree () {return _isbalance (_root);} Void inorder () {_inorder (_root); Cout << endl;} Protected:int _height (Node* root) {if (root == null) {return 0;} Int left = _height (Root->_left) + 1;int right = _height (root->_ right) + 1;return (left > right) ? left : right;} Bool _isbalance (Node* root) {if (root == null) {return true;} Int left = _height (Root->_left); Int right = _height (root->_right); int bf = abs (left - right);if (bf > 1) {return false;} Return _isbalance (Root->_left) && _isbalance (root->_right);} Void _rotatel (node* parent) {node *subr = parent->_right; node *subrl = subr->_left;parent->_right = subrl;if (SubRL) {subRL->_ Parent = parent;} subr->_left = parent;subr->_parent = parent->_parent;parent->_parent = subr;parent = subr;//father:) if (parent->_parent == null) {_root = Parent;} else{if (parent->_parent->_key < parent->_key) {parent->_parent->_right = parent;} Else{parent->_parent->_left = parent;}}} Void _rotater (node* parent) {node *subl = parent->_left; node *sublr = subl->_right;parent->_left = sublr;if (subLR != NULL) {sublr->_parent = parent;} subl->_right = parent;subl->_parent = parent->_parent;parent->_parent = subL;parent = subL;if (parent->_parent == null) {_root = parent;} else{if (parent->_parent->_key < parent->_key) {parent->_parent->_right = parent;} else{parent->_parent->_left = parent;}}} Void _inorder (Node*& root) {if (root == null) {return;} _inorder (Root->_left);cout << root->_key << " "; _InOrder (root->_ right);} protected:node* _root;}; Void testrbtree1 () {rbtree<int, int> t1;int a[10] = { 5, 2, 9, 6, 7, 3, 4, 0, 1, 8 };for (size_t i = 0; i < sizeof (a) / sizeof (int); ++i) {t1. Insert (a[i], i); t1. Inorder ();} cout << "isbalancetree ? " << t1. Isbalancetree () << endl;}
This article is from the "Wood Man" blog, please make sure to keep this source http://10324228.blog.51cto.com/10314228/1771005
Rbtree (red-black tree)--c++