rbtree.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. =================================
  2. Red-black Trees (rbtree) in Linux
  3. =================================
  4. :Date: January 18, 2007
  5. :Author: Rob Landley <rob@landley.net>
  6. What are red-black trees, and what are they for?
  7. ------------------------------------------------
  8. Red-black trees are a type of self-balancing binary search tree, used for
  9. storing sortable key/value data pairs. This differs from radix trees (which
  10. are used to efficiently store sparse arrays and thus use long integer indexes
  11. to insert/access/delete nodes) and hash tables (which are not kept sorted to
  12. be easily traversed in order, and must be tuned for a specific size and
  13. hash function where rbtrees scale gracefully storing arbitrary keys).
  14. Red-black trees are similar to AVL trees, but provide faster real-time bounded
  15. worst case performance for insertion and deletion (at most two rotations and
  16. three rotations, respectively, to balance the tree), with slightly slower
  17. (but still O(log n)) lookup time.
  18. To quote Linux Weekly News:
  19. There are a number of red-black trees in use in the kernel.
  20. The deadline and CFQ I/O schedulers employ rbtrees to
  21. track requests; the packet CD/DVD driver does the same.
  22. The high-resolution timer code uses an rbtree to organize outstanding
  23. timer requests. The ext3 filesystem tracks directory entries in a
  24. red-black tree. Virtual memory areas (VMAs) are tracked with red-black
  25. trees, as are epoll file descriptors, cryptographic keys, and network
  26. packets in the "hierarchical token bucket" scheduler.
  27. This document covers use of the Linux rbtree implementation. For more
  28. information on the nature and implementation of Red Black Trees, see:
  29. Linux Weekly News article on red-black trees
  30. https://lwn.net/Articles/184495/
  31. Wikipedia entry on red-black trees
  32. https://en.wikipedia.org/wiki/Red-black_tree
  33. Linux implementation of red-black trees
  34. ---------------------------------------
  35. Linux's rbtree implementation lives in the file "lib/rbtree.c". To use it,
  36. "#include <linux/rbtree.h>".
  37. The Linux rbtree implementation is optimized for speed, and thus has one
  38. less layer of indirection (and better cache locality) than more traditional
  39. tree implementations. Instead of using pointers to separate rb_node and data
  40. structures, each instance of struct rb_node is embedded in the data structure
  41. it organizes. And instead of using a comparison callback function pointer,
  42. users are expected to write their own tree search and insert functions
  43. which call the provided rbtree functions. Locking is also left up to the
  44. user of the rbtree code.
  45. Creating a new rbtree
  46. ---------------------
  47. Data nodes in an rbtree tree are structures containing a struct rb_node member::
  48. struct mytype {
  49. struct rb_node node;
  50. char *keystring;
  51. };
  52. When dealing with a pointer to the embedded struct rb_node, the containing data
  53. structure may be accessed with the standard container_of() macro. In addition,
  54. individual members may be accessed directly via rb_entry(node, type, member).
  55. At the root of each rbtree is an rb_root structure, which is initialized to be
  56. empty via:
  57. struct rb_root mytree = RB_ROOT;
  58. Searching for a value in an rbtree
  59. ----------------------------------
  60. Writing a search function for your tree is fairly straightforward: start at the
  61. root, compare each value, and follow the left or right branch as necessary.
  62. Example::
  63. struct mytype *my_search(struct rb_root *root, char *string)
  64. {
  65. struct rb_node *node = root->rb_node;
  66. while (node) {
  67. struct mytype *data = container_of(node, struct mytype, node);
  68. int result;
  69. result = strcmp(string, data->keystring);
  70. if (result < 0)
  71. node = node->rb_left;
  72. else if (result > 0)
  73. node = node->rb_right;
  74. else
  75. return data;
  76. }
  77. return NULL;
  78. }
  79. Inserting data into an rbtree
  80. -----------------------------
  81. Inserting data in the tree involves first searching for the place to insert the
  82. new node, then inserting the node and rebalancing ("recoloring") the tree.
  83. The search for insertion differs from the previous search by finding the
  84. location of the pointer on which to graft the new node. The new node also
  85. needs a link to its parent node for rebalancing purposes.
  86. Example::
  87. int my_insert(struct rb_root *root, struct mytype *data)
  88. {
  89. struct rb_node **new = &(root->rb_node), *parent = NULL;
  90. /* Figure out where to put new node */
  91. while (*new) {
  92. struct mytype *this = container_of(*new, struct mytype, node);
  93. int result = strcmp(data->keystring, this->keystring);
  94. parent = *new;
  95. if (result < 0)
  96. new = &((*new)->rb_left);
  97. else if (result > 0)
  98. new = &((*new)->rb_right);
  99. else
  100. return FALSE;
  101. }
  102. /* Add new node and rebalance tree. */
  103. rb_link_node(&data->node, parent, new);
  104. rb_insert_color(&data->node, root);
  105. return TRUE;
  106. }
  107. Removing or replacing existing data in an rbtree
  108. ------------------------------------------------
  109. To remove an existing node from a tree, call::
  110. void rb_erase(struct rb_node *victim, struct rb_root *tree);
  111. Example::
  112. struct mytype *data = mysearch(&mytree, "walrus");
  113. if (data) {
  114. rb_erase(&data->node, &mytree);
  115. myfree(data);
  116. }
  117. To replace an existing node in a tree with a new one with the same key, call::
  118. void rb_replace_node(struct rb_node *old, struct rb_node *new,
  119. struct rb_root *tree);
  120. Replacing a node this way does not re-sort the tree: If the new node doesn't
  121. have the same key as the old node, the rbtree will probably become corrupted.
  122. Iterating through the elements stored in an rbtree (in sort order)
  123. ------------------------------------------------------------------
  124. Four functions are provided for iterating through an rbtree's contents in
  125. sorted order. These work on arbitrary trees, and should not need to be
  126. modified or wrapped (except for locking purposes)::
  127. struct rb_node *rb_first(struct rb_root *tree);
  128. struct rb_node *rb_last(struct rb_root *tree);
  129. struct rb_node *rb_next(struct rb_node *node);
  130. struct rb_node *rb_prev(struct rb_node *node);
  131. To start iterating, call rb_first() or rb_last() with a pointer to the root
  132. of the tree, which will return a pointer to the node structure contained in
  133. the first or last element in the tree. To continue, fetch the next or previous
  134. node by calling rb_next() or rb_prev() on the current node. This will return
  135. NULL when there are no more nodes left.
  136. The iterator functions return a pointer to the embedded struct rb_node, from
  137. which the containing data structure may be accessed with the container_of()
  138. macro, and individual members may be accessed directly via
  139. rb_entry(node, type, member).
  140. Example::
  141. struct rb_node *node;
  142. for (node = rb_first(&mytree); node; node = rb_next(node))
  143. printk("key=%s\n", rb_entry(node, struct mytype, node)->keystring);
  144. Cached rbtrees
  145. --------------
  146. Computing the leftmost (smallest) node is quite a common task for binary
  147. search trees, such as for traversals or users relying on a the particular
  148. order for their own logic. To this end, users can use 'struct rb_root_cached'
  149. to optimize O(logN) rb_first() calls to a simple pointer fetch avoiding
  150. potentially expensive tree iterations. This is done at negligible runtime
  151. overhead for maintanence; albeit larger memory footprint.
  152. Similar to the rb_root structure, cached rbtrees are initialized to be
  153. empty via::
  154. struct rb_root_cached mytree = RB_ROOT_CACHED;
  155. Cached rbtree is simply a regular rb_root with an extra pointer to cache the
  156. leftmost node. This allows rb_root_cached to exist wherever rb_root does,
  157. which permits augmented trees to be supported as well as only a few extra
  158. interfaces::
  159. struct rb_node *rb_first_cached(struct rb_root_cached *tree);
  160. void rb_insert_color_cached(struct rb_node *, struct rb_root_cached *, bool);
  161. void rb_erase_cached(struct rb_node *node, struct rb_root_cached *);
  162. Both insert and erase calls have their respective counterpart of augmented
  163. trees::
  164. void rb_insert_augmented_cached(struct rb_node *node, struct rb_root_cached *,
  165. bool, struct rb_augment_callbacks *);
  166. void rb_erase_augmented_cached(struct rb_node *, struct rb_root_cached *,
  167. struct rb_augment_callbacks *);
  168. Support for Augmented rbtrees
  169. -----------------------------
  170. Augmented rbtree is an rbtree with "some" additional data stored in
  171. each node, where the additional data for node N must be a function of
  172. the contents of all nodes in the subtree rooted at N. This data can
  173. be used to augment some new functionality to rbtree. Augmented rbtree
  174. is an optional feature built on top of basic rbtree infrastructure.
  175. An rbtree user who wants this feature will have to call the augmentation
  176. functions with the user provided augmentation callback when inserting
  177. and erasing nodes.
  178. C files implementing augmented rbtree manipulation must include
  179. <linux/rbtree_augmented.h> instead of <linux/rbtree.h>. Note that
  180. linux/rbtree_augmented.h exposes some rbtree implementations details
  181. you are not expected to rely on; please stick to the documented APIs
  182. there and do not include <linux/rbtree_augmented.h> from header files
  183. either so as to minimize chances of your users accidentally relying on
  184. such implementation details.
  185. On insertion, the user must update the augmented information on the path
  186. leading to the inserted node, then call rb_link_node() as usual and
  187. rb_augment_inserted() instead of the usual rb_insert_color() call.
  188. If rb_augment_inserted() rebalances the rbtree, it will callback into
  189. a user provided function to update the augmented information on the
  190. affected subtrees.
  191. When erasing a node, the user must call rb_erase_augmented() instead of
  192. rb_erase(). rb_erase_augmented() calls back into user provided functions
  193. to updated the augmented information on affected subtrees.
  194. In both cases, the callbacks are provided through struct rb_augment_callbacks.
  195. 3 callbacks must be defined:
  196. - A propagation callback, which updates the augmented value for a given
  197. node and its ancestors, up to a given stop point (or NULL to update
  198. all the way to the root).
  199. - A copy callback, which copies the augmented value for a given subtree
  200. to a newly assigned subtree root.
  201. - A tree rotation callback, which copies the augmented value for a given
  202. subtree to a newly assigned subtree root AND recomputes the augmented
  203. information for the former subtree root.
  204. The compiled code for rb_erase_augmented() may inline the propagation and
  205. copy callbacks, which results in a large function, so each augmented rbtree
  206. user should have a single rb_erase_augmented() call site in order to limit
  207. compiled code size.
  208. Sample usage
  209. ^^^^^^^^^^^^
  210. Interval tree is an example of augmented rb tree. Reference -
  211. "Introduction to Algorithms" by Cormen, Leiserson, Rivest and Stein.
  212. More details about interval trees:
  213. Classical rbtree has a single key and it cannot be directly used to store
  214. interval ranges like [lo:hi] and do a quick lookup for any overlap with a new
  215. lo:hi or to find whether there is an exact match for a new lo:hi.
  216. However, rbtree can be augmented to store such interval ranges in a structured
  217. way making it possible to do efficient lookup and exact match.
  218. This "extra information" stored in each node is the maximum hi
  219. (max_hi) value among all the nodes that are its descendants. This
  220. information can be maintained at each node just be looking at the node
  221. and its immediate children. And this will be used in O(log n) lookup
  222. for lowest match (lowest start address among all possible matches)
  223. with something like::
  224. struct interval_tree_node *
  225. interval_tree_first_match(struct rb_root *root,
  226. unsigned long start, unsigned long last)
  227. {
  228. struct interval_tree_node *node;
  229. if (!root->rb_node)
  230. return NULL;
  231. node = rb_entry(root->rb_node, struct interval_tree_node, rb);
  232. while (true) {
  233. if (node->rb.rb_left) {
  234. struct interval_tree_node *left =
  235. rb_entry(node->rb.rb_left,
  236. struct interval_tree_node, rb);
  237. if (left->__subtree_last >= start) {
  238. /*
  239. * Some nodes in left subtree satisfy Cond2.
  240. * Iterate to find the leftmost such node N.
  241. * If it also satisfies Cond1, that's the match
  242. * we are looking for. Otherwise, there is no
  243. * matching interval as nodes to the right of N
  244. * can't satisfy Cond1 either.
  245. */
  246. node = left;
  247. continue;
  248. }
  249. }
  250. if (node->start <= last) { /* Cond1 */
  251. if (node->last >= start) /* Cond2 */
  252. return node; /* node is leftmost match */
  253. if (node->rb.rb_right) {
  254. node = rb_entry(node->rb.rb_right,
  255. struct interval_tree_node, rb);
  256. if (node->__subtree_last >= start)
  257. continue;
  258. }
  259. }
  260. return NULL; /* No match */
  261. }
  262. }
  263. Insertion/removal are defined using the following augmented callbacks::
  264. static inline unsigned long
  265. compute_subtree_last(struct interval_tree_node *node)
  266. {
  267. unsigned long max = node->last, subtree_last;
  268. if (node->rb.rb_left) {
  269. subtree_last = rb_entry(node->rb.rb_left,
  270. struct interval_tree_node, rb)->__subtree_last;
  271. if (max < subtree_last)
  272. max = subtree_last;
  273. }
  274. if (node->rb.rb_right) {
  275. subtree_last = rb_entry(node->rb.rb_right,
  276. struct interval_tree_node, rb)->__subtree_last;
  277. if (max < subtree_last)
  278. max = subtree_last;
  279. }
  280. return max;
  281. }
  282. static void augment_propagate(struct rb_node *rb, struct rb_node *stop)
  283. {
  284. while (rb != stop) {
  285. struct interval_tree_node *node =
  286. rb_entry(rb, struct interval_tree_node, rb);
  287. unsigned long subtree_last = compute_subtree_last(node);
  288. if (node->__subtree_last == subtree_last)
  289. break;
  290. node->__subtree_last = subtree_last;
  291. rb = rb_parent(&node->rb);
  292. }
  293. }
  294. static void augment_copy(struct rb_node *rb_old, struct rb_node *rb_new)
  295. {
  296. struct interval_tree_node *old =
  297. rb_entry(rb_old, struct interval_tree_node, rb);
  298. struct interval_tree_node *new =
  299. rb_entry(rb_new, struct interval_tree_node, rb);
  300. new->__subtree_last = old->__subtree_last;
  301. }
  302. static void augment_rotate(struct rb_node *rb_old, struct rb_node *rb_new)
  303. {
  304. struct interval_tree_node *old =
  305. rb_entry(rb_old, struct interval_tree_node, rb);
  306. struct interval_tree_node *new =
  307. rb_entry(rb_new, struct interval_tree_node, rb);
  308. new->__subtree_last = old->__subtree_last;
  309. old->__subtree_last = compute_subtree_last(old);
  310. }
  311. static const struct rb_augment_callbacks augment_callbacks = {
  312. augment_propagate, augment_copy, augment_rotate
  313. };
  314. void interval_tree_insert(struct interval_tree_node *node,
  315. struct rb_root *root)
  316. {
  317. struct rb_node **link = &root->rb_node, *rb_parent = NULL;
  318. unsigned long start = node->start, last = node->last;
  319. struct interval_tree_node *parent;
  320. while (*link) {
  321. rb_parent = *link;
  322. parent = rb_entry(rb_parent, struct interval_tree_node, rb);
  323. if (parent->__subtree_last < last)
  324. parent->__subtree_last = last;
  325. if (start < parent->start)
  326. link = &parent->rb.rb_left;
  327. else
  328. link = &parent->rb.rb_right;
  329. }
  330. node->__subtree_last = last;
  331. rb_link_node(&node->rb, rb_parent, link);
  332. rb_insert_augmented(&node->rb, root, &augment_callbacks);
  333. }
  334. void interval_tree_remove(struct interval_tree_node *node,
  335. struct rb_root *root)
  336. {
  337. rb_erase_augmented(&node->rb, root, &augment_callbacks);
  338. }