About RB Trees
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. ( Nature 3, Nature 4 ensures that the longest path of the red and black tree does not exceed the shortest path of twice times )
:
The red-black tree is a two-fork search tree that satisfies the following red-black nature
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.
Well, now that we know the characteristics of the red and black trees, we'll start creating the red and black trees.
The structure of the nodes of the red-black tree
1template<classKclassV>2 structRedblacetreenode3 {4typedef redblacetreenode<k, V>Node;5Redblacetreenode (Constk& Key,Constv&value)6 : _left (null), _right (null), _parent (null)7 , _key (key), _value (value), _col (RED)8 {}9 Tennode*_left; Onenode*_right; Anode*_parent; - K _key; - V _value; the Sign _col; -};
The structure of red and black trees
template<classKclassV>classredblacetree{typedef redblacetreenode<k, v>Node; Public: Redblacetree (): _root (NULL) {} Public: BOOL_push (Constk& Key,Constv&value); void_leftspin (node*&parent); void_rightspin (node*&parent); void_leftrightspin (node*&parent); void_rightleftspin (node*&parent); /*Check*/ //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 void_inorder () {inorder (_root);} voidInorder (node*root);protected: Node*_root;};
Create a red-black tree function _push ()
Several cases of insertion
Cur is the current node, p is the parent node, G is the grandfather node, and U is the Uncle node
1. The first case cur is red, p is red, G is black, u exists and red will p,u change to black, g to red, and then G as cur, continue upward adjustment. (Pretty Simple O (∩_∩) o keep looking down ↓↓)
2. The second case cur is red, p is red, G is black, U does not exist in the left child of/U for Black P, and cur is P left child, then the right single rotation; instead, p is the right child of G, the right child of P is cur, then the left single rotation p, g discoloration--p turn black, g turns red
3. The third case cur is red, p is red, G is black, U does not exist/U is black
P is the left child of G, Cur is P's right child, then p is left single rotation; instead, p is G's right child, Cur is P's left child, then the right single rotation for p is converted into a case 2
template<classKclassV>BOOLRedblacetree<k, V>::_push (Constk& Key,Constv&value) {Node* cur =_root; Node* Parent =NULL; if(_root = =NULL) {_root=NewNode (key, value); _root->_col =BLACK; } Else { while(cur) {parent=cur; if(Key > Cur->_key) {cur= cur->_right; } Else if(Key < Cur->_key) {cur= cur->_left; } Else { return false; }} cur=NewNode (key, value); if(Key > Parent->_key) {Parent->_right =cur; Cur->_parent =parent; } Else{Parent->_left =cur; Cur->_parent =parent; } if(Parent->_col = = RED)//If the Father node is red, you need to adjust { BOOLSign =false;//tag bit, used to mark whether grandparent is the root node while(cur->_parent) {Node* Grandparent =NULL; Node* Uncle =NULL; Parent= cur->_parent; Grandparent= parent->_parent; if(grandparent)//if the grandfather node exists { if(Grandparent = =_root) { Sign=true;//flag whether the grandparent node is a root node } if(Parent->_key > Grandparent->_key)//determine the Uncle node{Uncle= grandparent->_left; } Else{Uncle= grandparent->_right; } //first case: cur is red, p is red, G is black, u exists and is red if(Uncle && Uncle->_col = =RED) {Parent->_col =BLACK; Uncle->_col =BLACK; Grandparent->_col =RED; if(sign) { Break; } cur=grandparent; } //The second case: cur is red, p is red, G is black, U does not exist/U is black (paddle)//The third case: cur is red, p is red, G is black, U does not exist/U is black (double spin) Else { //Second if(Grandparent->_left = = Parent && Parent->_left = =cur) {Grandparent->_col =RED; Parent->_col =BLACK; _rightspin (grandparent); if(sign) {_root=grandparent; } Break; } Else if(Grandparent->_right = = Parent && Parent->_right = =cur) {Grandparent->_col =RED; Parent->_col =BLACK; _leftspin (grandparent); if(sign) {_root=grandparent; } Break; } //Third Else if(Grandparent->_left = = Parent && Parent->_right = =cur) {Grandparent->_col =RED; Cur->_col =BLACK; _leftrightspin (grandparent); if(sign) {_root=grandparent; } Break; } Else if(Grandparent->_right = = Parent && Parent->_left = =cur) {Grandparent->_col =RED; Cur->_col =BLACK; _rightleftspin (grandparent); if(sign) {_root=grandparent; } Break; } } } Else //There 's only one floor above cur. { Break; } }}} _root->_col =BLACK;}
Balance Search tree (ii) Rb red-black Tree