views:

48

answers:

1

Hi all,

I've been implementing my own version of a red-black tree, mostly basing my algorithms from Wikipedia (http://en.wikipedia.org/wiki/Red-black_tree). Its fairly concise for the most part, but there's one part that I would like clarification on.

When erasing a node from the tree that has 2 non-leaf (non-NULL) children, it says to move either side's children into the deletable node, and remove that child.

I'm a little confused as to which side to remove from, based on that. Do I pick the side randomly, do I alternate between sides, or do I stick to the same side for every future deletion?

+4  A: 

If you have no prior knowledge about your input data, you cannot know which side is more benefitial of being the new intermediate node or the new child.

You can therefore just apply the rule that fits you most (is easiest to write/compute -- probably "always take the left one"). Employing a random scheme typically just introduces more unneeded computation.

ypnos
+1 - avoid deliberately introducing random behaviour; at some point, there will be a bug somewhere, and random behaviour will make the effects of the bug more baffling, harder to reproduce consistently, etc.
Daniel Earwicker
Agreed. The one situation where it's worth introducing random behaviour is to make pathological worst-case behaviour occur with low probability and unpredictably, rather than occurring predictably on certain inputs. Hence random pivot choice in qsort prior to the invention of introsort. I don't think that the worst case behaviour of a red-black tree is bad enough to justify it here. There is a well-known randomized "probably balanced" tree structure, I just can't remember the name of it for the moment.
Steve Jessop
Thanks for your speedy response!
SalamiArmi