Repository navigation
Fix red-black tree insertion invariants - #2248
Open
Shubham-Padkonde wants to merge 1 commit into
Open
Shubham-Padkonde wants to merge 1 commit into
Shubham-Padkonde wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Inserting
[1, 3, 2, 4, 5]currently loses two keys; other sequences in #2229 throw or produce unequal black heights. Preserve parent ownership when a rotation replaces an already-reparented child, and stop balancing a black subtree root after a rotation. The latter cannot introduce a red/red violation and rotating it again can invalidate the tree.Fixes #2229. Thanks to @devladpopov for the reproducible invariant failures.
Related to the existing proposal #561: that patch repairs parent pointers locally in the two double rotations. This change addresses the underlying child-setter ownership behavior, including single rotations below another ancestor, and the separate black-node balancing defect. Fixing parent links alone still fails two of the new invariant cases. I am linking the overlap explicitly so maintainers can choose how to integrate the proposals.
The tests verify sorted membership, parent pointers, root color, adjacent red nodes, and equal black heights after every insertion. They cover the reported sequences and their mirrors, plus all 720 permutations of six keys. Child-setter tests also confirm that ordinary removal still clears the parent.
Validation:
npm run lintandgit diff --checkpass.AI assistance: implementation and tests were prepared with Codex and validated using the checks above.