blob: 9fedfedfd85fc59e5becbcd2b332678d88560520 [file] [log] [blame]
Rob Landleyc742b532007-02-10 01:46:20 -08001Red-black Trees (rbtree) in Linux
2January 18, 2007
3Rob Landley <rob@landley.net>
4=============================
5
6What are red-black trees, and what are they for?
7------------------------------------------------
8
9Red-black trees are a type of self-balancing binary search tree, used for
10storing sortable key/value data pairs. This differs from radix trees (which
11are used to efficiently store sparse arrays and thus use long integer indexes
12to insert/access/delete nodes) and hash tables (which are not kept sorted to
13be easily traversed in order, and must be tuned for a specific size and
14hash function where rbtrees scale gracefully storing arbitrary keys).
15
16Red-black trees are similar to AVL trees, but provide faster real-time bounded
17worst case performance for insertion and deletion (at most two rotations and
18three rotations, respectively, to balance the tree), with slightly slower
19(but still O(log n)) lookup time.
20
21To quote Linux Weekly News:
22
23 There are a number of red-black trees in use in the kernel.
Randy Dunlap17a9e7b2010-11-11 12:09:59 +010024 The deadline and CFQ I/O schedulers employ rbtrees to
25 track requests; the packet CD/DVD driver does the same.
Rob Landleyc742b532007-02-10 01:46:20 -080026 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.
31
32This document covers use of the Linux rbtree implementation. For more
33information on the nature and implementation of Red Black Trees, see:
34
35 Linux Weekly News article on red-black trees
36 http://lwn.net/Articles/184495/
37
38 Wikipedia entry on red-black trees
39 http://en.wikipedia.org/wiki/Red-black_tree
40
41Linux implementation of red-black trees
42---------------------------------------
43
44Linux's rbtree implementation lives in the file "lib/rbtree.c". To use it,
45"#include <linux/rbtree.h>".
46
47The Linux rbtree implementation is optimized for speed, and thus has one
48less layer of indirection (and better cache locality) than more traditional
49tree implementations. Instead of using pointers to separate rb_node and data
50structures, each instance of struct rb_node is embedded in the data structure
51it organizes. And instead of using a comparison callback function pointer,
52users are expected to write their own tree search and insert functions
53which call the provided rbtree functions. Locking is also left up to the
54user of the rbtree code.
55
56Creating a new rbtree
57---------------------
58
59Data nodes in an rbtree tree are structures containing a struct rb_node member:
60
61 struct mytype {
62 struct rb_node node;
63 char *keystring;
64 };
65
66When dealing with a pointer to the embedded struct rb_node, the containing data
67structure may be accessed with the standard container_of() macro. In addition,
68individual members may be accessed directly via rb_entry(node, type, member).
69
70At the root of each rbtree is an rb_root structure, which is initialized to be
71empty via:
72
73 struct rb_root mytree = RB_ROOT;
74
75Searching for a value in an rbtree
76----------------------------------
77
78Writing a search function for your tree is fairly straightforward: start at the
79root, compare each value, and follow the left or right branch as necessary.
80
81Example:
82
83 struct mytype *my_search(struct rb_root *root, char *string)
84 {
85 struct rb_node *node = root->rb_node;
86
87 while (node) {
88 struct mytype *data = container_of(node, struct mytype, node);
89 int result;
90
91 result = strcmp(string, data->keystring);
92
93 if (result < 0)
94 node = node->rb_left;
95 else if (result > 0)
96 node = node->rb_right;
97 else
98 return data;
99 }
100 return NULL;
101 }
102
103Inserting data into an rbtree
104-----------------------------
105
106Inserting data in the tree involves first searching for the place to insert the
107new node, then inserting the node and rebalancing ("recoloring") the tree.
108
109The search for insertion differs from the previous search by finding the
110location of the pointer on which to graft the new node. The new node also
111needs a link to its parent node for rebalancing purposes.
112
113Example:
114
115 int my_insert(struct rb_root *root, struct mytype *data)
116 {
117 struct rb_node **new = &(root->rb_node), *parent = NULL;
118
119 /* Figure out where to put new node */
120 while (*new) {
121 struct mytype *this = container_of(*new, struct mytype, node);
122 int result = strcmp(data->keystring, this->keystring);
123
124 parent = *new;
125 if (result < 0)
126 new = &((*new)->rb_left);
127 else if (result > 0)
128 new = &((*new)->rb_right);
129 else
130 return FALSE;
131 }
132
133 /* Add new node and rebalance tree. */
figo.zhang27af1da2009-04-17 10:58:48 +0800134 rb_link_node(&data->node, parent, new);
135 rb_insert_color(&data->node, root);
Rob Landleyc742b532007-02-10 01:46:20 -0800136
137 return TRUE;
138 }
139
140Removing or replacing existing data in an rbtree
141------------------------------------------------
142
143To remove an existing node from a tree, call:
144
145 void rb_erase(struct rb_node *victim, struct rb_root *tree);
146
147Example:
148
figo.zhang27af1da2009-04-17 10:58:48 +0800149 struct mytype *data = mysearch(&mytree, "walrus");
Rob Landleyc742b532007-02-10 01:46:20 -0800150
151 if (data) {
figo.zhang27af1da2009-04-17 10:58:48 +0800152 rb_erase(&data->node, &mytree);
Rob Landleyc742b532007-02-10 01:46:20 -0800153 myfree(data);
154 }
155
156To replace an existing node in a tree with a new one with the same key, call:
157
158 void rb_replace_node(struct rb_node *old, struct rb_node *new,
159 struct rb_root *tree);
160
161Replacing a node this way does not re-sort the tree: If the new node doesn't
162have the same key as the old node, the rbtree will probably become corrupted.
163
164Iterating through the elements stored in an rbtree (in sort order)
165------------------------------------------------------------------
166
167Four functions are provided for iterating through an rbtree's contents in
168sorted order. These work on arbitrary trees, and should not need to be
169modified or wrapped (except for locking purposes):
170
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);
175
176To start iterating, call rb_first() or rb_last() with a pointer to the root
177of the tree, which will return a pointer to the node structure contained in
178the first or last element in the tree. To continue, fetch the next or previous
179node by calling rb_next() or rb_prev() on the current node. This will return
180NULL when there are no more nodes left.
181
182The iterator functions return a pointer to the embedded struct rb_node, from
183which the containing data structure may be accessed with the container_of()
184macro, and individual members may be accessed directly via
185rb_entry(node, type, member).
186
187Example:
188
189 struct rb_node *node;
190 for (node = rb_first(&mytree); node; node = rb_next(node))
Wang Tinggong19034232009-05-14 11:00:20 +0200191 printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
Rob Landleyc742b532007-02-10 01:46:20 -0800192
Davidlohr Buesoc89a7682022-01-24 17:33:03 +0100193Cached rbtrees
194--------------
195
196Computing the leftmost (smallest) node is quite a common task for binary
197search trees, such as for traversals or users relying on a the particular
198order for their own logic. To this end, users can use 'struct rb_root_cached'
199to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding
200potentially expensive tree iterations. This is done at negligible runtime
201overhead for maintanence; albeit larger memory footprint.
202
203Similar to the rb_root structure, cached rbtrees are initialized to be
204empty via:
205
206 struct rb_root_cached mytree = RB_ROOT_CACHED;
207
208Cached rbtree is simply a regular rb_root with an extra pointer to cache the
209leftmost node. This allows rb_root_cached to exist wherever rb_root does,
210which permits augmented trees to be supported as well as only a few extra
211interfaces:
212
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 *);
216
217Both insert and erase calls have their respective counterpart of augmented
218trees:
219
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 *);
224
225
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800226Support for Augmented rbtrees
227-----------------------------
228
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700229Augmented rbtree is an rbtree with "some" additional data stored in
230each node, where the additional data for node N must be a function of
231the contents of all nodes in the subtree rooted at N. This data can
232be used to augment some new functionality to rbtree. Augmented rbtree
233is an optional feature built on top of basic rbtree infrastructure.
234An rbtree user who wants this feature will have to call the augmentation
235functions with the user provided augmentation callback when inserting
236and erasing nodes.
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800237
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700238C files implementing augmented rbtree manipulation must include
Alexey Klimov121e0242015-09-06 02:13:34 +0300239<linux/rbtree_augmented.h> instead of <linux/rbtree.h>. Note that
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700240linux/rbtree_augmented.h exposes some rbtree implementations details
241you are not expected to rely on; please stick to the documented APIs
242there and do not include <linux/rbtree_augmented.h> from header files
243either so as to minimize chances of your users accidentally relying on
244such implementation details.
245
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700246On insertion, the user must update the augmented information on the path
247leading to the inserted node, then call rb_link_node() as usual and
248rb_augment_inserted() instead of the usual rb_insert_color() call.
249If rb_augment_inserted() rebalances the rbtree, it will callback into
250a user provided function to update the augmented information on the
251affected subtrees.
Sasha Levin2f175072011-07-24 11:23:20 +0300252
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700253When erasing a node, the user must call rb_erase_augmented() instead of
254rb_erase(). rb_erase_augmented() calls back into user provided functions
255to updated the augmented information on affected subtrees.
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800256
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700257In both cases, the callbacks are provided through struct rb_augment_callbacks.
2583 callbacks must be defined:
259
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).
263
264- A copy callback, which copies the augmented value for a given subtree
265 to a newly assigned subtree root.
266
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.
270
Michel Lespinasse9c079ad2012-10-08 16:31:33 -0700271The compiled code for rb_erase_augmented() may inline the propagation and
272copy callbacks, which results in a large function, so each augmented rbtree
273user should have a single rb_erase_augmented() call site in order to limit
274compiled code size.
275
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700276
277Sample usage:
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800278
279Interval tree is an example of augmented rb tree. Reference -
280"Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein.
281More details about interval trees:
282
283Classical rbtree has a single key and it cannot be directly used to store
284interval ranges like [lo:hi] and do a quick lookup for any overlap with a new
285lo:hi or to find whether there is an exact match for a new lo:hi.
286
287However, rbtree can be augmented to store such interval ranges in a structured
288way making it possible to do efficient lookup and exact match.
289
290This "extra information" stored in each node is the maximum hi
Carlos Garciac98be0c2014-04-04 22:31:00 -0400291(max_hi) value among all the nodes that are its descendants. This
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800292information can be maintained at each node just be looking at the node
293and its immediate children. And this will be used in O(log n) lookup
294for lowest match (lowest start address among all possible matches)
295with something like:
296
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700297struct interval_tree_node *
298interval_tree_first_match(struct rb_root *root,
299 unsigned long start, unsigned long last)
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800300{
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700301 struct interval_tree_node *node;
302
303 if (!root->rb_node)
304 return NULL;
305 node = rb_entry(root->rb_node, struct interval_tree_node, rb);
306
307 while (true) {
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) {
313 /*
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.
320 */
321 node = left;
322 continue;
323 }
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800324 }
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700325 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)
332 continue;
333 }
334 }
335 return NULL; /* No match */
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800336 }
Pallipadi, Venkatesh17d9ddc2010-02-10 15:23:44 -0800337}
338
Michel Lespinasse14b94af2012-10-08 16:31:17 -0700339Insertion/removal are defined using the following augmented callbacks:
340
341static inline unsigned long
342compute_subtree_last(struct interval_tree_node *node)
343{
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)
349 max = subtree_last;
350 }
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)
355 max = subtree_last;
356 }
357 return max;
358}
359
360static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
361{
362 while (rb != 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)
367 break;
368 node->__subtree_last = subtree_last;
369 rb = rb_parent(&node->rb);
370 }
371}
372
373static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
374{
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);
379
380 new->__subtree_last = old->__subtree_last;
381}
382
383static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
384{
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);
389
390 new->__subtree_last = old->__subtree_last;
391 old->__subtree_last = compute_subtree_last(old);
392}
393
394static const struct rb_augment_callbacks augment_callbacks = {
395 augment_propagate, augment_copy, augment_rotate
396};
397
398void interval_tree_insert(struct interval_tree_node *node,
399 struct rb_root *root)
400{
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;
404
405 while (*link) {
406 rb_parent = *link;
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;
412 else
413 link = &parent->rb.rb_right;
414 }
415
416 node->__subtree_last = last;
417 rb_link_node(&node->rb, rb_parent, link);
418 rb_insert_augmented(&node->rb, root, &augment_callbacks);
419}
420
421void interval_tree_remove(struct interval_tree_node *node,
422 struct rb_root *root)
423{
424 rb_erase_augmented(&node->rb, root, &augment_callbacks);
425}