Application and Realization of red black tree

1, What are the uses of red and black trees? 1 map–> 2 nginx–> 3 timer – > 4 cfs (set of process scheduli...
1, What are the uses of red and black trees?
2, Concept
2, Realize red black tree

1, What are the uses of red and black trees?

1 map–>
2 nginx–>
3 timer – >
4 cfs (set of process scheduling) (set stored in red black tree in operating system) (fast and orderly search)
5 memory management (balance relationship between red and black trees);
Question: typical malloc memory fragmentation?
Answer: a piece of memory corresponds to a key;
The memory address pointed to by key plus the length is the management of memory fragments. Key value -- > search.
Question: what are the applications of red black tree?
1> Most self balancing BST library functions are implemented with red and black trees, such as map and set in C + + (or TreeSet and TreeMap in Java).
2> Red black tree is also used to realize CPU scheduling of Linux operating system. The complete fair scheduler uses the red black tree.

2, Concept

1. Each node is red or black
2. The root node is black
3 each leaf node is black (NULL)
4. There are no two adjacent red nodes in the tree (that is, neither the parent node nor the child node of the red node can be red)
5 each path from any node (including root node) to any descendant NULL node (black by default) has the same number of black nodes.

This is a typical red black tree. The color of each node in the tree is either black or red; The root node 6 is a black node; There are no two adjacent red nodes in the tree. For example, if node 15 is a red node, its parent node 6 and two child nodes must be black, not red; Each path from the node to its descendant NULL node has the same number of black nodes. For example, the NULL node from root node 6 to its left subtree contains three black nodes, and all NULL nodes to its right subtree also contain three black nodes. It may not be clear enough. Therefore, I modified the above figure to give a mark to all NULL nodes that are black by default.


Now the fourth rule of interpretation can hardly be clearer! For example, there are three black nodes on path 6 → 2 → a from root node 6 to NULL node a, and there are also three black nodes in path 6 → 15 → 10 → 9 → c from root node 6 to node c. similarly, the number of black nodes from root node 6 to all other NULL nodes is 3. Take another chestnut. The path 15 → 18 → g from red node 15 to NULL node d contains two black nodes, and the path 15 → 10 → 9 → c to NULL node c also contains two black nodes. The number of black nodes from node 15 to all its descendants is 2.

###Comparison between red black tree RBT and balanced binary tree AVL:

AVL trees are more balanced than red and black trees, but AVL trees also have a lot of rotation operations during insertion and deletion. So when your application involves frequent insert and delete operations, remember to give up the AVL tree and choose the red black tree with better performance; Of course, if the insertion and deletion operations involved in your application are not frequent, but the search operations are relatively more frequent, AVL tree is preferred for implementation.

###What is the black height of a red black tree?

In a red black tree, the number of black nodes contained in any simple path from a node x (excluding the node) to a leaf node is called black height, which is recorded as bh(x).
For example, the black height corresponding to node 6 and the black height corresponding to node 15 are both 2;
6->2->null ;
15 - > 10 - > 9_null (9 is the red node and is not included in the black height calculation);

2, Realize red black tree

*Realize a node of red black tree

typedef int KET_TYPE //Type of key typedef struct _rbtree_node{ unsinged char color; struct _rbbtree_node *left; struct _rbbtree_node *right; struct _rbbtree_node *parent; //Father of node KEY_TYPE key; //The key of the node on rbtree corresponds to the hidden value void *value; }rbtree_node;

Realize red black tree

typedef struct _rbtree{ rbtree_node *root; //According to the nature of red black tree - each leaf node is black //Make a general leaf node (* nil) and put all leaf nodes into it to judge whether it is black; rbtree_node *nil; };

Rotation of red black tree

Realize the left rotation of red and black trees

void _left_rotate(rbtree *T,rbtree_node *x){ rtree_node *y =x->right; // Precondition y is the right child of x x->right = y->left; //Point the right child of x to the left child of y if(y->left != T->nil) {// Judge whether the left child node of y is empty y->left->parent = x; // The parent of b points to x } y->parent = x->parent;// Point the parent node of y to the parent node of x if(x->parent == T->nil){ //Judge the parent node of x in three cases: 1 root, 2 left and 3 right. The parent node of x points to y T->root = y }else if(x == x->parent->left){ x-parent->left = y; //2 left }else { x->parent->right = y; //3 right } y->left = x; // At this time, the Y node has pointed the left child of y to x on the original parent node of x x->parent = y; // The parent node of x points to y }

Realize the right rotation of red and black trees

void _right_rotate(rbtree *T,rbtree_node *y){ rtree_node *x =y->left; // Precondition x is the left child of y y-left = x->right; if(x->right != T->nil) {// Judge whether the left child node of y is empty x->right->parent = y; // The parent of b points to y } x->parent = y->parent;// Point the parent node of x to the parent node of y if(y->parent == T->nil){ //Judge the parent node of y in three cases: 1 root, 2 left and 3 right. The parent node of y points to x T->root = x }else if(y == y->parent->left){ y-parent->left = x; //2 left }else { y->parent->right = x; //3 right } x->right= y; y->parent = x; }
3, Summary

The above is what we have mastered today. This paper only briefly introduces the purpose and implementation operation of red black tree, as well as the problems of insertion, deletion, coloring and rotation.

Reference > figure out the implementation of red black tree<

8 November 2021, 18:36 | Views: 6762

Add new comment

For adding a comment, please log in
or create account

0 comments