Commit d5af900
Changed files (2)
lib
data_structures
spec
data_structures
lib/data_structures/avl_tree.rb
@@ -61,6 +61,11 @@ class AVLTree
new_root = @right
@right = nil
new_root
+ elsif balance_factor > 1
+ @left.right = self
+ new_root = @left
+ @left = nil
+ new_root
else
self
end
spec/data_structures/avl_tree_spec.rb
@@ -81,5 +81,30 @@ describe AVLTree do
sut.root.right.data.should == "c"
end
end
+
+ context "with a left-left case" do
+ before :each do
+ sut.add("c")
+ sut.add("b")
+ sut.add("a")
+ end
+
+ it "should re-balance it self" do
+ sut.height.should == 2
+ end
+
+ it "should have a new root" do
+ sut.root.data.should == "b"
+ end
+
+ it "should change the left side" do
+ sut.root.left.data.should == "a"
+ end
+
+ it "should change the right side" do
+ sut.root.right.data.should == "c"
+ end
+ end
+
end
end