Pārlūkot izejas kodu

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

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> pirms 10 gadiem
vecāks
revīzija
b73bb8d901
2 mainītis faili ar 25 papildinājumiem un 0 dzēšanām
  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 Parādīt failu

@@ -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];


+ 1
- 0
src/t/algorithm/avl_tree.cpp Parādīt failu

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

int get_root_balance()
{


Notiek ielāde…
Atcelt
Saglabāt