Summary of Introduction to algorithms-Chapter 12. Red and black trees (1)

Source: Internet
Author: User

We recommend that you first look at the preface: http://www.cnblogs.com/tanky_woo/archive/2011/04/09/2010263.html

This chapter has a lot of content, so I will write it in four articles. This article is about some basic concepts and choices. The two articles in the middle are insert and delete, and the last one is a summary.

 

The previous chapter summarizes BST (http://www.wutianqi.com /? P = 2430), BST is relatively small in height and can achieve good performance (because the average time of BST operations is O (lgn), but when the height is large, the performance is normal. While the red and black trees "approximate balance" reduces the average time. Furthermore, the red and black trees do not pursue a "full balance"-They only require a balance in part and reduce the rotation requirements, this improves the performance.

When talking about the use of the red and black trees, the most widely known application should be the application of the red and black trees in C ++ STL. In set, multiset, map, multimap, etc, they all use the red and black trees (you can search for them on the Internet ).

 

The definition of the red/black tree and the black height is given below:

A binary search tree that meets the following conditions (red and black) is calledRed/black tree:
1. Each node is either red or black.
2. The root node is black.
3. All leaf nodes (NULL) are black. (NULL is regarded as a sentinel node. All pointers that should point to NULL are considered to point to NULL nodes .)
4. If a node is red, its two son nodes are black.
5. For each node, all paths from the node to its child node contain the same number of black nodes.

Black heightDefinition:
Starting from a node (not including this node) to any path of a leaf node, the number of black nodes becomes the black height of the node x.

The following is a red/black tree:

The red/black tree is a binary search tree. Unlike the common Binary Search Tree, each node of the red/black tree has another property-color, which can be red or black. By specifying the node color rules for each path, the red/black tree can ensure that the number of nodes in any two paths from the root to the leaves is no more than twice. Therefore, the red/black tree is an almost balanced binary search tree.

The search, maximum, minimum, forward, and subsequent operations of the red/black tree are no different from the common Binary Search Tree. The insert and delete operations must be implemented again. The insertion and deletion of a common binary search tree may damage some of the properties of the red/black tree. Therefore, additional processing is required. These additional operations mainly change the color of the Tree node or the structure of the tree.

About rotation:

I drew 13-2 manually and added some notes:

 

The rotation is actually relatively simple, so I won't say much about it. Here is the code:

123456789101112131415161718192021222324252627282930313233343536373839
void LeftRotate(RBTree &T, Node *x){Node *y = x->rchild;x->rchild = y->lchild;if(y->lchild != NULL)y->lchild->parent = x;y->parent = x->parent;if(x->parent == NULL)T = y;else{if(x == x->parent->lchild)x->parent->lchild = y;elsex->parent->rchild = y;}y->lchild = x;x->parent = y;} void RightRotate(RBTree &T, Node *x){Node *y = x->rchild;x->rchild = y->lchild;if(y->lchild != NULL)y->lchild->parent = x;y->parent = x->parent;if(x->parent == NULL)T = y;else{if(x == x->parent->lchild)x->parent->lchild = y;elsex->parent->rchild = y;}y->lchild = x;x->parent = y;}

The next article is about insert.

 

 

In my independent blog: http://www.wutianqi.com /? P = 2438

You are welcome to learn from each other and discuss each other!

 

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.