Algorithm series Note 4 (Red-black tree)

Source: Internet
Author: User
Tags color gamut

The height expectation of a randomly constructed two-fork lookup tree is O (LGN), and does not mean that all two-fork lookup trees have a height of O (LGN). But for some binary search tree deformation, the performance of the basic operation of the dynamic set is always very good, such as red black tree, B-tree, balanced binary tree (AVL tree), jumping table (not the exact tree, more or less the structure of the tree), treaps (tree heap) and so on. Here we explain the red and black trees.

Balance means that the operation of the dynamic dataset (minimum, maximum, search, predecessor (precursor), successor (successor), insert, and delete) is completed in O (LGN) time.

Defined:

The red-black tree adds color gamut-red or black on the basis of the binary search tree. A red and black tree to meet the following 4 properties: (1) The junction is either red or black.

(2) Both the root and the leaf nodes are black.

(3) A node is red, then its parent must be black.

(4) The number of black nodes on the simple path of any node to its leaf nodes should be the same = black height (excluding its own nodes, but including leaf nodes).

Properties:

A red-black tree with n nodes (excluding the leaf node) has a height of not more than 2LG (n+1).

So these dynamic set operations are done in O (LGN) time, so the red and black trees are balanced.

Actions (insert and delete)

The main difference between the red and black tree operation and the two-fork find tree is the insertion and deletion, the others are the same. Refer to the previous blog.

Insert:

And the red and black tree can only be inserted to create a red and black tree to maintain the red and black characteristics. The insert operation of the red and black tree is divided into three cases.

To Pass ... ..

which case1 for coloring, you can always move up, each time you move 2 layer, due to the tree height ≤ 2LG (n+1) , so most loops LG (N+1) times. case2 and case3 are termination cases, each rotated once, both O (1) time. So the insertion of the red and black tree can be done in O (LGN) time .

The code is as follows:

RBTree.h

#ifndef rbtree#define rbtree#include <iostream> #include <string>using namespace Std;class bstnode{public: Bstnode *left, *right; Bstnode *parent;int val;string color;}; Class Rbtree{public:rbtree (const int &rootval) {root = new Bstnode (); root->val = Rootval;root->parent = NULL; Root->left = Null;root->right = Null;root->color = "Black";} void Insertrbtree (Bstnode *root, const int &value);   Red-black Tree inserted void Inorderrbtree (Bstnode *p); Bstnode *root;private:bstnode *insertbst (bstnode *p, const int &value);       The insertion of a binary tree}; #endif

RBTree.cpp

#include "RBTree.h" using namespace std; bstnode* Rbtree::insertbst (bstnode *p, const int &value) {Bstnode *y = NULL; Bstnode *in = new Bstnode (); in->left = Null;in->right = Null;in->val = Value;in->parent = NULL;while (P! = NULL {y = p;if (P->val > In->val) p = p->left;else p = p->right;} if (y = = NULL) p = in;else{in->parent = Y;if (Y->val > In->val) y->left = In;else y->right = in;} return in;} void Rbtree::insertrbtree (Bstnode *root1, const int &value) {Bstnode * in = Insertbst (root1, value); In->color = "Red "while (In! = root1 && In->color = =" Red ") {//adjust the red and black features if (In->parent->color = =" Black ") retur   N It is guaranteed that the if (in->parent = = in->parent->parent->left) {Bstnode *y = in->parent->parent->right;if ( Y! = NULL && Y->color = = "Red") {//Case 1y->color = "BLACK"; y->parent->color = "Red"; in->parent ->color = "BLACK"; in = in->parent->parent;} Else{if (in = = In->paRent->right) {//Case 2 in->parent L-bstnode *pa = In->parent;in->parent = pa->parent;pa->parent- >left = In;pa->parent = In;if (in->left! = NULL) in->left->parent = Pa;pa->right = in->left;in-> left = Pa;in = PA;} Case 3 In->parent->parent Right Bstnode *pa = in->parent; Bstnode *gpa = in->parent->parent;if (gpa->parent! = NULL) {if (GPA = = gpa->parent->left) {gpa->parent- >left = PA;} Elsegpa->parent->right = PA;} Pa->parent = Gpa->parent;if (pa->right! = NULL) pa->right->parent = Gpa;gpa->left = pa->right;pa- >right = Gpa;gpa->parent = Pa;pa->color = "BLACK"; gpa->color = "Red";}}  Else{bstnode *y = in->parent->parent->left;if (Y! = NULL && Y->color = = "Red") {//Case 1y->color = "BLACK"; y->parent->color = "red"; In->parent->color = "BLACK"; in = in->parent->parent;} else{//Do the same as A and left and right swap if (in= = In->parent->left) {//Case 2 in->parent right Bstnode *pa = In->parent;in->parent = pa->parent;pa- >parent->right = In;pa->parent = In;if (in->right! = NULL) in->right->parent = Pa;pa->left = in-> Right;in->right = Pa;in = PA;} Case 3 in->parent->parent L-bstnode *pa = in->parent; Bstnode *gpa = in->parent->parent;if (gpa->parent! = NULL) {if (GPA = = gpa->parent->left) {gpa->parent- >left = PA;} Elsegpa->parent->right = PA;} Pa->parent = Gpa->parent;if (pa->left! = NULL) pa->left->parent = Gpa;gpa->right = pa->left;pa-> left = Gpa;gpa->parent = Pa;pa->color = "BLACK"; gpa->color = "Red";}}} Root1->color = "Black";} void Rbtree::inorderrbtree (Bstnode *p) {if (p = = null) return;if (p->left! = null) Inorderrbtree (p->left); cout <& Lt P->val << p->color << ""; if (p->right! = NULL) Inorderrbtree (p->right);}

Main.cpp

int a[10] = {5,4,6, 7,2,4, 1, 8, 5, 10}; Rbtree RBT (a[0]); for (int i =1; i <; i++) Rbt.insertrbtree (Rbt.root, a[i]); cout << "Middle sequence traversal result:" << ENDL;RBT . Inorderrbtree (Rbt.root);
Result:



Delete:

To be continued ... ..

Algorithm series Note 4 (Red-black tree)

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.