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
- 100 struct rb_node
- 101 {
- 102 unsigned LONG  RB_PARENT_COLOR;  
- 103 #define  RB_RED          0  
- 104 #define RB_BLACK 1
- 105 struct rb_node *rb_right;
- 106 struct rb_node *rb_left;
- 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
- #define Rb_parent (R) (struct Rb_node *) ((R)->rb_parent_color &))
- 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
- #define RB_ROOT (struct rb_root) {NULL,}
- rb_root struct
- 111 {
- Rb_node *rb_node, a struct;
- 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
- -Static void __rb_rotate_left (struct rb_node *node, struct rb_root *root)
- 27 {
- rb_node struct *right = node->rb_right;
- The struct Rb_node *parent = rb_parent (node);
- 30
- if ((Node->rb_right = right->rb_left))
- Rb_set_parent (right->rb_left, node);
- right->rb_left = node;
- 34
- Rb_set_parent (right, parent);
- 36
- PNS if (parent)
- 38 {
- if (node = = parent->rb_left)
- Parent->rb_left = right;
- $ Else
- Parent->rb_right = right;
- 43}
- The else
- Root->rb_node = right;
- Rb_set_parent (node, right);
- 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