Repository navigation
fix(data_structures): repair RedBlackTree deletion rebalancing and black_height - #15818
Open
gastondana627 wants to merge 1 commit into
Open
gastondana627 wants to merge 1 commit into
gastondana627 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Describe your change
Fixes three correctness bugs in
data_structures/binary_tree/red_black_tree.pythat broke Red-Black Tree deletion invariants and masked those violations inblack_height()/check_color_properties():_remove_repair()skipped repair wheneverself.grandparentorself.parent.siblingwasNone:Deleting a black child of the root (e.g.
RedBlackTree(10).insert(5).insert(15).insert(20).remove(5)) exited_remove_repair()immediately without rebalancing, leaving unequal black heights (10 blkwith leftNoneand right15 blk -> 20 red). Recoloringsibling,self.parent, and the outer red nephew before rotating in Case 6 removes any need to accessself.grandparent/self.parent.siblingahead of the rotation.black_height()returned1whenever either child wasNone:Nonechild and a black subtree on the other side (e.g.10 blk -> right: 15 blk) returned1instead ofNone, causingcheck_color_properties()to report invalid trees as valid.RedBlackTree(20).insert(-19).insert(-3).insert(-6).insert(-12)) returnedNone(Property 5) because a black leaf returned1while its sibling returned2.Now
Nonechildren contribute0and each node adds1 - self.color, verifying both subtrees uniformly.insert()andremove()returnedself.parent or self, which can return a non-root node when multi-step rebalancing rotates the original root down more than once; added_find_root()so top-level calls always return the true root (parent is None).parent.left == selftoparent.left is selfinrotate_left()(matchingrotate_right()forVerified with
python3 -m doctest -v data_structures/binary_tree/red_black_tree.py(11/11 passed),uvx ruff check(All checks passed!),uvx ruff format --check(1 file already formatted), and a randomized property-based test over 50,000+ consecutive insert/remove/drain-to-empty operations with zero invariant violations.Checklist