Lines Matching +full:root +full:- +full:node

1 // SPDX-License-Identifier: GPL-2.0+
18 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
20 * 1) A node is either red or black
21 * 2) The root is black
23 * 4) Both children of every red node are black
24 * 5) Every simple path from root to leaves contains the same number
28 * consecutive red nodes in a path and every red node is therefore followed by
39 rb->__rb_parent_color |= RB_BLACK; in rb_set_black()
44 return (struct rb_node *)red->__rb_parent_color; in rb_red_parent()
49 * - old's parent and color get assigned to new
50 * - old gets assigned new as a parent and 'color' as a color.
54 struct rb_root *root, int color) in __rb_rotate_set_parents() argument
57 new->__rb_parent_color = old->__rb_parent_color; in __rb_rotate_set_parents()
59 __rb_change_child(old, new, parent, root); in __rb_rotate_set_parents()
63 __rb_insert(struct rb_node *node, struct rb_root *root, in __rb_insert() argument
66 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; in __rb_insert()
70 * Loop invariant: node is red in __rb_insert()
74 * want a red root or two consecutive red nodes. in __rb_insert()
77 rb_set_parent_color(node, NULL, RB_BLACK); in __rb_insert()
84 tmp = gparent->rb_right; in __rb_insert()
85 if (parent != tmp) { /* parent == gparent->rb_left */ in __rb_insert()
88 * Case 1 - color flips in __rb_insert()
92 * p u --> P U in __rb_insert()
102 node = gparent; in __rb_insert()
103 parent = rb_parent(node); in __rb_insert()
104 rb_set_parent_color(node, parent, RB_RED); in __rb_insert()
108 tmp = parent->rb_right; in __rb_insert()
109 if (node == tmp) { in __rb_insert()
111 * Case 2 - left rotate at parent in __rb_insert()
115 * p U --> n U in __rb_insert()
122 parent->rb_right = tmp = node->rb_left; in __rb_insert()
123 node->rb_left = parent; in __rb_insert()
127 rb_set_parent_color(parent, node, RB_RED); in __rb_insert()
128 augment_rotate(parent, node); in __rb_insert()
129 parent = node; in __rb_insert()
130 tmp = node->rb_right; in __rb_insert()
134 * Case 3 - right rotate at gparent in __rb_insert()
138 * p U --> n g in __rb_insert()
142 gparent->rb_left = tmp; /* == parent->rb_right */ in __rb_insert()
143 parent->rb_right = gparent; in __rb_insert()
146 __rb_rotate_set_parents(gparent, parent, root, RB_RED); in __rb_insert()
150 tmp = gparent->rb_left; in __rb_insert()
152 /* Case 1 - color flips */ in __rb_insert()
155 node = gparent; in __rb_insert()
156 parent = rb_parent(node); in __rb_insert()
157 rb_set_parent_color(node, parent, RB_RED); in __rb_insert()
161 tmp = parent->rb_left; in __rb_insert()
162 if (node == tmp) { in __rb_insert()
163 /* Case 2 - right rotate at parent */ in __rb_insert()
164 parent->rb_left = tmp = node->rb_right; in __rb_insert()
165 node->rb_right = parent; in __rb_insert()
169 rb_set_parent_color(parent, node, RB_RED); in __rb_insert()
170 augment_rotate(parent, node); in __rb_insert()
171 parent = node; in __rb_insert()
172 tmp = node->rb_left; in __rb_insert()
175 /* Case 3 - left rotate at gparent */ in __rb_insert()
176 gparent->rb_right = tmp; /* == parent->rb_left */ in __rb_insert()
177 parent->rb_left = gparent; in __rb_insert()
180 __rb_rotate_set_parents(gparent, parent, root, RB_RED); in __rb_insert()
188 * Inline version for rb_erase() use - we want to be able to inline
192 ____rb_erase_color(struct rb_node *parent, struct rb_root *root, in ____rb_erase_color() argument
195 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2; in ____rb_erase_color() local
200 * - node is black (or NULL on first iteration) in ____rb_erase_color()
201 * - node is not the root (parent is not NULL) in ____rb_erase_color()
202 * - All leaf paths going through parent and node have a in ____rb_erase_color()
203 * black node count that is 1 lower than other leaf paths. in ____rb_erase_color()
205 sibling = parent->rb_right; in ____rb_erase_color()
206 if (node != sibling) { /* node == parent->rb_left */ in ____rb_erase_color()
209 * Case 1 - left rotate at parent in ____rb_erase_color()
213 * N s --> p Sr in ____rb_erase_color()
217 parent->rb_right = tmp1 = sibling->rb_left; in ____rb_erase_color()
218 sibling->rb_left = parent; in ____rb_erase_color()
220 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
225 tmp1 = sibling->rb_right; in ____rb_erase_color()
227 tmp2 = sibling->rb_left; in ____rb_erase_color()
230 * Case 2 - sibling color flip in ____rb_erase_color()
235 * N S --> N s in ____rb_erase_color()
249 node = parent; in ____rb_erase_color()
250 parent = rb_parent(node); in ____rb_erase_color()
257 * Case 3 - right rotate at sibling in ____rb_erase_color()
262 * N S --> N Sl in ____rb_erase_color()
268 sibling->rb_left = tmp1 = tmp2->rb_right; in ____rb_erase_color()
269 tmp2->rb_right = sibling; in ____rb_erase_color()
270 parent->rb_right = tmp2; in ____rb_erase_color()
279 * Case 4 - left rotate at parent + color flips in ____rb_erase_color()
286 * N S --> P Sr in ____rb_erase_color()
290 parent->rb_right = tmp2 = sibling->rb_left; in ____rb_erase_color()
291 sibling->rb_left = parent; in ____rb_erase_color()
295 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
300 sibling = parent->rb_left; in ____rb_erase_color()
302 /* Case 1 - right rotate at parent */ in ____rb_erase_color()
303 parent->rb_left = tmp1 = sibling->rb_right; in ____rb_erase_color()
304 sibling->rb_right = parent; in ____rb_erase_color()
306 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
311 tmp1 = sibling->rb_left; in ____rb_erase_color()
313 tmp2 = sibling->rb_right; in ____rb_erase_color()
315 /* Case 2 - sibling color flip */ in ____rb_erase_color()
321 node = parent; in ____rb_erase_color()
322 parent = rb_parent(node); in ____rb_erase_color()
328 /* Case 3 - right rotate at sibling */ in ____rb_erase_color()
329 sibling->rb_right = tmp1 = tmp2->rb_left; in ____rb_erase_color()
330 tmp2->rb_left = sibling; in ____rb_erase_color()
331 parent->rb_left = tmp2; in ____rb_erase_color()
339 /* Case 4 - left rotate at parent + color flips */ in ____rb_erase_color()
340 parent->rb_left = tmp2 = sibling->rb_right; in ____rb_erase_color()
341 sibling->rb_right = parent; in ____rb_erase_color()
345 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
353 /* Non-inline version for rb_erase_augmented() use */
354 void __rb_erase_color(struct rb_node *parent, struct rb_root *root, in __rb_erase_color() argument
357 ____rb_erase_color(parent, root, augment_rotate); in __rb_erase_color()
362 * Non-augmented rbtree manipulation functions.
368 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} in dummy_propagate() argument
376 void rb_insert_color(struct rb_node *node, struct rb_root *root) in rb_insert_color() argument
378 __rb_insert(node, root, dummy_rotate); in rb_insert_color()
382 void rb_erase(struct rb_node *node, struct rb_root *root) in rb_erase() argument
385 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks); in rb_erase()
387 ____rb_erase_color(rebalance, root, dummy_rotate); in rb_erase()
394 * This instantiates the same __always_inline functions as in the non-augmented
395 * case, but this time with user-defined callbacks.
398 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, in __rb_insert_augmented() argument
401 __rb_insert(node, root, augment_rotate); in __rb_insert_augmented()
406 * This function returns the first node (in sort order) of the tree.
408 struct rb_node *rb_first(const struct rb_root *root) in rb_first() argument
412 n = root->rb_node; in rb_first()
415 while (n->rb_left) in rb_first()
416 n = n->rb_left; in rb_first()
421 struct rb_node *rb_last(const struct rb_root *root) in rb_last() argument
425 n = root->rb_node; in rb_last()
428 while (n->rb_right) in rb_last()
429 n = n->rb_right; in rb_last()
434 struct rb_node *rb_next(const struct rb_node *node) in rb_next() argument
438 if (RB_EMPTY_NODE(node)) in rb_next()
442 * If we have a right-hand child, go down and then left as far in rb_next()
445 if (node->rb_right) { in rb_next()
446 node = node->rb_right; in rb_next()
447 while (node->rb_left) in rb_next()
448 node=node->rb_left; in rb_next()
449 return (struct rb_node *)node; in rb_next()
453 * No right-hand children. Everything down and left is smaller than us, in rb_next()
454 * so any 'next' node must be in the general direction of our parent. in rb_next()
455 * Go up the tree; any time the ancestor is a right-hand child of its in rb_next()
456 * parent, keep going up. First time it's a left-hand child of its in rb_next()
457 * parent, said parent is our 'next' node. in rb_next()
459 while ((parent = rb_parent(node)) && node == parent->rb_right) in rb_next()
460 node = parent; in rb_next()
466 struct rb_node *rb_prev(const struct rb_node *node) in rb_prev() argument
470 if (RB_EMPTY_NODE(node)) in rb_prev()
474 * If we have a left-hand child, go down and then right as far in rb_prev()
477 if (node->rb_left) { in rb_prev()
478 node = node->rb_left; in rb_prev()
479 while (node->rb_right) in rb_prev()
480 node=node->rb_right; in rb_prev()
481 return (struct rb_node *)node; in rb_prev()
485 * No left-hand children. Go up till we find an ancestor which in rb_prev()
486 * is a right-hand child of its parent. in rb_prev()
488 while ((parent = rb_parent(node)) && node == parent->rb_left) in rb_prev()
489 node = parent; in rb_prev()
496 struct rb_root *root) in rb_replace_node() argument
501 __rb_change_child(victim, new, parent, root); in rb_replace_node()
502 if (victim->rb_left) in rb_replace_node()
503 rb_set_parent(victim->rb_left, new); in rb_replace_node()
504 if (victim->rb_right) in rb_replace_node()
505 rb_set_parent(victim->rb_right, new); in rb_replace_node()
512 static struct rb_node *rb_left_deepest_node(const struct rb_node *node) in rb_left_deepest_node() argument
515 if (node->rb_left) in rb_left_deepest_node()
516 node = node->rb_left; in rb_left_deepest_node()
517 else if (node->rb_right) in rb_left_deepest_node()
518 node = node->rb_right; in rb_left_deepest_node()
520 return (struct rb_node *)node; in rb_left_deepest_node()
524 struct rb_node *rb_next_postorder(const struct rb_node *node) in rb_next_postorder() argument
527 if (!node) in rb_next_postorder()
529 parent = rb_parent(node); in rb_next_postorder()
531 /* If we're sitting on node, we've already seen our children */ in rb_next_postorder()
532 if (parent && node == parent->rb_left && parent->rb_right) { in rb_next_postorder()
533 /* If we are the parent's left node, go to the parent's right in rb_next_postorder()
534 * node then all the way down to the left */ in rb_next_postorder()
535 return rb_left_deepest_node(parent->rb_right); in rb_next_postorder()
537 /* Otherwise we are the parent's right node, and the parent in rb_next_postorder()
543 struct rb_node *rb_first_postorder(const struct rb_root *root) in rb_first_postorder() argument
545 if (!root->rb_node) in rb_first_postorder()
548 return rb_left_deepest_node(root->rb_node); in rb_first_postorder()