Explore Rb-tree data structures in depth

Source: Internet
Author: User

Intro

The Department promotes the software common skills matrix tool in various teams, hoping to find the weak point by measuring and lead the team to improve. From the data of our team, the team's short board in data structure and algorithm obviously needs to be strengthened, which is the original intention behind writing this article.

Data structures and algorithms are the basic skills of programmers and the touchstone of Daniel's programmers. Linus Great God once said: "Bad Programer care about code, good the Progamer care about data." At ordinary times, you may feel that you can not use complex data structures, arrays, the most linked list is enough, but the actual situation may be because you do not understand the other data structures and their respective application scenarios, only the array or chain list to choose.

As a team responsible for the development of Linux kernel and driver, a large number of data structure types are used in the Linux kernel, with the most frequent use of rb-tree (red Block tree, black tree), such as memory management, file system, Network subsystem and other core subsystems are heavily used rb-tree. Rb-tree algorithm is very complex, it is worth studying, unfortunately, in the books and articles I can see, have not yet seen what really can be clearly understood, this is also the choice of Rb-tree as the main motive for sharing the theme.

The background of Rb-tree's production

I have a habit of researching the problem, and I like to explore what kind of demand leads to it, and then what historical evolution eventually becomes the present. It is much more interesting and meaningful to think about the process of thinking behind it than just looking at the complex concrete implementation, a bunch of rule definitions. For example, "data structure and Algorithmic analysis" (Mark Allen Weiss) The 4 rules of the red-black tree given in the book:

    1. Each node is either red or black.
    2. The roots are black.
    3. If a node is red, its child nodes must be black.
    4. Each path from one node to a null pointer must contain the same number of black nodes.

So the question is: why should the 4 rules be rb-tree, and what needs derive from these rules, and what value can these rules bring?

Prior to the advent of Rb-tree (1972), the presence of AVL tree (1962) and 2-3-4 Tree,2-3-4 tree was introduced to obtain a faster, more stable insertion deletion time than the AVL tree (worst case), but 2-3-4 The tree itself has a problem: many scenarios need to be dealt with, the node model is not unified, and the complexity is realized. The emergence of rb-tree is to solve these problems of 2-3-4 tree. This article does not analyze the AVL tree and 2-3-4 tree, if necessary, please consult Wikipedia on its own, the specific comparison is as follows.

column 1 avl tree  2-3-4 trees  red Black Trees
balanced
O. (log N) search time
balanced
O. (log N) search time
faster real-time bounded insertion and deletion
balanced
O. (log N) search time
faster real-time bounded insertion and deletion
same node structures
bads slower real-time bounded insert Ion and deletion different node structures
Large number of special cases INVOLV Ed in operations on the tree
slightly slower search time

As shown, Rb-tree is equivalent to 2-3-4 tree and is an optimization scheme implemented by 2-3-4 tree.

The coloring of Rb-tree is to construct the 3-node, 4-node node model in 2-3-4 tree, the 2 nodes connected by a red line in Rb-tree, equivalent to the 2-3-4 node in 3-node tree, and the 3 nodes connected by two contiguous red lines in Rb-tree. Equivalent to the 4-node node in the 2-3-4 tree. This is the origin of the Rb-tree rule (1).

The Rb-tree rule (4) is a feature of the 2-3-4 tree, so nature is also applicable to Rb-tree.

Red as the internal connector for Rb-tree 3-node, 4-node nodes, the red node has an implied meaning: It also has a parent node. So the root node does not need red, which is the origin of the Rb-tree rule (2). If the root node becomes red in the process of transformation, it can be changed directly to black.

It can be seen that the rules (1), (2), and (4) are a natural interpretation of Rb-tree's 2-3-4 tree. So far, we have no analysis of the Rules (3), is the rb-tree difference from the 2-3-4 tree important rules, but also the core of the rules. This is also the focus of the next section of the analysis.

The implementation mechanism of Rb-tree

Under Rules (1), (2), (4), if the programme A,rb-tree has 2 forms of expression for 3-node, there are 5 forms of expression for 4-node, and 1 forms of expression of 2-node, altogether there are 8 basic structures, which represents the insertion of rb-tree, There are 8 scenarios to consider when deleting an operation, so many scenes need to be dealt with, which will inevitably lead to the complexity and efficiency of the implementation, which is the biggest problem of 2-3-4 tree and the first problem that Rb-tree solves.

The idea of Plan B is to reduce the basic structure of 4-node to one by unifying the 5 forms of expression of 4-node into the 4.5 structure, and reduce the processing scenarios to simplify the implementation of the efficiency of Ascension. This is precisely the purpose of the rule (3), which excludes the 4 structures from 4.1 to 4.4, leaving only 4.5 of this structure. Scenario B shows how structures 4.1, 4.2, 4.3, and 4.4 are transformed to struct 4.5 by rotation.

Plan C, based on scenario B, a step closer to the structure 3.2 through the left to structure 3.1, the 3-node structure is a structure, the scheme is called LLRB (left-leaning rb-tree), its basic structure and 2-3-4 tree one by one corresponding to the implementation of simple, And the algorithm is unified.

Demonstrates the insertion process for rb-tree. Rb-tree when inserting a new node, it is guaranteed to be inserted at the leaf node and is colored red. The insert and delete operations may break the rule (3), which requires a logarithmic structure for rebalancing, and the 2-3-4 tree's rebalancing is node splitting and merging, and in the RB tree is the coloring flip (color flip) and rotation (rotation).

Coloring rollover guarantees that when 5-node occurs, it is decomposed into 2 3-node and the intermediate nodes are sent to the parent node.

Node rotation is divided into left-and right-handed. The node p is left immediately as the left child node of P's right child node, and P is immediately the right child node of P's left child node.

The rotation and shading steps after inserting a new node are shown in the 2-node,3-node,4-node case, respectively. Plan B is more than one group (b) Processing steps, a total of 1+2=3 rotation operation; Plan C is a group of (c) processing steps, which is also a 3-time rotation operation. It can be seen from the realization efficiency, Plan B and program C are the same, but the basic structure of program C is few, the algorithm is unified, the implementation is simple and easier to understand.

With the implementation of the Insert function in Rb-tree in Linux kernel, it can be found that the implementation of the scenario B insertion in and is identical.

void Rb_insert_color (struct rb_node *node, struct rb_root *root) {struct Rb_node *parent, *gparent;while ((parent = Rb_pare NT (node) && rb_is_red (parent)) {gparent = rb_parent (parent), if (parent = = Gparent->rb_left) {{Register struct Rb_node *uncle = gparent->rb_right;if (Uncle && rb_is_red (uncle)) {rb_set_black (uncle); Rb_set_black ( parent); rb_set_red (gparent); node = Gparent;continue;}} if (Parent->rb_right = = node) {Register struct Rb_node *tmp;__rb_rotate_left (parent, root); tmp = Parent;parent = Node;n ODE = tmp;} Rb_set_black (parent); rb_set_red (gparent); __rb_rotate_right (gparent, root);} else {Register struct Rb_node *uncle = gparent->rb_left;if (Uncle && rb_is_red (uncle)) {rb_set_black (uncle); Rb_set_black (parent); rb_set_red (gparent); node = Gparent;continue;}} if (Parent->rb_left = = node) {Register struct Rb_node *tmp;__rb_rotate_right (parent, root); tmp = Parent;parent = Node;n ODE = tmp;} Rb_set_black (parent); rb_set_red (gparent); __rb_rotate_left (Gparent, Root);}} Rb_set_black (Root->rb_node);}

  

--eof--

Explore Rb-tree data structures in depth

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.