Skip to content

fix delete - #12

Open
cscott wants to merge 2 commits into
dan-on:masterfrom
cscott:fix-delete
Open

cscott wants to merge 2 commits into
dan-on:masterfrom
cscott:fix-delete

Conversation

@cscott

@cscott cscott commented Oct 15, 2026

Copy link
Copy Markdown
  • fix: recompute the new subtree root max in rotateRight()
  • fix: make deleteFixup() match CLRS RB-DELETE-FIXUP

After a right rotation the node that moved up ($x) must have its max
recomputed, but the code recomputed $y a second time instead.  The
stale max on the new subtree root makes findIntersections() prune
subtrees that do contain matches.  Insert-only workloads mostly mask
this because insert() re-walks the path to the root afterwards; the
delete fix-up does not.
Two deviations from the textbook algorithm corrupted the tree on removal:

* Case 2 must only apply when *both* of the sibling's children are
  black; the code tested only the near child.
* After the case-3 rotation the sibling must be re-read from the parent
  (w = x.p.right / x.p.left); the code kept using the old sibling.

Either one breaks the black-height or red-red invariant, after which
later removals dereference the nil sentinel's (null) children and throw
a TypeError, and queries return wrong results.

Add a randomized test that checks findIntersections() and getSize()
against a brute-force list across a mix of inserts and removals.  On
the previous code 45 of the 50 seeds fail.
@dan-on

dan-on commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Thanks for the fixes! I went through both changes against CLRS and they look correct: the rotateRight() max recompute and the two deleteFixup() fixes (both nephews checked in case 2, sibling re-fetched after the case 3 rotation).

CI was failing for a reason unrelated to your changes: the workflow used the retired actions/cache@v2, so every job failed at "Set up job". That's now fixed on master.

Could you rebase this branch onto the current master and force-push? That will re-run CI with the updated workflow. (Re-running the old jobs won't help, because they use the previous merge commit with the old workflow.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants