Skip to content

fix(data_structures): repair RedBlackTree deletion rebalancing and black_height - #15818

Open
gastondana627 wants to merge 1 commit into
TheAlgorithms:masterfrom
gastondana627:fix/red-black-tree-remove-and-black-height
Open

gastondana627 wants to merge 1 commit into
TheAlgorithms:masterfrom
gastondana627:fix/red-black-tree-remove-and-black-height

Conversation

@gastondana627

Copy link
Copy Markdown

Describe your change

Fixes three correctness bugs in data_structures/binary_tree/red_black_tree.py that broke Red-Black Tree deletion invariants and masked those violations in black_height() / check_color_properties():

  1. _remove_repair() skipped repair whenever self.grandparent or self.parent.sibling was None:
    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 blk with left None and right 15 blk -> 20 red). Recoloring sibling, self.parent, and the outer red nephew before rotating in Case 6 removes any need to access self.grandparent / self.parent.sibling ahead of the rotation.
  2. black_height() returned 1 whenever either child was None:
    • False negatives: Trees with one None child and a black subtree on the other side (e.g. 10 blk -> right: 15 blk) returned 1 instead of None, causing check_color_properties() to report invalid trees as valid.
    • False positives: Valid trees where a black leaf sat alongside a black sibling with two red children (e.g. RedBlackTree(20).insert(-19).insert(-3).insert(-6).insert(-12)) returned None (Property 5) because a black leaf returned 1 while its sibling returned 2.
      Now None children contribute 0 and each node adds 1 - self.color, verifying both subtrees uniformly.
  3. Root tracking & rotation identity check:
    • insert() and remove() returned self.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).
    • Changed parent.left == self to parent.left is self in rotate_left() (matching rotate_right() for $O(1)$ identity checks instead of deep tree equality).

Verified 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.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues, then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files labels Nov 27, 2026

This branch has not been deployed

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

Labels

awaiting reviews This PR is ready to be reviewed enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant