Ngx_rbtree_t red/black tree

Source: Internet
Author: User
: 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

  1. The node is red or black;
  2. The root node is black;
  3. All leaf nodes are black (that is, NIL sentinel nodes );
  4. The two subnodes of each red node are black;
  5. 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.

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.