diff --git a/CHANGELOG.md b/CHANGELOG.md index 121b930..d19dd51 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` diff --git a/lib/red-black-tree.rb b/lib/red-black-tree.rb index 8225fc7..1bab1ed 100644 --- a/lib/red-black-tree.rb +++ b/lib/red-black-tree.rb @@ -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! @@ -194,7 +196,7 @@ 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 @@ -202,7 +204,7 @@ def delete! node end if node.parent.red? - node.sibling.red! + node.sibling.red! unless node.sibling.leaf? node.parent.black! break