From 2d8a36e932fa79ef6e7dc7fcc6201180931be61a Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Wed, 5 Jun 2024 23:43:04 +0200 Subject: [PATCH] Thomas Cormen save us --- libtree/src/red_black_tree.rs | 38 +++++++++++++++-------------------- 1 file changed, 16 insertions(+), 22 deletions(-) diff --git a/libtree/src/red_black_tree.rs b/libtree/src/red_black_tree.rs index c41814a..f0f0f22 100644 --- a/libtree/src/red_black_tree.rs +++ b/libtree/src/red_black_tree.rs @@ -278,19 +278,16 @@ impl RedBlackTree { parent.set_colour(Black); grandparent.set_colour(Red); node = grandparent; - continue; - } + } else { + if parent.right() == node { + node = parent; + self.rotate_left(node); + } - if parent.right() == node { - self.rotate_left(parent); - let temp = parent; - parent = node; - node = temp; + parent.set_colour(Black); + grandparent.set_colour(Red); + self.rotate_right(grandparent); } - - parent.set_colour(Black); - grandparent.set_colour(Red); - self.rotate_right(grandparent); } else { let mut uncle = grandparent.left(); if !uncle.is_null() && uncle.colour() == Red { @@ -298,19 +295,16 @@ impl RedBlackTree { parent.set_colour(Black); grandparent.set_colour(Black); node = grandparent; - continue; - } + } else { + if parent.left() == node { + node = parent; + self.rotate_right(node); + } - if parent.left() == node { - self.rotate_right(parent); - let temp = parent; - parent = node; - node = temp; + parent.set_colour(Black); + grandparent.set_colour(Red); + self.rotate_left(grandparent); } - - parent.set_colour(Black); - grandparent.set_colour(Red); - self.rotate_left(grandparent); } } self.root.set_colour(Black);