0001 =================================
0002 Red-black Trees (rbtree) in Linux
0003 =================================
0004
0005
0006 :Date: January 18, 2007
0007 :Author: Rob Landley <rob@landley.net>
0008
0009 What are red-black trees, and what are they for?
0010 ------------------------------------------------
0011
0012 Red-black trees are a type of self-balancing binary search tree, used for
0013 storing sortable key/value data pairs. This differs from radix trees (which
0014 are used to efficiently store sparse arrays and thus use long integer indexes
0015 to insert/access/delete nodes) and hash tables (which are not kept sorted to
0016 be easily traversed in order, and must be tuned for a specific size and
0017 hash function where rbtrees scale gracefully storing arbitrary keys).
0018
0019 Red-black trees are similar to AVL trees, but provide faster real-time bounded
0020 worst case performance for insertion and deletion (at most two rotations and
0021 three rotations, respectively, to balance the tree), with slightly slower
0022 (but still O(log n)) lookup time.
0023
0024 To quote Linux Weekly News:
0025
0026 There are a number of red-black trees in use in the kernel.
0027 The deadline and CFQ I/O schedulers employ rbtrees to
0028 track requests; the packet CD/DVD driver does the same.
0029 The high-resolution timer code uses an rbtree to organize outstanding
0030 timer requests. The ext3 filesystem tracks directory entries in a
0031 red-black tree. Virtual memory areas (VMAs) are tracked with red-black
0032 trees, as are epoll file descriptors, cryptographic keys, and network
0033 packets in the "hierarchical token bucket" scheduler.
0034
0035 This document covers use of the Linux rbtree implementation. For more
0036 information on the nature and implementation of Red Black Trees, see:
0037
0038 Linux Weekly News article on red-black trees
0039 https://lwn.net/Articles/184495/
0040
0041 Wikipedia entry on red-black trees
0042 https://en.wikipedia.org/wiki/Red-black_tree
0043
0044 Linux implementation of red-black trees
0045 ---------------------------------------
0046
0047 Linux's rbtree implementation lives in the file "lib/rbtree.c". To use it,
0048 "#include <linux/rbtree.h>".
0049
0050 The Linux rbtree implementation is optimized for speed, and thus has one
0051 less layer of indirection (and better cache locality) than more traditional
0052 tree implementations. Instead of using pointers to separate rb_node and data
0053 structures, each instance of struct rb_node is embedded in the data structure
0054 it organizes. And instead of using a comparison callback function pointer,
0055 users are expected to write their own tree search and insert functions
0056 which call the provided rbtree functions. Locking is also left up to the
0057 user of the rbtree code.
0058
0059 Creating a new rbtree
0060 ---------------------
0061
0062 Data nodes in an rbtree tree are structures containing a struct rb_node member::
0063
0064 struct mytype {
0065 struct rb_node node;
0066 char *keystring;
0067 };
0068
0069 When dealing with a pointer to the embedded struct rb_node, the containing data
0070 structure may be accessed with the standard container_of() macro. In addition,
0071 individual members may be accessed directly via rb_entry(node, type, member).
0072
0073 At the root of each rbtree is an rb_root structure, which is initialized to be
0074 empty via:
0075
0076 struct rb_root mytree = RB_ROOT;
0077
0078 Searching for a value in an rbtree
0079 ----------------------------------
0080
0081 Writing a search function for your tree is fairly straightforward: start at the
0082 root, compare each value, and follow the left or right branch as necessary.
0083
0084 Example::
0085
0086 struct mytype *my_search(struct rb_root *root, char *string)
0087 {
0088 struct rb_node *node = root->rb_node;
0089
0090 while (node) {
0091 struct mytype *data = container_of(node, struct mytype, node);
0092 int result;
0093
0094 result = strcmp(string, data->keystring);
0095
0096 if (result < 0)
0097 node = node->rb_left;
0098 else if (result > 0)
0099 node = node->rb_right;
0100 else
0101 return data;
0102 }
0103 return NULL;
0104 }
0105
0106 Inserting data into an rbtree
0107 -----------------------------
0108
0109 Inserting data in the tree involves first searching for the place to insert the
0110 new node, then inserting the node and rebalancing ("recoloring") the tree.
0111
0112 The search for insertion differs from the previous search by finding the
0113 location of the pointer on which to graft the new node. The new node also
0114 needs a link to its parent node for rebalancing purposes.
0115
0116 Example::
0117
0118 int my_insert(struct rb_root *root, struct mytype *data)
0119 {
0120 struct rb_node **new = &(root->rb_node), *parent = NULL;
0121
0122 /* Figure out where to put new node */
0123 while (*new) {
0124 struct mytype *this = container_of(*new, struct mytype, node);
0125 int result = strcmp(data->keystring, this->keystring);
0126
0127 parent = *new;
0128 if (result < 0)
0129 new = &((*new)->rb_left);
0130 else if (result > 0)
0131 new = &((*new)->rb_right);
0132 else
0133 return FALSE;
0134 }
0135
0136 /* Add new node and rebalance tree. */
0137 rb_link_node(&data->node, parent, new);
0138 rb_insert_color(&data->node, root);
0139
0140 return TRUE;
0141 }
0142
0143 Removing or replacing existing data in an rbtree
0144 ------------------------------------------------
0145
0146 To remove an existing node from a tree, call::
0147
0148 void rb_erase(struct rb_node *victim, struct rb_root *tree);
0149
0150 Example::
0151
0152 struct mytype *data = mysearch(&mytree, "walrus");
0153
0154 if (data) {
0155 rb_erase(&data->node, &mytree);
0156 myfree(data);
0157 }
0158
0159 To replace an existing node in a tree with a new one with the same key, call::
0160
0161 void rb_replace_node(struct rb_node *old, struct rb_node *new,
0162 struct rb_root *tree);
0163
0164 Replacing a node this way does not re-sort the tree: If the new node doesn't
0165 have the same key as the old node, the rbtree will probably become corrupted.
0166
0167 Iterating through the elements stored in an rbtree (in sort order)
0168 ------------------------------------------------------------------
0169
0170 Four functions are provided for iterating through an rbtree's contents in
0171 sorted order. These work on arbitrary trees, and should not need to be
0172 modified or wrapped (except for locking purposes)::
0173
0174 struct rb_node *rb_first(struct rb_root *tree);
0175 struct rb_node *rb_last(struct rb_root *tree);
0176 struct rb_node *rb_next(struct rb_node *node);
0177 struct rb_node *rb_prev(struct rb_node *node);
0178
0179 To start iterating, call rb_first() or rb_last() with a pointer to the root
0180 of the tree, which will return a pointer to the node structure contained in
0181 the first or last element in the tree. To continue, fetch the next or previous
0182 node by calling rb_next() or rb_prev() on the current node. This will return
0183 NULL when there are no more nodes left.
0184
0185 The iterator functions return a pointer to the embedded struct rb_node, from
0186 which the containing data structure may be accessed with the container_of()
0187 macro, and individual members may be accessed directly via
0188 rb_entry(node, type, member).
0189
0190 Example::
0191
0192 struct rb_node *node;
0193 for (node = rb_first(&mytree); node; node = rb_next(node))
0194 printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
0195
0196 Cached rbtrees
0197 --------------
0198
0199 Computing the leftmost (smallest) node is quite a common task for binary
0200 search trees, such as for traversals or users relying on a the particular
0201 order for their own logic. To this end, users can use 'struct rb_root_cached'
0202 to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding
0203 potentially expensive tree iterations. This is done at negligible runtime
0204 overhead for maintenance; albeit larger memory footprint.
0205
0206 Similar to the rb_root structure, cached rbtrees are initialized to be
0207 empty via::
0208
0209 struct rb_root_cached mytree = RB_ROOT_CACHED;
0210
0211 Cached rbtree is simply a regular rb_root with an extra pointer to cache the
0212 leftmost node. This allows rb_root_cached to exist wherever rb_root does,
0213 which permits augmented trees to be supported as well as only a few extra
0214 interfaces::
0215
0216 struct rb_node *rb_first_cached(struct rb_root_cached *tree);
0217 void rb_insert_color_cached(struct rb_node *, struct rb_root_cached *, bool);
0218 void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
0219
0220 Both insert and erase calls have their respective counterpart of augmented
0221 trees::
0222
0223 void rb_insert_augmented_cached(struct rb_node *node, struct rb_root_cached *,
0224 bool, struct rb_augment_callbacks *);
0225 void rb_erase_augmented_cached(struct rb_node *, struct rb_root_cached *,
0226 struct rb_augment_callbacks *);
0227
0228
0229 Support for Augmented rbtrees
0230 -----------------------------
0231
0232 Augmented rbtree is an rbtree with "some" additional data stored in
0233 each node, where the additional data for node N must be a function of
0234 the contents of all nodes in the subtree rooted at N. This data can
0235 be used to augment some new functionality to rbtree. Augmented rbtree
0236 is an optional feature built on top of basic rbtree infrastructure.
0237 An rbtree user who wants this feature will have to call the augmentation
0238 functions with the user provided augmentation callback when inserting
0239 and erasing nodes.
0240
0241 C files implementing augmented rbtree manipulation must include
0242 <linux/rbtree_augmented.h> instead of <linux/rbtree.h>. Note that
0243 linux/rbtree_augmented.h exposes some rbtree implementations details
0244 you are not expected to rely on; please stick to the documented APIs
0245 there and do not include <linux/rbtree_augmented.h> from header files
0246 either so as to minimize chances of your users accidentally relying on
0247 such implementation details.
0248
0249 On insertion, the user must update the augmented information on the path
0250 leading to the inserted node, then call rb_link_node() as usual and
0251 rb_augment_inserted() instead of the usual rb_insert_color() call.
0252 If rb_augment_inserted() rebalances the rbtree, it will callback into
0253 a user provided function to update the augmented information on the
0254 affected subtrees.
0255
0256 When erasing a node, the user must call rb_erase_augmented() instead of
0257 rb_erase(). rb_erase_augmented() calls back into user provided functions
0258 to updated the augmented information on affected subtrees.
0259
0260 In both cases, the callbacks are provided through struct rb_augment_callbacks.
0261 3 callbacks must be defined:
0262
0263 - A propagation callback, which updates the augmented value for a given
0264 node and its ancestors, up to a given stop point (or NULL to update
0265 all the way to the root).
0266
0267 - A copy callback, which copies the augmented value for a given subtree
0268 to a newly assigned subtree root.
0269
0270 - A tree rotation callback, which copies the augmented value for a given
0271 subtree to a newly assigned subtree root AND recomputes the augmented
0272 information for the former subtree root.
0273
0274 The compiled code for rb_erase_augmented() may inline the propagation and
0275 copy callbacks, which results in a large function, so each augmented rbtree
0276 user should have a single rb_erase_augmented() call site in order to limit
0277 compiled code size.
0278
0279
0280 Sample usage
0281 ^^^^^^^^^^^^
0282
0283 Interval tree is an example of augmented rb tree. Reference -
0284 "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein.
0285 More details about interval trees:
0286
0287 Classical rbtree has a single key and it cannot be directly used to store
0288 interval ranges like [lo:hi] and do a quick lookup for any overlap with a new
0289 lo:hi or to find whether there is an exact match for a new lo:hi.
0290
0291 However, rbtree can be augmented to store such interval ranges in a structured
0292 way making it possible to do efficient lookup and exact match.
0293
0294 This "extra information" stored in each node is the maximum hi
0295 (max_hi) value among all the nodes that are its descendants. This
0296 information can be maintained at each node just be looking at the node
0297 and its immediate children. And this will be used in O(log n) lookup
0298 for lowest match (lowest start address among all possible matches)
0299 with something like::
0300
0301 struct interval_tree_node *
0302 interval_tree_first_match(struct rb_root *root,
0303 unsigned long start, unsigned long last)
0304 {
0305 struct interval_tree_node *node;
0306
0307 if (!root->rb_node)
0308 return NULL;
0309 node = rb_entry(root->rb_node, struct interval_tree_node, rb);
0310
0311 while (true) {
0312 if (node->rb.rb_left) {
0313 struct interval_tree_node *left =
0314 rb_entry(node->rb.rb_left,
0315 struct interval_tree_node, rb);
0316 if (left->__subtree_last >= start) {
0317 /*
0318 * Some nodes in left subtree satisfy Cond2.
0319 * Iterate to find the leftmost such node N.
0320 * If it also satisfies Cond1, that's the match
0321 * we are looking for. Otherwise, there is no
0322 * matching interval as nodes to the right of N
0323 * can't satisfy Cond1 either.
0324 */
0325 node = left;
0326 continue;
0327 }
0328 }
0329 if (node->start <= last) { /* Cond1 */
0330 if (node->last >= start) /* Cond2 */
0331 return node; /* node is leftmost match */
0332 if (node->rb.rb_right) {
0333 node = rb_entry(node->rb.rb_right,
0334 struct interval_tree_node, rb);
0335 if (node->__subtree_last >= start)
0336 continue;
0337 }
0338 }
0339 return NULL; /* No match */
0340 }
0341 }
0342
0343 Insertion/removal are defined using the following augmented callbacks::
0344
0345 static inline unsigned long
0346 compute_subtree_last(struct interval_tree_node *node)
0347 {
0348 unsigned long max = node->last, subtree_last;
0349 if (node->rb.rb_left) {
0350 subtree_last = rb_entry(node->rb.rb_left,
0351 struct interval_tree_node, rb)->__subtree_last;
0352 if (max < subtree_last)
0353 max = subtree_last;
0354 }
0355 if (node->rb.rb_right) {
0356 subtree_last = rb_entry(node->rb.rb_right,
0357 struct interval_tree_node, rb)->__subtree_last;
0358 if (max < subtree_last)
0359 max = subtree_last;
0360 }
0361 return max;
0362 }
0363
0364 static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
0365 {
0366 while (rb != stop) {
0367 struct interval_tree_node *node =
0368 rb_entry(rb, struct interval_tree_node, rb);
0369 unsigned long subtree_last = compute_subtree_last(node);
0370 if (node->__subtree_last == subtree_last)
0371 break;
0372 node->__subtree_last = subtree_last;
0373 rb = rb_parent(&node->rb);
0374 }
0375 }
0376
0377 static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
0378 {
0379 struct interval_tree_node *old =
0380 rb_entry(rb_old, struct interval_tree_node, rb);
0381 struct interval_tree_node *new =
0382 rb_entry(rb_new, struct interval_tree_node, rb);
0383
0384 new->__subtree_last = old->__subtree_last;
0385 }
0386
0387 static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
0388 {
0389 struct interval_tree_node *old =
0390 rb_entry(rb_old, struct interval_tree_node, rb);
0391 struct interval_tree_node *new =
0392 rb_entry(rb_new, struct interval_tree_node, rb);
0393
0394 new->__subtree_last = old->__subtree_last;
0395 old->__subtree_last = compute_subtree_last(old);
0396 }
0397
0398 static const struct rb_augment_callbacks augment_callbacks = {
0399 augment_propagate, augment_copy, augment_rotate
0400 };
0401
0402 void interval_tree_insert(struct interval_tree_node *node,
0403 struct rb_root *root)
0404 {
0405 struct rb_node **link = &root->rb_node, *rb_parent = NULL;
0406 unsigned long start = node->start, last = node->last;
0407 struct interval_tree_node *parent;
0408
0409 while (*link) {
0410 rb_parent = *link;
0411 parent = rb_entry(rb_parent, struct interval_tree_node, rb);
0412 if (parent->__subtree_last < last)
0413 parent->__subtree_last = last;
0414 if (start < parent->start)
0415 link = &parent->rb.rb_left;
0416 else
0417 link = &parent->rb.rb_right;
0418 }
0419
0420 node->__subtree_last = last;
0421 rb_link_node(&node->rb, rb_parent, link);
0422 rb_insert_augmented(&node->rb, root, &augment_callbacks);
0423 }
0424
0425 void interval_tree_remove(struct interval_tree_node *node,
0426 struct rb_root *root)
0427 {
0428 rb_erase_augmented(&node->rb, root, &augment_callbacks);
0429 }