: This article mainly introduces the ngx_rbtree_t red/black tree. if you are interested in the PHP Tutorial, refer to it. Ngx_rbtree_t red/black tree
Features of the red/black tree
- The node is red or black;
- The root node is black;
- All leaf nodes are black (that is, NIL sentinel nodes );
- The two subnodes of each red node are black;
- All simple paths from any node to each of its leaf nodes contain the same number of black nodes.
Red/black tree node structure
Typedef ngx_uint_t regular; typedefstruct regular ngx_rbtree_node_t; structstruct ngx_rbtree_node_s {// unsigned integer keyword primary key; // left subnode Primary * left; // right subnode Primary * right; // parent node ngx_rbtree_node_t * parent; // node color, 0: black, 1: Red u_char color; // only one byte of data u_char data ;};
Place such a tree node in the first member of the element, which facilitates direct forced conversion.
I. e.
typedefstruct { ngx_rbtree_node_t node; ngx_uint_t num; }TestRBTreeBode;
Functions provided by the red/black tree node
Function name |
Parameter description |
Meaning |
Ngx_rbt_red (node) |
Node is a node pointer of the ngx_rbtree_node_t type. |
Set node to red |
Ngx_rbt_black (node) |
Node is a node pointer of the ngx_rbtree_node_t type. |
Set node to black |
Ngx_rbt_is_red (node) |
Node is a node pointer of the ngx_rbtree_node_t type. |
Judge whether the node is red |
Ngx_rbt_is_black (node) |
Node is a node pointer of the ngx_rbtree_node_t type. |
Judge whether the node is black |
Ngx_rbt_copy_color (n1, n2) |
N1 and n2 are the same as above |
Copy the node color of n2 to n1 |
Ngx_rbtree_node_t * ngx_rbtree_min (node, sentinel) |
Node and sentinel are of the ngx_rbtree_node_t * type. |
Find the minimum node in the current node and its subtree (by key) |
Ngx_rbtree_sentinel_init (node) |
Same as node |
Initialize the sentinel node |
Red/black tree structure
Typedefstruct ngx_rbtree_s ngx_rbtree_t;/* pointer */typedefvoid (* cursor) (ngx_rbtree_node_t * root, ngx_rbtree_node_t * node to solve element conflicts with the same keywords on different nodes, optional * sentinel); struct ngx_rbtree_s {// point to the root node of the tree (which can be directly converted to a data element) ngx_rbtree_node_t * root; // point to NIL sentinel node ngx_rbtree_node_t * sentinel; // ngx_rbtree_insert_pt insert ;};
Functions provided by the red/black tree container
Function name |
Parameter description |
Meaning |
Ngx_rbtree_init (tree, s, I) |
Tree is the container pointer; s is the Sentinel pointer; I is the addition function of the ngx_rbtree_insert_pt type. |
Initialize the red/black tree |
Void ngx_rbtree_insert (ngx_rbtree_t * tree, ngx_rbtree_node_t * node) |
Tree is the same as above; node is the added node pointer |
Add nodes to automatically rotate and maintain nodes |
Void ngx_rbtree_delete (ngx_rbtree_t * tree, ngx_rbtree_node_t * node) |
Tree is the same as above; node is the node pointer to be deleted |
Delete a node and automatically rotate and maintain the node. |
Copyright: Pain is just in your mind.
The above section introduces the ngx \ _ rbtree_t red/black tree, including some content, and hopes to help those who are interested in PHP tutorials.