
Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr> writes:
URL: https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog: 2007-10-29 Guillaume Duhamel <guillaume.duhamel@lrde.epita.fr>
New method to check consistency of tree.
* mln/util/tree.hh: Add new method which checks the consistency of a tree.
Le mieux est encore de dire quelle(s) est (sont) cette (ces) nouvelle(s) méthode(s), comme ceci : * mln/util/tree.hh (node<T>::check_consistency) (tree<T>::check_consistency()): New methods. Check the consistency of a tree.
* tests/tree.cc: Add this test at the end of file.
C'est bien ; juste pour la forme, voici une autre façon de mentionner l'utilisation de check_consistency dans tests/tree.cc : * mln/util/tree.hh (node<T>::check_consistency) (tree<T>::check_consistency()): New methods. Check the consistency of a tree. Use it... * tests/tree.cc: ...here. (Mais c'est vraiment du détail.)
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; + }
Bien !