Browse Source

avl_tree: iterator full implementation + test.

undefined
Guillaume Bittoun Sam Hocevar <sam@hocevar.net> 10 years ago
parent
commit
b05c252b96
2 changed files with 56 additions and 33 deletions
  1. +39
    -33
      src/lol/algorithm/avl_tree.h
  2. +17
    -0
      src/t/algorithm/avl_tree.cpp

+ 39
- 33
src/lol/algorithm/avl_tree.h View File

@@ -12,7 +12,6 @@


#pragma once #pragma once



namespace lol namespace lol
{ {


@@ -104,26 +103,36 @@ public:
class Iterator; class Iterator;
class ConstIterator; class ConstIterator;


Iterator get_iterator()
Iterator begin()
{ {
tree_node * node = nullptr; tree_node * node = nullptr;


if (this->m_root) if (this->m_root)
node = this->m_root->get_min();
this->m_root->get_min(node);


return Iterator(node); return Iterator(node);
} }


ConstIterator get_iterator() const
ConstIterator begin() const
{ {
tree_node * node = nullptr; tree_node * node = nullptr;


if (this->m_root) if (this->m_root)
node = this->m_root->get_min();
this->m_root->get_min(node);


return ConstIterator(node); return ConstIterator(node);
} }


Iterator end()
{
return Iterator(nullptr);
}

ConstIterator end() const
{
return ConstIterator(nullptr);
}

protected: protected:


class tree_node class tree_node
@@ -234,34 +243,31 @@ protected:
return false; return false;
} }


void get_min(tree_node * & min_node) const
void get_min(tree_node * & min_node)
{ {
min_node = this; min_node = this;


while (this->m_child[0])
min_node = this->m_child[0];
while (min_node->m_child[0])
min_node = min_node->m_child[0];
} }


void get_max(tree_node * & max_node) const void get_max(tree_node * & max_node) const
{ {
max_node = this; max_node = this;


while (this->m_child[1])
max_node = this->m_child[1];
while (max_node->m_child[1])
max_node = max_node->m_child[1];
} }


void cascade_delete() void cascade_delete()
{ {
if (this->m_child[0])
{
this->m_child[0]->cascade_delete();
delete this->m_child[0];
}

if (this->m_child[1])
for (int i = 0 ; i < 2 ; ++i)
{ {
this->m_child[1]->cascade_delete();
delete this->m_child[1];
if (this->m_child[i])
{
this->m_child[i]->cascade_delete();
delete this->m_child[i];
}
} }
} }


@@ -384,14 +390,14 @@ public:


struct OutputValue struct OutputValue
{ {
OutputValue(K const & key, V & value) :
m_key(key),
m_value(value)
OutputValue(K const & _key, V & _value) :
key(_key),
value(_value)
{ {
} }


K const & m_key;
V & m_value;
K const & key;
V & value;
}; };


class Iterator class Iterator
@@ -446,9 +452,9 @@ public:
return OutputValue(this->m_node->get_key(), this->m_node->get_value()); return OutputValue(this->m_node->get_key(), this->m_node->get_value());
} }


bool operator==(Iterator const & that) const
bool operator!=(Iterator const & that) const
{ {
return this->m_node = that->m_node;
return this->m_node != that.m_node;
} }


protected: protected:
@@ -458,14 +464,14 @@ public:


struct ConstOutputValue struct ConstOutputValue
{ {
ConstOutputValue(K const & key, V const & value) :
m_key(key),
m_value(value)
ConstOutputValue(K const & _key, V const & _value) :
key(_key),
value(_value)
{ {
} }


K const & m_key;
V const & m_value;
K const & key;
V const & value;
}; };


class ConstIterator class ConstIterator
@@ -520,9 +526,9 @@ public:
return ConstOutputValue(this->m_node->get_key(), this->m_node->get_value()); return ConstOutputValue(this->m_node->get_key(), this->m_node->get_value());
} }


bool operator==(ConstIterator const & that) const
bool operator!=(ConstIterator const & that) const
{ {
return this->m_node = that->m_node;
return this->m_node != that.m_node;
} }


protected: protected:


+ 17
- 0
src/t/algorithm/avl_tree.cpp View File

@@ -173,6 +173,23 @@ lolunit_declare_fixture(AvlTreeTest)
lolunit_assert_equal(tree.try_get_value(67, value_ptr), false); lolunit_assert_equal(tree.try_get_value(67, value_ptr), false);
lolunit_assert_equal(*value_ptr, 3); lolunit_assert_equal(*value_ptr, 3);
} }

lolunit_declare_test(AvlTreeTestIteratorRead)
{
test_tree tree;

for (int i = 1 ; i < 100 ; ++i)
tree.insert(i, 2 * i + i % 3);

int tmp = 0;

for (auto iterator : tree)
{
lolunit_assert_equal(iterator.key > tmp, true);
lolunit_assert_equal(iterator.value == (iterator.key * 2 + iterator.key % 3), true);
tmp = iterator.key;
}
}
}; };


} }

Loading…
Cancel
Save