Skip to content

Commit

Permalink
fix RedBlackTree::LeafNode-s being marked red
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuay03 committed Aug 24, 2024
1 parent 1ec1acb commit 243a6b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## [Unreleased]

- Fix `RedBlackTree::LeafNode`s being marked red
- Handle comparison with `RedBlackTree::LeafNode` in subclasses of `RedBlackTreeNode`
- Add `RedBlackTree#include?`
- Add `RedBlackTree#search`
Expand Down
12 changes: 7 additions & 5 deletions lib/red-black-tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,11 @@ def delete! node
end

if node.distant_nephew && node.distant_nephew.valid? && node.distant_nephew.red?
case node.parent.colour
when Node::RED then node.sibling.red!
when Node::BLACK then node.sibling.black!
unless node.sibling.leaf?
case node.parent.colour
when Node::RED then node.sibling.red!
when Node::BLACK then node.sibling.black!
end
end
node.parent.black!
node.distant_nephew.black!
Expand All @@ -194,15 +196,15 @@ def delete! node
end

if node.close_nephew && node.close_nephew.valid? && node.close_nephew.red?
node.sibling.red!
node.sibling.red! unless node.sibling.leaf?
node.close_nephew.black!
rotate_sub_tree! node.sibling, opp_direction

next
end

if node.parent.red?
node.sibling.red!
node.sibling.red! unless node.sibling.leaf?
node.parent.black!

break
Expand Down

0 comments on commit 243a6b9

Please sign in to comment.