diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2013-10-29 17:42:34 +0100 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2013-10-29 17:42:34 +0100 |
commit | 8f1093f0e00f9b1df7ce343a879303fd56a95d08 (patch) | |
tree | 6aaf0720c2093ba05cb81ba7f95b4e9808b3ecab /tests/exec/ABR.cpp | |
download | LPC-Projet-8f1093f0e00f9b1df7ce343a879303fd56a95d08.tar.gz LPC-Projet-8f1093f0e00f9b1df7ce343a879303fd56a95d08.zip |
First commit.
Diffstat (limited to 'tests/exec/ABR.cpp')
-rw-r--r-- | tests/exec/ABR.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/tests/exec/ABR.cpp b/tests/exec/ABR.cpp new file mode 100644 index 0000000..979ff4b --- /dev/null +++ b/tests/exec/ABR.cpp @@ -0,0 +1,60 @@ +#include <iostream> + +/* arbres binaires de recherche */ + +class ABR { +public: + int valeur; + ABR *gauche; + ABR *droite; + ABR(ABR *g, int v, ABR *d); + void insere(int x); + int contient(int x); + void affiche(); +}; + +ABR::ABR(ABR *g, int v, ABR *d) { valeur = v; gauche = g; droite = d; } + +void ABR::insere(int x) { + if (x == valeur) return; + if (x < valeur) { + if (gauche == NULL) + gauche = new ABR(NULL, x, NULL); + else + gauche->insere(x); + } else + if (droite == NULL) + droite = new ABR(NULL, x, NULL); + else + droite->insere(x); +} + +int ABR::contient(int x) { + if (x == valeur) return true; + if (x < valeur && gauche != NULL) return gauche->contient(x); + if (droite != NULL) return droite->contient(x); + return false; +} + +void ABR::affiche() { + if (gauche != NULL) gauche->affiche(); + std::cout << "(" << valeur << ")"; + if (droite != NULL) droite->affiche(); +} + + +int main() { + ABR dico = ABR(NULL, 1, NULL); + dico.insere(17); + dico.insere(5); + dico.insere(8); + dico.affiche(); std::cout << "\n"; + + if (dico.contient(5) && + ! dico.contient(0) && + dico.contient(17) && + ! dico.contient(3)) + std::cout << "ok\n"; + + dico.affiche(); std::cout << "\n"; +} |