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/[email protected]
Signed-off-by: Davidlohr Bueso <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Davidlohr Bueso authored and torvalds committed Sep 9, 2017
1 parent 2aadf7f commit 35dc67d
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 35dc67d

Please sign in to comment.