1 Red-black Trees (rbtree) in Linux
3 Rob Landley <rob@landley.net>
4 =============================
6 What are red-black trees, and what are they for?
7 ------------------------------------------------
9 Red-black trees are a type of self-balancing binary search tree, used for
10 storing sortable key/value data pairs. This differs from radix trees (which
11 are used to efficiently store sparse arrays and thus use long integer indexes
12 to insert/access/delete nodes) and hash tables (which are not kept sorted to
13 be easily traversed in order, and must be tuned for a specific size and
14 hash function where rbtrees scale gracefully storing arbitrary keys).
16 Red-black trees are similar to AVL trees, but provide faster real-time bounded
17 worst case performance for insertion and deletion (at most two rotations and
18 three rotations, respectively, to balance the tree), with slightly slower
19 (but still O(log n)) lookup time.
21 To quote Linux Weekly News:
23 There are a number of red-black trees in use in the kernel.
24 The deadline and CFQ I/O schedulers employ rbtrees to
25 track requests; the packet CD/DVD driver does the same.
26 The high-resolution timer code uses an rbtree to organize outstanding
27 timer requests. The ext3 filesystem tracks directory entries in a
28 red-black tree. Virtual memory areas (VMAs) are tracked with red-black
29 trees, as are epoll file descriptors, cryptographic keys, and network
30 packets in the "hierarchical token bucket" scheduler.
32 This document covers use of the Linux rbtree implementation. For more
33 information on the nature and implementation of Red Black Trees, see:
35 Linux Weekly News article on red-black trees
36 http://lwn.net/Articles/184495/
38 Wikipedia entry on red-black trees
39 http://en.wikipedia.org/wiki/Red-black_tree
41 Linux implementation of red-black trees
42 ---------------------------------------
44 Linux's rbtree implementation lives in the file "lib/rbtree.c". To use it,
45 "#include <linux/rbtree.h>".
47 The Linux rbtree implementation is optimized for speed, and thus has one
48 less layer of indirection (and better cache locality) than more traditional
49 tree implementations. Instead of using pointers to separate rb_node and data
50 structures, each instance of struct rb_node is embedded in the data structure
51 it organizes. And instead of using a comparison callback function pointer,
52 users are expected to write their own tree search and insert functions
53 which call the provided rbtree functions. Locking is also left up to the
54 user of the rbtree code.
59 Data nodes in an rbtree tree are structures containing a struct rb_node member:
66 When dealing with a pointer to the embedded struct rb_node, the containing data
67 structure may be accessed with the standard container_of() macro. In addition,
68 individual members may be accessed directly via rb_entry(node, type, member).
70 At the root of each rbtree is an rb_root structure, which is initialized to be
73 struct rb_root mytree = RB_ROOT;
75 Searching for a value in an rbtree
76 ----------------------------------
78 Writing a search function for your tree is fairly straightforward: start at the
79 root, compare each value, and follow the left or right branch as necessary.
83 struct mytype *my_search(struct rb_root *root, char *string)
85 struct rb_node *node = root->rb_node;
88 struct mytype *data = container_of(node, struct mytype, node);
91 result = strcmp(string, data->keystring);
96 node = node->rb_right;
103 Inserting data into an rbtree
104 -----------------------------
106 Inserting data in the tree involves first searching for the place to insert the
107 new node, then inserting the node and rebalancing ("recoloring") the tree.
109 The search for insertion differs from the previous search by finding the
110 location of the pointer on which to graft the new node. The new node also
111 needs a link to its parent node for rebalancing purposes.
115 int my_insert(struct rb_root *root, struct mytype *data)
117 struct rb_node **new = &(root->rb_node), *parent = NULL;
119 /* Figure out where to put new node */
121 struct mytype *this = container_of(*new, struct mytype, node);
122 int result = strcmp(data->keystring, this->keystring);
126 new = &((*new)->rb_left);
128 new = &((*new)->rb_right);
133 /* Add new node and rebalance tree. */
134 rb_link_node(&data->node, parent, new);
135 rb_insert_color(&data->node, root);
140 Removing or replacing existing data in an rbtree
141 ------------------------------------------------
143 To remove an existing node from a tree, call:
145 void rb_erase(struct rb_node *victim, struct rb_root *tree);
149 struct mytype *data = mysearch(&mytree, "walrus");
152 rb_erase(&data->node, &mytree);
156 To replace an existing node in a tree with a new one with the same key, call:
158 void rb_replace_node(struct rb_node *old, struct rb_node *new,
159 struct rb_root *tree);
161 Replacing a node this way does not re-sort the tree: If the new node doesn't
162 have the same key as the old node, the rbtree will probably become corrupted.
164 Iterating through the elements stored in an rbtree (in sort order)
165 ------------------------------------------------------------------
167 Four functions are provided for iterating through an rbtree's contents in
168 sorted order. These work on arbitrary trees, and should not need to be
169 modified or wrapped (except for locking purposes):
171 struct rb_node *rb_first(struct rb_root *tree);
172 struct rb_node *rb_last(struct rb_root *tree);
173 struct rb_node *rb_next(struct rb_node *node);
174 struct rb_node *rb_prev(struct rb_node *node);
176 To start iterating, call rb_first() or rb_last() with a pointer to the root
177 of the tree, which will return a pointer to the node structure contained in
178 the first or last element in the tree. To continue, fetch the next or previous
179 node by calling rb_next() or rb_prev() on the current node. This will return
180 NULL when there are no more nodes left.
182 The iterator functions return a pointer to the embedded struct rb_node, from
183 which the containing data structure may be accessed with the container_of()
184 macro, and individual members may be accessed directly via
185 rb_entry(node, type, member).
189 struct rb_node *node;
190 for (node = rb_first(&mytree); node; node = rb_next(node))
191 printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
196 Computing the leftmost (smallest) node is quite a common task for binary
197 search trees, such as for traversals or users relying on a the particular
198 order for their own logic. To this end, users can use 'struct rb_root_cached'
199 to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding
200 potentially expensive tree iterations. This is done at negligible runtime
201 overhead for maintanence; albeit larger memory footprint.
203 Similar to the rb_root structure, cached rbtrees are initialized to be
206 struct rb_root_cached mytree = RB_ROOT_CACHED;
208 Cached rbtree is simply a regular rb_root with an extra pointer to cache the
209 leftmost node. This allows rb_root_cached to exist wherever rb_root does,
210 which permits augmented trees to be supported as well as only a few extra
213 struct rb_node *rb_first_cached(struct rb_root_cached *tree);
214 void rb_insert_color_cached(struct rb_node *, struct rb_root_cached *, bool);
215 void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
217 Both insert and erase calls have their respective counterpart of augmented
220 void rb_insert_augmented_cached(struct rb_node *node, struct rb_root_cached *,
221 bool, struct rb_augment_callbacks *);
222 void rb_erase_augmented_cached(struct rb_node *, struct rb_root_cached *,
223 struct rb_augment_callbacks *);
226 Support for Augmented rbtrees
227 -----------------------------
229 Augmented rbtree is an rbtree with "some" additional data stored in
230 each node, where the additional data for node N must be a function of
231 the contents of all nodes in the subtree rooted at N. This data can
232 be used to augment some new functionality to rbtree. Augmented rbtree
233 is an optional feature built on top of basic rbtree infrastructure.
234 An rbtree user who wants this feature will have to call the augmentation
235 functions with the user provided augmentation callback when inserting
238 C files implementing augmented rbtree manipulation must include
239 <linux/rbtree_augmented.h> instead of <linux/rbtree.h>. Note that
240 linux/rbtree_augmented.h exposes some rbtree implementations details
241 you are not expected to rely on; please stick to the documented APIs
242 there and do not include <linux/rbtree_augmented.h> from header files
243 either so as to minimize chances of your users accidentally relying on
244 such implementation details.
246 On insertion, the user must update the augmented information on the path
247 leading to the inserted node, then call rb_link_node() as usual and
248 rb_augment_inserted() instead of the usual rb_insert_color() call.
249 If rb_augment_inserted() rebalances the rbtree, it will callback into
250 a user provided function to update the augmented information on the
253 When erasing a node, the user must call rb_erase_augmented() instead of
254 rb_erase(). rb_erase_augmented() calls back into user provided functions
255 to updated the augmented information on affected subtrees.
257 In both cases, the callbacks are provided through struct rb_augment_callbacks.
258 3 callbacks must be defined:
260 - A propagation callback, which updates the augmented value for a given
261 node and its ancestors, up to a given stop point (or NULL to update
262 all the way to the root).
264 - A copy callback, which copies the augmented value for a given subtree
265 to a newly assigned subtree root.
267 - A tree rotation callback, which copies the augmented value for a given
268 subtree to a newly assigned subtree root AND recomputes the augmented
269 information for the former subtree root.
271 The compiled code for rb_erase_augmented() may inline the propagation and
272 copy callbacks, which results in a large function, so each augmented rbtree
273 user should have a single rb_erase_augmented() call site in order to limit
279 Interval tree is an example of augmented rb tree. Reference -
280 "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein.
281 More details about interval trees:
283 Classical rbtree has a single key and it cannot be directly used to store
284 interval ranges like [lo:hi] and do a quick lookup for any overlap with a new
285 lo:hi or to find whether there is an exact match for a new lo:hi.
287 However, rbtree can be augmented to store such interval ranges in a structured
288 way making it possible to do efficient lookup and exact match.
290 This "extra information" stored in each node is the maximum hi
291 (max_hi) value among all the nodes that are its descendants. This
292 information can be maintained at each node just be looking at the node
293 and its immediate children. And this will be used in O(log n) lookup
294 for lowest match (lowest start address among all possible matches)
297 struct interval_tree_node *
298 interval_tree_first_match(struct rb_root *root,
299 unsigned long start, unsigned long last)
301 struct interval_tree_node *node;
305 node = rb_entry(root->rb_node, struct interval_tree_node, rb);
308 if (node->rb.rb_left) {
309 struct interval_tree_node *left =
310 rb_entry(node->rb.rb_left,
311 struct interval_tree_node, rb);
312 if (left->__subtree_last >= start) {
314 * Some nodes in left subtree satisfy Cond2.
315 * Iterate to find the leftmost such node N.
316 * If it also satisfies Cond1, that's the match
317 * we are looking for. Otherwise, there is no
318 * matching interval as nodes to the right of N
319 * can't satisfy Cond1 either.
325 if (node->start <= last) { /* Cond1 */
326 if (node->last >= start) /* Cond2 */
327 return node; /* node is leftmost match */
328 if (node->rb.rb_right) {
329 node = rb_entry(node->rb.rb_right,
330 struct interval_tree_node, rb);
331 if (node->__subtree_last >= start)
335 return NULL; /* No match */
339 Insertion/removal are defined using the following augmented callbacks:
341 static inline unsigned long
342 compute_subtree_last(struct interval_tree_node *node)
344 unsigned long max = node->last, subtree_last;
345 if (node->rb.rb_left) {
346 subtree_last = rb_entry(node->rb.rb_left,
347 struct interval_tree_node, rb)->__subtree_last;
348 if (max < subtree_last)
351 if (node->rb.rb_right) {
352 subtree_last = rb_entry(node->rb.rb_right,
353 struct interval_tree_node, rb)->__subtree_last;
354 if (max < subtree_last)
360 static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
363 struct interval_tree_node *node =
364 rb_entry(rb, struct interval_tree_node, rb);
365 unsigned long subtree_last = compute_subtree_last(node);
366 if (node->__subtree_last == subtree_last)
368 node->__subtree_last = subtree_last;
369 rb = rb_parent(&node->rb);
373 static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
375 struct interval_tree_node *old =
376 rb_entry(rb_old, struct interval_tree_node, rb);
377 struct interval_tree_node *new =
378 rb_entry(rb_new, struct interval_tree_node, rb);
380 new->__subtree_last = old->__subtree_last;
383 static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
385 struct interval_tree_node *old =
386 rb_entry(rb_old, struct interval_tree_node, rb);
387 struct interval_tree_node *new =
388 rb_entry(rb_new, struct interval_tree_node, rb);
390 new->__subtree_last = old->__subtree_last;
391 old->__subtree_last = compute_subtree_last(old);
394 static const struct rb_augment_callbacks augment_callbacks = {
395 augment_propagate, augment_copy, augment_rotate
398 void interval_tree_insert(struct interval_tree_node *node,
399 struct rb_root *root)
401 struct rb_node **link = &root->rb_node, *rb_parent = NULL;
402 unsigned long start = node->start, last = node->last;
403 struct interval_tree_node *parent;
407 parent = rb_entry(rb_parent, struct interval_tree_node, rb);
408 if (parent->__subtree_last < last)
409 parent->__subtree_last = last;
410 if (start < parent->start)
411 link = &parent->rb.rb_left;
413 link = &parent->rb.rb_right;
416 node->__subtree_last = last;
417 rb_link_node(&node->rb, rb_parent, link);
418 rb_insert_augmented(&node->rb, root, &augment_callbacks);
421 void interval_tree_remove(struct interval_tree_node *node,
422 struct rb_root *root)
424 rb_erase_augmented(&node->rb, root, &augment_callbacks);