Skip to content

Commit

Permalink
[RBTREE] Remove dead code in rb_erase()
Browse files Browse the repository at this point in the history
Observe rb_erase(), when the victim node 'old' has two children so
neither of the simple cases at the beginning are taken.

Observe that it effectively does an 'rb_next()' operation to find the
next (by value) node in the tree. That is; we go to the victim's
right-hand child and then follow left-hand pointers all the way
down the tree as far as we can until we find the next node 'node'. We
end up with 'node' being either the same immediate right-hand child of
'old', or one of its descendants on the far left-hand side.

For a start, we _know_ that 'node' has a parent. We can drop that check.

We also know that if 'node's parent is 'old', then 'node' is the
right-hand child of its parent. And that if 'node's parent is _not_
'old', then 'node' is the left-hand child of its parent.

So instead of checking for 'node->rb_parent == old' in one place and
also checking 'node's heritage separately when we're trying to change
its link from its parent, we can shuffle things around a bit and do
it like this...

Signed-off-by: David Woodhouse <dwmw2@infradead.org>
  • Loading branch information
dwmw2 committed Apr 21, 2006
1 parent 21f1d5f commit 1975e59
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions lib/rbtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,18 +243,13 @@ void rb_erase(struct rb_node *node, struct rb_root *root)

if (child)
child->rb_parent = parent;
if (parent)
{
if (parent->rb_left == node)
parent->rb_left = child;
else
parent->rb_right = child;
}
else
root->rb_node = child;

if (node->rb_parent == old)
if (node->rb_parent == old) {
parent->rb_right = child;
parent = node;
} else
parent->rb_left = child;

node->rb_parent = old->rb_parent;
node->rb_color = old->rb_color;
node->rb_right = old->rb_right;
Expand Down

0 comments on commit 1975e59

Please sign in to comment.