Data structure--red and black trees

Source: Internet
Author: User

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.


The red-black tree is a two-fork search tree that satisfies the following red-black nature:

    1. Every node, not red or black.

    2. The root node is black

    3. If a node is red, its two child nodes are black ( no contiguous red nodes )

    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. (The number of black nodes per path is equal )


Here's an analysis of why the red-black tree can guarantee that the longest path does not exceed twice times the shortest path: first, because the 4th constraint assumes that a tree looks like this:

B

b b

b b b is represented as a black node, then to insert any one of the black nodes you need to ensure that the 4th Convention, and if you want to insert a red node, the 3rd constraint so that the red node can only be inserted between the black nodes, so a path up to:

B

B R

b b

R

B Therefore, the longest path does not exceed the shortest path of twice times, it will ensure the efficiency of the search;



Here's how to implement the red-black tree Insert process:

Color of the #pragma  once#include <iostream>using namespace std;//junction   red or black enum color{ RED,BLACK};//node Structure Body template <class k, class v>struct rbtreenode{k _key; v _val; rbtreenode<k, v>* _left; rbtreenode<k, v>* _right; rbtreenode<k, v>* _parent; color _col; Rbtreenode (Const k& key, const v& val): _key (Key), _val (Val), _left (NULL), _right (null), _parent (null), _col (red) {}};//red-black tree Template <class k, class v>class rbtree {Typedef rbtreenode<k, v> node;public:rbtree (): _root (NULL) {}~rbtree () {_Clear (_root);} Insert Node Bool insert (const k& key, const v& val) {if (_root == NULL )//If the root node is null, create a new node to return True{_root = new node (Key, val) to the root node; _root->_col = black ; return true;} If the root node is not NULL, traverse the tree until the appropriate insertion position is found node* cur = _root; node*&Nbsp;prev = cur;while (cur != null) {if (Cur->_key == key)//If the node is already in the tree, Returns FALSERETURN&NBSP;FALSE;ELSE&NBSP;IF (Cur->_key > key)//If the key value is less than the key value of the node, go to the left subtree for {prev =  cur;cur = cur->_left;} else//otherwise the key value is greater than the key value of the node, go to the right subtree to find {prev = cur;cur = cur->_right;}} The loop ends, the appropriate insertion position is found, and the judgment should be inserted to the left of the node or to the right Node* tmp = new node (key, val); if (Prev->_key  > key) prev->_left = tmp;elseprev->_right = tmp;tmp->_parent =  prev;//after inserting a node, it is time to start judging whether the current tree conforms to the nature of the red-black tree while (Tmp != _root  &&  (prev->_col  == red) {node* grandfather = prev->_parent;//extracts the TMP's grandfather node if (grandfather->_left  == prev)//If Prev is the left node of grandfather {//The first case//tmp is red, prev is red, grandfather is black, uncle exists and is red//Then prev,uncle is changed to black, Grandfather change to red, and then grandfather as TMP, continue to adjust upward. Node* uncle = grandfather->_right;if (Uncle != null &&  uncle->_col == red) {prev->_col = black;uncle->_col = black; Grandfather->_col = red;tmp = grandfather;prev = tmp->_parent;} else//Second situation: TMP is red, prev is red, grandfather is black, uncle does not exist/uncle for black//prev for grandfather left child, TMP for Prev left child, then the right single rotation;//prev, Grandfather Discoloration--prev Black, grandfather Red {//third case//tmp for red, prev for red, grandfather for Black, Uncle no/uncle for black//prev for grandfather left child , TMP is prev right child, then the left single rotation for prev,//is converted to the case two if (prev->_right == tmp) {_rotatel (prev); swap (Tmp, prev);} _rotater (grandfather);//Right Paddle Prev->_col = black;grandfather->_col = red;break;}} else//when Perv is the right node of grandfather, the opposite of the above {//The first case//tmp is red, prev is red, grandfather is black, uncle exists and is red//The prev,uncle is changed to black, Grandfather change to red, and then grandfather as TMP, continue to adjust upward. Node* uncle = grandfather->_left;if (uncle != null && uncle->_ col == red) {Prev->_col = black;uncle->_col = black;grandfather->_col = red;tmp = grandfather;prev = tmp->_parent;} else//Second situation: TMP is red, prev is red, grandfather is black, uncle does not exist/uncle for black//prev for grandfather right child, TMP for Prev right child, then left single rotation//prev, Grandfather Discoloration--prev Black, grandfather Red {//The third case//tmp for red, prev for red, grandfather for Black, uncle not present/uncle for black//prev for grandfather right child , TMP is Prev's left child, then the right single rotation for Prev//is converted into the case two if (prev->_left == tmp) {_rotater (prev); swap (Tmp, prev);} _rotatel (grandfather);//For Right paddle Prev->_col = black;grandfather->_col = red;break;}}} If the root node is adjusted to red, it will change to black, and will not affect the number of left and right black nodes if (_root->_col == red) _root->_col = black;return  true;} Void inorder () {_inorder (_root); Cout<<endl;} /*bool find (Const k& key) {}*/private:void _clear (node* root) {if (root ==  null) return;_clear (root->_left); _clear (root->_right);d elete root;root = null;} Void _rotatel (node* parent)//Zodan &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp; node* subr = parent->_right;        node*  subrl = subr->_left;         parent->_right  = subRL;        if  (subrl != null)              subRL->_parent = parent;                  Node*  Ppnode = parent->_parent;         subr->_left  = parent;        parent->_parent = subR;          if  (ppnode == null)              _root = subR;         else  &nBsp;     {            if   (ppnode->_left == parent)                  ppNode->_left = subR;             else                 ppnode->_right = subr;        } Subr->_parent = ppnode;    }    void _rotater (Node*  parent)//Right paddle &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;NODE*&NBSP;SUBL  = parent->_left;        Node* subLR =  subl->_right;         parent->_left = sublr;         if  (sublr != null)              sublr->_parent = parent;         node*  ppnode = parent->_parent;        subl->_right  = parent;        parent->_parent = subL;          if  (ppnode == null)              _root = subL;         else        {             if  (ppnode->_left == parent)                  ppNode->_left = subL;             else                 ppnode->_right = subl;        } Subl->_parent = ppnode;} Void _inorder (node* root) {if (root != null) {_inorder (root->_left); cout<<root->_ key<< " "; _inorder (Root->_right);}} private:node* _root;}; Void test () {int arr[] = {3, 4, 6, 1, 7, 2, 8}; Rbtree<int, int> t;for (int i = 0; i < sizeof (arr)/sizeof (arr[ 0]);  ++i) T.insert (arr[i], i); T.inorder ();


To run the program:

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M00/85/3F/wKioL1eeGTLBZhTBAABpj_8nBYA643.png-wh_500x0-wm_3 -wmp_4-s_3537116884.png "title=" Rbtree.png "alt=" Wkiol1eegtlbzhtbaabpj_8nbya643.png-wh_50 "/>



Finish


This article is from the "Knock Code good Sleep zzz" blog, please be sure to keep this source http://2627lounuo.blog.51cto.com/10696599/1832660

Data structure--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.