From b73bb8d9015b73d1148a64b6ca7f79520e81508b Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Tue, 14 Oct 2014 08:16:41 +0000 Subject: [PATCH] =?UTF-8?q?avl=5Ftree:=20memory=20released,=20valgrind=20s?= =?UTF-8?q?ays=20=E2=80=9Cno=20leaks=20are=20possible=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lol/algorithm/avl_tree.h | 24 ++++++++++++++++++++++++ src/t/algorithm/avl_tree.cpp | 1 + 2 files changed, 25 insertions(+) diff --git a/src/lol/algorithm/avl_tree.h b/src/lol/algorithm/avl_tree.h index bc41be5b..f0769df4 100644 --- a/src/lol/algorithm/avl_tree.h +++ b/src/lol/algorithm/avl_tree.h @@ -52,6 +52,15 @@ public: return this->m_root->exists(key); } + virtual ~avl_tree() + { + if (this->m_root) + { + this->m_root->cascade_delete(); + delete this->m_root; + } + } + protected: class tree_node @@ -169,6 +178,21 @@ protected: return false; } + void cascade_delete() + { + if (this->m_child[0]) + { + this->m_child[0]->cascade_delete(); + delete this->m_child[0]; + } + + if (this->m_child[1]) + { + this->m_child[1]->cascade_delete(); + delete this->m_child[1]; + } + } + int get_balance() { return this->m_stairs[1] - this->m_stairs[0]; diff --git a/src/t/algorithm/avl_tree.cpp b/src/t/algorithm/avl_tree.cpp index f902ea58..1f510eab 100644 --- a/src/t/algorithm/avl_tree.cpp +++ b/src/t/algorithm/avl_tree.cpp @@ -20,6 +20,7 @@ namespace lol class test_tree : public avl_tree { public: + virtual ~test_tree() {} int get_root_balance() {