瀏覽代碼

avl_tree: memory released, valgrind says “no leaks are possible”

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 11 年之前
父節點
當前提交
b73bb8d901
共有 2 個檔案被更改,包括 25 行新增0 行删除
  1. +24
    -0
      src/lol/algorithm/avl_tree.h
  2. +1
    -0
      src/t/algorithm/avl_tree.cpp

+ 24
- 0
src/lol/algorithm/avl_tree.h 查看文件

@@ -52,6 +52,15 @@ public:
return this->m_root->exists(key); return this->m_root->exists(key);
} }


virtual ~avl_tree()
{
if (this->m_root)
{
this->m_root->cascade_delete();
delete this->m_root;
}
}

protected: protected:


class tree_node class tree_node
@@ -169,6 +178,21 @@ protected:
return false; 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() int get_balance()
{ {
return this->m_stairs[1] - this->m_stairs[0]; return this->m_stairs[1] - this->m_stairs[0];


+ 1
- 0
src/t/algorithm/avl_tree.cpp 查看文件

@@ -20,6 +20,7 @@ namespace lol
class test_tree : public avl_tree<int, int> class test_tree : public avl_tree<int, int>
{ {
public: public:
virtual ~test_tree() {}


int get_root_balance() int get_root_balance()
{ {


Loading…
取消
儲存