From 243a6b9e44485b8845d5fb2f2241a10cce419450 Mon Sep 17 00:00:00 2001 From: Joshua Young Date: Sun, 25 Aug 2024 09:03:12 +1000 Subject: [PATCH] fix RedBlackTree::LeafNode-s being marked red --- CHANGELOG.md | 1 + lib/red-black-tree.rb | 12 +++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) 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