From 9b46f5d22b0ba176b048737908544c28e8c3a983 Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Thu, 9 Oct 2014 12:06:29 +0000 Subject: [PATCH] avl_tree: fixing tests --- src/lol/algorithm/avl_tree.h | 14 ++++++++------ src/t/algorithm/avl_tree.cpp | 2 +- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/lol/algorithm/avl_tree.h b/src/lol/algorithm/avl_tree.h index e162d0f5..a33483c9 100644 --- a/src/lol/algorithm/avl_tree.h +++ b/src/lol/algorithm/avl_tree.h @@ -123,14 +123,16 @@ protected: if (this->get_balance() == 2) { - return this->rotate(CW); + return this->rotate(CCW); } else if (this->get_balance() == -2) { - return this->rotate(CCW); + return this->rotate(CW); } else - ASSERT(lol::abs(this->m_stairs_lo - this->m_stairs_hi) < 3); + ASSERT(lol::abs(this->m_stairs_hi - this->m_stairs_lo) < 3); + + return 0; } enum Rotation { CW = 0, CCW = 1 }; @@ -169,13 +171,13 @@ protected: void compute_balance() { - this->m_stairs_lo = this->m_lo ? this->m_lo->m_stairs_lo + this->m_lo->m_stairs_hi : 0; - this->m_stairs_hi = this->m_hi ? this->m_hi->m_stairs_lo + this->m_lo->m_stairs_hi : 0; + this->m_stairs_lo = this->m_lo ? this->m_lo->m_stairs_lo + this->m_lo->m_stairs_hi + 1 : 0; + this->m_stairs_hi = this->m_hi ? this->m_hi->m_stairs_lo + this->m_hi->m_stairs_hi + 1 : 0; } int get_balance() { - return this->m_stairs_lo - this->m_stairs_hi; + return this->m_stairs_hi - this->m_stairs_lo; } protected: diff --git a/src/t/algorithm/avl_tree.cpp b/src/t/algorithm/avl_tree.cpp index 4c6ce83e..71986b30 100644 --- a/src/t/algorithm/avl_tree.cpp +++ b/src/t/algorithm/avl_tree.cpp @@ -59,7 +59,7 @@ lolunit_declare_fixture(AvlTreeTest) lolunit_assert_equal(tree.get_root_balance(), -1); lolunit_assert_equal(tree.insert(-1, 1), true); - lolunit_assert_equal(tree.get_root_balance(), 0); + lolunit_assert_equal(tree.get_root_balance(), -1); } };