URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-10-29 Guillaume Duhamel <guillaume.duhamel(a)lrde.epita.fr>
New method to check consistency of tree.
* mln/util/tree.hh: Add new method which checks the
consistency of a tree.
* tests/tree.cc: Add this test at the end of file.
---
mln/util/tree.hh | 35 +++++++++++++++++++++++++++--------
tests/tree.cc | 1 +
2 files changed, 28 insertions(+), 8 deletions(-)
Index: trunk/milena/tests/tree.cc
===================================================================
--- trunk/milena/tests/tree.cc (revision 1403)
+++ trunk/milena/tests/tree.cc (revision 1404)
@@ -60,4 +60,5 @@
util::tree<unsigned>* tre = new util::tree<unsigned>(&node);
mln_assertion(tre);
tre->add_tree_up(elt6);
+ mln_assertion (tre->check_consistency());
}
Index: trunk/milena/mln/util/tree.hh
===================================================================
--- trunk/milena/mln/util/tree.hh (revision 1403)
+++ trunk/milena/mln/util/tree.hh (revision 1404)
@@ -29,7 +29,7 @@
# define MLN_UTIL_TREE_HH
# include <vector>
-
+# include <iostream>
# include <mln/core/contract.hh>
/*!
@@ -82,6 +82,7 @@
void set_parent(node<T>* parent);
void print_rec(int n) const;
void print() const;
+ bool check_consistency();
int search_rec(node<T>** res, T& elt);
node<T>* search(T& elt);
@@ -102,6 +103,7 @@
node<T>* root();
branch<T> main_branch();
+ bool check_consistency();
void add_tree_up (T& elt);
void add_tree_down (T& elt);
@@ -174,6 +176,13 @@
template <typename T>
+ bool
+ tree<T>::check_consistency()
+ {
+ return root()->check_consistency ();
+ }
+
+ template <typename T>
node<T>::node()
: parent_ (0)
{
@@ -252,13 +261,6 @@
return parent_;
}
-// template <typename T>
-// node<T>*&
-// node<T>::parent()
-// {
-// return parent_;
-// }
-
template <typename T>
int
node<T>::search_rec(node<T>** res, T& elt)
@@ -291,6 +293,23 @@
return 0;
}
+ template <typename T>
+ bool
+ node<T>::check_consistency()
+ {
+ for (typename std::vector<node<T>* >::iterator it =
this->child_.begin();
+ it != this->child_.end(); ++it)
+ {
+ if ((**it).parent() != this)
+ return false;
+
+ if (!((**it).check_consistency()))
+ return false;
+ }
+ return true;
+ }
+
+
// Branch methods
template <typename T>
branch<T>::branch(util::tree<T>& tree,