-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdictionnaire.cpp
More file actions
95 lines (71 loc) · 2.31 KB
/
dictionnaire.cpp
File metadata and controls
95 lines (71 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "dictionnaire.h"
#include <iostream>
Dictionnaire::Dictionnaire() : racine(nullptr) {}
Dictionnaire::Dictionnaire(const Dictionnaire& d) : racine(copierArbre(d.racine)) {}
Dictionnaire::~Dictionnaire() {
detruireArbre(racine);
}
void Dictionnaire::ajouterMot(const std::string& mot) {
if (mot.empty()) return;
ajouterMotRec(racine, mot, 0);
}
void Dictionnaire::ajouterMotRec(Noeud*& noeud, const std::string& mot, size_t index) {
if (!noeud) {
noeud = new Noeud(mot[index]);
}
if (index == mot.size() - 1) {
noeud->finMot = true;
return;
}
ajouterMotRec(noeud->gauche, mot, index + 1);
}
bool Dictionnaire::enleverMot(const std::string& mot) {
return enleverMotRec(racine, mot, 0);
}
bool Dictionnaire::enleverMotRec(Noeud*& noeud, const std::string& mot, size_t index) {
if (!noeud) return false;
if (index == mot.size() - 1) {
if (noeud->finMot) {
noeud->finMot = false;
return true;
}
return false;
}
return enleverMotRec(noeud->gauche, mot, index + 1);
}
bool Dictionnaire::chercherMot(const std::string& mot) const {
return chercherMotRec(racine, mot, 0);
}
bool Dictionnaire::chercherMotRec(const Noeud* noeud, const std::string& mot, size_t index) const {
if (!noeud) return false;
if (index == mot.size() - 1) {
return noeud->finMot;
}
return chercherMotRec(noeud->gauche, mot, index + 1);
}
void Dictionnaire::afficherDict() const {
afficherDictRec(racine, "");
}
void Dictionnaire::afficherDictRec(const Noeud* noeud, std::string mot) const {
if (!noeud) return;
std::string nouveauMot = mot + noeud->lettre;
if (noeud->finMot) {
std::cout << nouveauMot << std::endl;
}
afficherDictRec(noeud->gauche, nouveauMot);
afficherDictRec(noeud->droite, mot);
}
Noeud* Dictionnaire::copierArbre(const Noeud* noeud) {
if (!noeud) return nullptr;
Noeud* nouveauNoeud = new Noeud(noeud->lettre);
nouveauNoeud->finMot = noeud->finMot;
nouveauNoeud->gauche = copierArbre(noeud->gauche);
nouveauNoeud->droite = copierArbre(noeud->droite);
return nouveauNoeud;
}
void Dictionnaire::detruireArbre(Noeud* noeud) {
if (!noeud) return;
detruireArbre(noeud->gauche);
detruireArbre(noeud->droite);
delete noeud;
}