Lines Matching +full:root +full:- +full:node
7 * SPDX-License-Identifier: GPL-2.0+
19 * red-black trees properties: http://en.wikipedia.org/wiki/Rbtree
21 * 1) A node is either red or black
22 * 2) The root is black
24 * 4) Both children of every red node are black
25 * 5) Every simple path from root to leaves contains the same number
29 * consecutive red nodes in a path and every red node is therefore followed by
40 rb->__rb_parent_color |= RB_BLACK; in rb_set_black()
45 return (struct rb_node *)red->__rb_parent_color; in rb_red_parent()
50 * - old's parent and color get assigned to new
51 * - old gets assigned new as a parent and 'color' as a color.
55 struct rb_root *root, int color) in __rb_rotate_set_parents() argument
58 new->__rb_parent_color = old->__rb_parent_color; in __rb_rotate_set_parents()
60 __rb_change_child(old, new, parent, root); in __rb_rotate_set_parents()
64 __rb_insert(struct rb_node *node, struct rb_root *root, in __rb_insert() argument
67 struct rb_node *parent = rb_red_parent(node), *gparent, *tmp; in __rb_insert()
71 * Loop invariant: node is red in __rb_insert()
75 * want a red root or two consecutive red nodes. in __rb_insert()
78 rb_set_parent_color(node, NULL, RB_BLACK); in __rb_insert()
85 tmp = gparent->rb_right; in __rb_insert()
86 if (parent != tmp) { /* parent == gparent->rb_left */ in __rb_insert()
89 * Case 1 - color flips in __rb_insert()
93 * p u --> P U in __rb_insert()
103 node = gparent; in __rb_insert()
104 parent = rb_parent(node); in __rb_insert()
105 rb_set_parent_color(node, parent, RB_RED); in __rb_insert()
109 tmp = parent->rb_right; in __rb_insert()
110 if (node == tmp) { in __rb_insert()
112 * Case 2 - left rotate at parent in __rb_insert()
116 * p U --> n U in __rb_insert()
123 parent->rb_right = tmp = node->rb_left; in __rb_insert()
124 node->rb_left = parent; in __rb_insert()
128 rb_set_parent_color(parent, node, RB_RED); in __rb_insert()
129 augment_rotate(parent, node); in __rb_insert()
130 parent = node; in __rb_insert()
131 tmp = node->rb_right; in __rb_insert()
135 * Case 3 - right rotate at gparent in __rb_insert()
139 * p U --> n g in __rb_insert()
143 gparent->rb_left = tmp; /* == parent->rb_right */ in __rb_insert()
144 parent->rb_right = gparent; in __rb_insert()
147 __rb_rotate_set_parents(gparent, parent, root, RB_RED); in __rb_insert()
151 tmp = gparent->rb_left; in __rb_insert()
153 /* Case 1 - color flips */ in __rb_insert()
156 node = gparent; in __rb_insert()
157 parent = rb_parent(node); in __rb_insert()
158 rb_set_parent_color(node, parent, RB_RED); in __rb_insert()
162 tmp = parent->rb_left; in __rb_insert()
163 if (node == tmp) { in __rb_insert()
164 /* Case 2 - right rotate at parent */ in __rb_insert()
165 parent->rb_left = tmp = node->rb_right; in __rb_insert()
166 node->rb_right = parent; in __rb_insert()
170 rb_set_parent_color(parent, node, RB_RED); in __rb_insert()
171 augment_rotate(parent, node); in __rb_insert()
172 parent = node; in __rb_insert()
173 tmp = node->rb_left; in __rb_insert()
176 /* Case 3 - left rotate at gparent */ in __rb_insert()
177 gparent->rb_right = tmp; /* == parent->rb_left */ in __rb_insert()
178 parent->rb_left = gparent; in __rb_insert()
181 __rb_rotate_set_parents(gparent, parent, root, RB_RED); in __rb_insert()
189 * Inline version for rb_erase() use - we want to be able to inline
193 ____rb_erase_color(struct rb_node *parent, struct rb_root *root, in ____rb_erase_color() argument
196 struct rb_node *node = NULL, *sibling, *tmp1, *tmp2; in ____rb_erase_color() local
201 * - node is black (or NULL on first iteration) in ____rb_erase_color()
202 * - node is not the root (parent is not NULL) in ____rb_erase_color()
203 * - All leaf paths going through parent and node have a in ____rb_erase_color()
204 * black node count that is 1 lower than other leaf paths. in ____rb_erase_color()
206 sibling = parent->rb_right; in ____rb_erase_color()
207 if (node != sibling) { /* node == parent->rb_left */ in ____rb_erase_color()
210 * Case 1 - left rotate at parent in ____rb_erase_color()
214 * N s --> p Sr in ____rb_erase_color()
218 parent->rb_right = tmp1 = sibling->rb_left; in ____rb_erase_color()
219 sibling->rb_left = parent; in ____rb_erase_color()
221 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
226 tmp1 = sibling->rb_right; in ____rb_erase_color()
228 tmp2 = sibling->rb_left; in ____rb_erase_color()
231 * Case 2 - sibling color flip in ____rb_erase_color()
236 * N S --> N s in ____rb_erase_color()
250 node = parent; in ____rb_erase_color()
251 parent = rb_parent(node); in ____rb_erase_color()
258 * Case 3 - right rotate at sibling in ____rb_erase_color()
263 * N S --> N Sl in ____rb_erase_color()
269 sibling->rb_left = tmp1 = tmp2->rb_right; in ____rb_erase_color()
270 tmp2->rb_right = sibling; in ____rb_erase_color()
271 parent->rb_right = tmp2; in ____rb_erase_color()
280 * Case 4 - left rotate at parent + color flips in ____rb_erase_color()
287 * N S --> P Sr in ____rb_erase_color()
291 parent->rb_right = tmp2 = sibling->rb_left; in ____rb_erase_color()
292 sibling->rb_left = parent; in ____rb_erase_color()
296 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
301 sibling = parent->rb_left; in ____rb_erase_color()
303 /* Case 1 - right rotate at parent */ in ____rb_erase_color()
304 parent->rb_left = tmp1 = sibling->rb_right; in ____rb_erase_color()
305 sibling->rb_right = parent; in ____rb_erase_color()
307 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
312 tmp1 = sibling->rb_left; in ____rb_erase_color()
314 tmp2 = sibling->rb_right; in ____rb_erase_color()
316 /* Case 2 - sibling color flip */ in ____rb_erase_color()
322 node = parent; in ____rb_erase_color()
323 parent = rb_parent(node); in ____rb_erase_color()
329 /* Case 3 - right rotate at sibling */ in ____rb_erase_color()
330 sibling->rb_right = tmp1 = tmp2->rb_left; in ____rb_erase_color()
331 tmp2->rb_left = sibling; in ____rb_erase_color()
332 parent->rb_left = tmp2; in ____rb_erase_color()
340 /* Case 4 - left rotate at parent + color flips */ in ____rb_erase_color()
341 parent->rb_left = tmp2 = sibling->rb_right; in ____rb_erase_color()
342 sibling->rb_right = parent; in ____rb_erase_color()
346 __rb_rotate_set_parents(parent, sibling, root, in ____rb_erase_color()
354 /* Non-inline version for rb_erase_augmented() use */
355 void __rb_erase_color(struct rb_node *parent, struct rb_root *root, in __rb_erase_color() argument
358 ____rb_erase_color(parent, root, augment_rotate); in __rb_erase_color()
363 * Non-augmented rbtree manipulation functions.
369 static inline void dummy_propagate(struct rb_node *node, struct rb_node *stop) {} in dummy_propagate() argument
377 void rb_insert_color(struct rb_node *node, struct rb_root *root) in rb_insert_color() argument
379 __rb_insert(node, root, dummy_rotate); in rb_insert_color()
383 void rb_erase(struct rb_node *node, struct rb_root *root) in rb_erase() argument
386 rebalance = __rb_erase_augmented(node, root, &dummy_callbacks); in rb_erase()
388 ____rb_erase_color(rebalance, root, dummy_rotate); in rb_erase()
395 * This instantiates the same __always_inline functions as in the non-augmented
396 * case, but this time with user-defined callbacks.
399 void __rb_insert_augmented(struct rb_node *node, struct rb_root *root, in __rb_insert_augmented() argument
402 __rb_insert(node, root, augment_rotate); in __rb_insert_augmented()
407 * This function returns the first node (in sort order) of the tree.
409 struct rb_node *rb_first(const struct rb_root *root) in rb_first() argument
413 n = root->rb_node; in rb_first()
416 while (n->rb_left) in rb_first()
417 n = n->rb_left; in rb_first()
422 struct rb_node *rb_last(const struct rb_root *root) in rb_last() argument
426 n = root->rb_node; in rb_last()
429 while (n->rb_right) in rb_last()
430 n = n->rb_right; in rb_last()
435 struct rb_node *rb_next(const struct rb_node *node) in rb_next() argument
439 if (RB_EMPTY_NODE(node)) in rb_next()
443 * If we have a right-hand child, go down and then left as far in rb_next()
446 if (node->rb_right) { in rb_next()
447 node = node->rb_right; in rb_next()
448 while (node->rb_left) in rb_next()
449 node=node->rb_left; in rb_next()
450 return (struct rb_node *)node; in rb_next()
454 * No right-hand children. Everything down and left is smaller than us, in rb_next()
455 * so any 'next' node must be in the general direction of our parent. in rb_next()
456 * Go up the tree; any time the ancestor is a right-hand child of its in rb_next()
457 * parent, keep going up. First time it's a left-hand child of its in rb_next()
458 * parent, said parent is our 'next' node. in rb_next()
460 while ((parent = rb_parent(node)) && node == parent->rb_right) in rb_next()
461 node = parent; in rb_next()
467 struct rb_node *rb_prev(const struct rb_node *node) in rb_prev() argument
471 if (RB_EMPTY_NODE(node)) in rb_prev()
475 * If we have a left-hand child, go down and then right as far in rb_prev()
478 if (node->rb_left) { in rb_prev()
479 node = node->rb_left; in rb_prev()
480 while (node->rb_right) in rb_prev()
481 node=node->rb_right; in rb_prev()
482 return (struct rb_node *)node; in rb_prev()
486 * No left-hand children. Go up till we find an ancestor which in rb_prev()
487 * is a right-hand child of its parent. in rb_prev()
489 while ((parent = rb_parent(node)) && node == parent->rb_left) in rb_prev()
490 node = parent; in rb_prev()
497 struct rb_root *root) in rb_replace_node() argument
502 __rb_change_child(victim, new, parent, root); in rb_replace_node()
503 if (victim->rb_left) in rb_replace_node()
504 rb_set_parent(victim->rb_left, new); in rb_replace_node()
505 if (victim->rb_right) in rb_replace_node()
506 rb_set_parent(victim->rb_right, new); in rb_replace_node()
513 static struct rb_node *rb_left_deepest_node(const struct rb_node *node) in rb_left_deepest_node() argument
516 if (node->rb_left) in rb_left_deepest_node()
517 node = node->rb_left; in rb_left_deepest_node()
518 else if (node->rb_right) in rb_left_deepest_node()
519 node = node->rb_right; in rb_left_deepest_node()
521 return (struct rb_node *)node; in rb_left_deepest_node()
525 struct rb_node *rb_next_postorder(const struct rb_node *node) in rb_next_postorder() argument
528 if (!node) in rb_next_postorder()
530 parent = rb_parent(node); in rb_next_postorder()
532 /* If we're sitting on node, we've already seen our children */ in rb_next_postorder()
533 if (parent && node == parent->rb_left && parent->rb_right) { in rb_next_postorder()
534 /* If we are the parent's left node, go to the parent's right in rb_next_postorder()
535 * node then all the way down to the left */ in rb_next_postorder()
536 return rb_left_deepest_node(parent->rb_right); in rb_next_postorder()
538 /* Otherwise we are the parent's right node, and the parent in rb_next_postorder()
544 struct rb_node *rb_first_postorder(const struct rb_root *root) in rb_first_postorder() argument
546 if (!root->rb_node) in rb_first_postorder()
549 return rb_left_deepest_node(root->rb_node); in rb_first_postorder()