Skip to content

Commit

Permalink
rbtree: add some additional comments for rebalancing cases
Browse files Browse the repository at this point in the history
While overall the code is very nicely commented, it might not be
immediately obvious from the diagrams what is going on.  Add a very
brief summary of each case.  Opposite cases where the node is the left
child are left untouched.

Link: http://lkml.kernel.org/r/20170719014603.19029-4-dave@stgolabs.net
Signed-off-by: Davidlohr Bueso <dbueso@suse.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit 35dc67d7d922b2c9a1adb006c7a0f370eeb5c114)
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
Signed-off-by: khusika <khusikadhamar@gmail.com>
Signed-off-by: kdrag0n <dragon@khronodragon.com>
Signed-off-by: Oktapra Amtono <oktapra.amtono@gmail.com>
Signed-off-by: Kneba <abenkenary3@gmail.com>
Signed-off-by: dotkit <ewprjkt@proton.me>
  • Loading branch information
Davidlohr Bueso authored and Tiktodz committed Aug 11, 2023
1 parent c6c5aeb commit f1dc446
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
if (parent != tmp) { /* parent == gparent->rb_left */
if (tmp && rb_is_red(tmp)) {
/*
* Case 1 - color flips
* Case 1 - node's uncle is red (color flips).
*
* G g
* / \ / \
Expand All @@ -155,7 +155,8 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
tmp = parent->rb_right;
if (node == tmp) {
/*
* Case 2 - left rotate at parent
* Case 2 - node's uncle is black and node is
* the parent's right child (left rotate at parent).
*
* G G
* / \ / \
Expand All @@ -179,7 +180,8 @@ __rb_insert(struct rb_node *node, struct rb_root *root,
}

/*
* Case 3 - right rotate at gparent
* Case 3 - node's uncle is black and node is
* the parent's left child (right rotate at gparent).
*
* G P
* / \ / \
Expand Down

0 comments on commit f1dc446

Please sign in to comment.