[Data structure] red black tree

Source: Internet
Author: User

Binary search Tree
(i) Concept
A binary search tree is a node-ordered two-fork tree,
(1) The value of the left branch node of the root node is small.
(2) The right branch node value is greater than the root node value
(3) All sub-trees are also two-fork search tree

Self-balancing binary search tree
Balanced binary tree: a two-fork search tree with a depth difference of not more than 1 for all leaf nodes
Self-balancing binary search tree: Refers to a two-fork search tree whose operations are trying to maintain balance


Red and black Trees
Red-black tree is a self-balancing binary search tree, the red and black tree has the following characteristics:
1. Each node is either black or red.
2. The root node is black
3. Each leaf node (NIL) is black
4. If a node is red, then its two children are black.
5. Contains the same number of black nodes on each simple path from which a node reaches its descendant leaf node
The red-black tree implemented in the Linux kernel is rbtree, and the advantage of the red-black tree in the rbtree.c file is that all operations can be done in O (log n) time,

The nodes of the red-black tree in the Linux kernel are represented by the following structure:

[CPP]View Plaincopy
    1. 100 struct rb_node  
    2. 101 {  
    3. 102          unsigned LONG  RB_PARENT_COLOR;  
    4. 103   #define  RB_RED          0  
    5. 104  #define  RB_BLACK        1  
    6. 105         struct rb_node  *rb_right;  
    7. 106          struct rb_node *rb_left;  
    8. 107 } __ ATTRIBUTE__ ((Aligned (sizeof (long;


A pointer to its parent node is hidden in the rb_node structure of Linux, in the Rb_parent_color member because the red and black only requires a bit, and the pointer is aligned in 4 byte

[CPP]View Plaincopy
    1. #define Rb_parent (R) (struct Rb_node *) ((R)->rb_parent_color &))
    2. 117 #define RB_COLOR (R) (r)->rb_parent_color & 1)


The Linux kernel finds specific members through a red-black tree in the form of a container.
#define CONTAINNER_OF (Ptr,type,member)

The root node of the red and black tree behaves as follows, and the benefit is that you can not pass a level two pointer

[CPP]View Plaincopy
    1. #define RB_ROOT (struct rb_root) {NULL,}
    2. rb_root struct
    3. 111 {
    4. Rb_node *rb_node, a struct;
    5. 113};


(ii) left-hand, right-handed and insert operation of red and black trees

For the left and right-hand specific illustrations, please refer to the following URL to open the link

The code for L in RBTREE.C is as follows:

[CPP]View Plaincopy
  1. -Static void __rb_rotate_left (struct rb_node *node, struct rb_root *root)
  2. 27 {
  3. rb_node struct *right = node->rb_right;
  4. The struct Rb_node *parent = rb_parent (node);
  5. 30
  6. if ((Node->rb_right = right->rb_left))
  7. Rb_set_parent (right->rb_left, node);
  8. right->rb_left = node;
  9. 34
  10. Rb_set_parent (right, parent);
  11. 36
  12. PNS if (parent)
  13. 38 {
  14. if (node = = parent->rb_left)
  15. Parent->rb_left = right;
  16. $ Else
  17. Parent->rb_right = right;
  18. 43}
  19. The else
  20. Root->rb_node = right;
  21. Rb_set_parent (node, right);
  22. 47}


Red and black tree Insert can refer to the following URL click to open the link

Red and black Tree detailed explanation

[Data structure] red black tree

Related Article

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.