Red-Black Tree Delete Fixup in CLRS Second Edition

2019-07-18 06:13发布

I am implementing red-black tree deletion for interval trees following CLRS 2nd edition, fourth printing, pg 288-9.

Summary of bug:

RB-Delete-Fixup

If x and w are the sentinel nodes, which is a possible consequence of RB-Delete, then the evaluation of color(left(w)) resp. color(right(w)) in RB-Delete-Fixup suffers a null pointer exception on the first iteration of the while loop.

(if (and (= (get-color (get-left @w)) black) 
         (= (get-color (get-right @w)) black)) ;; Bug here!

All of the code for this question is here in Clojure:

https://github.com/mobiusinversion/interval-trees

and here is a diagram of the red-black tree state when the exception is thrown:

http://gorillamatrix.com/files/rb-delete-fixup.jpg

The failing unit test is

(deftest insert-seven-delete-three-test ... )

in the file

test/interval_trees/interval_tree_test.clj 

Here is what the output of lein test looks like:

$ lein test

lein test interval-trees.interval-test

lein test interval-trees.interval-tree-test

lein test :only interval-trees.interval-tree-test/insert-seven-delete-three-test

ERROR in (insert-seven-delete-three-test) (core.clj:2108)
Uncaught exception, not in assertion.
expected: nil
  actual: java.lang.NullPointerException: null
 at clojure.core$deref_future.invoke (core.clj:2108)
    clojure.core$deref.invoke (core.clj:2129)
    interval_trees.interval_tree$get_color.invoke (interval_tree.clj:61)
    interval_trees.interval_tree$delete_fixup.invoke (interval_tree.clj:451)
    interval_trees.interval_tree$delete$fn__323.invoke (interval_tree.clj:528)

The problem seems to be after the assignment

w <- right(p(x))

of CLRS, pg. 289 rb-delete-fixup line 7 of the pseudocode, w points to the sentinel node, and therefore has no left and right to check for color on line 9 of the pseudocode.

The line in the Clojure implementation throwing the exception is here

src/interval_trees/interval_tree.clj:451 (where you see Bug here! comment)

There does not appear to be a bug filed in the errata:

http://www.cs.dartmouth.edu/~thc/clrs-bugs/bugs-2e.php

I apologize that this question is very specific and dense, but help is greatly appreciated, I've been bangin my head on this for days.

It appears that this person has asked the same question but not received an answer Red Black Tree deletion algorithm

Update: I implemented the delete and delete fixups routines in the third edition third printing, but was ot able to solve the problem.

1条回答
霸刀☆藐视天下
2楼-- · 2019-07-18 06:32

This turned out to be my mistake, I thought a nodes "color" was part of its satellite data. It's is not.

查看更多
登录 后发表回答