From 1d144ebf529c14d88fa8106a1bbf54c3983d487d Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Thu, 9 Oct 2014 12:51:48 +0000 Subject: [PATCH] avl_tree.h: adding more test, fixing more bugs --- src/lol/algorithm/avl_tree.h | 4 ++-- src/t/algorithm/avl_tree.cpp | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lol/algorithm/avl_tree.h b/src/lol/algorithm/avl_tree.h index a33483c9..e330d33b 100644 --- a/src/lol/algorithm/avl_tree.h +++ b/src/lol/algorithm/avl_tree.h @@ -171,8 +171,8 @@ protected: void compute_balance() { - 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; + this->m_stairs_lo = this->m_lo ? lol::max(this->m_lo->m_stairs_lo, this->m_lo->m_stairs_hi) + 1 : 0; + this->m_stairs_hi = this->m_hi ? lol::max(this->m_hi->m_stairs_lo, this->m_hi->m_stairs_hi) + 1 : 0; } int get_balance() diff --git a/src/t/algorithm/avl_tree.cpp b/src/t/algorithm/avl_tree.cpp index 71986b30..42d10b5e 100644 --- a/src/t/algorithm/avl_tree.cpp +++ b/src/t/algorithm/avl_tree.cpp @@ -12,6 +12,8 @@ #include +#include + #include namespace lol @@ -60,6 +62,9 @@ lolunit_declare_fixture(AvlTreeTest) lolunit_assert_equal(tree.insert(-1, 1), true); lolunit_assert_equal(tree.get_root_balance(), -1); + + lolunit_assert_equal(tree.insert(-2, 1), true); + lolunit_assert_equal(tree.get_root_balance(), 0); } };