There is a version in the introduction to the algorithms of Thomas X Cormen, Charles E Lazerson, Ronald Lavert, Clifford Stein.
Pseudo-code is available on Google books (p. 270).
As pointed out in the comments, the approach to copying data to node z instead of replacing z with y on line 14/15 is not optimal, especially if you have pointers to nodes somewhere else. So line 13-16 can be:
do_fixup = y.color == BLACK if y is not z: replace_parent(tree, y, z) y.left = z.left y.left.parent = y y.right = z.right y.right.parent = y y.color = z.color if do_fixup: ...
Where replace_parent defined as (this can also be used for lines 7-12):
def replace_parent(tree, a, b): a.parent = b.parent if not b.parent: tree.root = a else: if b is b.parent.left: b.parent.left = a else: b.parent.right = a
schlamar Jul 04 '12 at 11:33 2012-07-04 11:33
source share