Voici un petit projet que j'ai à compléter en C++ sur les arbres binaires et équilibrés
je ne viens pas vous demander de l'aide pour le faire, vu que j'ai réussi à tout faire, et que ca marche (enfin à priori tout ce qu'on a testé, mais comme on nous a deja dit, ca peut marcher 1000 fois et la 1001eme fois aller dans le mur
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358 #ifndef _Binary_tree #define _Binary_tree #include <math.h> #include <stddef.h> // NULL #include <cassert> template<class T> class Binary_tree { protected: T _content; Binary_tree<T>* _left,*_right,*_super; int number_node() const; int search() const; public: inline virtual bool validator() const{ return true;} /* Fonction utile pour l'arbre AVL */ Binary_tree(const T&); int h() const; int H() const; virtual bool isAVL() const; virtual void push_left(Binary_tree<T>*); virtual void push_right(Binary_tree<T>*); inline Binary_tree<T>* root() {if(! _super) return this;return _super->root();}; inline Binary_tree<T>*& super() {return _super;}; virtual bool isComplete() const; virtual bool isPerfect() const; virtual bool remove_left(); virtual bool remove_right(); }; template<class T> Binary_tree<T>::Binary_tree(const T& t) : _content(t) { _left = _right = _super = NULL; } template<class T> int Binary_tree<T>::h() const { if(! _super) return 0; return _super->h() + 1; } template<class T> int Binary_tree<T>::H() const { int H_left,H_right; _left ? H_left = _left->H() + 1 : H_left = 0; _right ? H_right = _right->H() + 1 : H_right = 0; return H_left < H_right ? H_right : H_left; } template<class T> bool Binary_tree<T>::isAVL() const { if(! _left && ! _right) return true; if(! _left) return _right->H() < 1; /* il faut enlever le = ? */ if(! _right) return _left->H() < 1; return ::abs(_right->H() - _left->H()) <= 1; } template<class T> void Binary_tree<T>::push_left(Binary_tree<T>* left) { bool add; assert(left); if(_left || left->_super) return; left->_super = this; _left = left; add = root()->validator(); if(add == false){ left->_super = NULL; _left = NULL; } } template<class T> void Binary_tree<T>::push_right(Binary_tree<T>* right) { bool add; assert(right); if(_right || right->_super) return; right->_super = this; _right = right; add = root()->validator(); if(add == false){ right->_super = NULL; _right = NULL; } } template<class T> int Binary_tree<T>::number_node() const { int nbNode=0; if(_left != NULL) nbNode = nbNode + _left->number_node(); if(_right != NULL) nbNode = nbNode + _right->number_node(); return (1+nbNode); } template<class T> bool Binary_tree<T>::isComplete() const { double height; /* nombre de noeuds théoriques selon la hauteur */ double nbNode; height = pow(2,H()+1)-1; nbNode = number_node(); if(height == nbNode)return true; else return false; } template<class T> int Binary_tree<T>::search() const{ if(!_right && !_left) return 1; if(!_left && _right) return 0; if(_left && !_right) return 1; if(_left->search() >= _right->search()) return 1; else return 0; } template<class T> bool Binary_tree<T>::isPerfect() const { double height; /* Nombre de noeuds théoriques minimum selon la hauteur */ double nbNode; height = pow(2,H())-1; nbNode = number_node(); if(nbNode <= height) return false; else return search(); } template<class T> bool Binary_tree<T>::remove_left() { Binary_tree<T> *temp1,*temp2; bool del = true; /* valide ou pas la suppression */ temp1 = _left; temp2 = _left->_super; _left->_super = NULL; _left = NULL; del = root()->validator(); if(del == false){ _left = temp1; _left->_super = temp2; } return del; } template<class T> bool Binary_tree<T>::remove_right() { Binary_tree<T> *temp1,*temp2; bool del = true; /* valide ou pas la suppression */ temp1 = _right; temp2 = _right->_super; _right->_super = NULL; _right = NULL; del = root()->validator(); if(del == false){ _right = temp1; _right->_super = temp2; } return del; } template<class T> class AVL_tree : public Binary_tree<T> { protected: virtual bool validator() const; /* Valide ou pas la modification de l'arbre AVL */ public: AVL_tree(const T&); virtual void push_left(Binary_tree<T>*); virtual void push_right(Binary_tree<T>*); inline bool isAVL() const { return true;} }; template<class T> AVL_tree<T>::AVL_tree(const T& t) : Binary_tree<T>(t) { } template<class T> void AVL_tree<T>::push_left(Binary_tree<T>* left) { bool add; assert(left); if(_left || left->super()) return; if(! _right && left->H() > 1) return; if(_right && ::abs(_right->H() - left->H()) > 1) return; left->super() = this; _left = left; if(_right != NULL) return; add = root()->validator(); if(add == false){ left->super() = NULL; _left = NULL; } } template<class T> void AVL_tree<T>::push_right(Binary_tree<T>* right) { bool add; assert(right); if(_right || right->super()) return; if(! _left && right->H() > 1) return; if(_left && ::abs(_left->H() - right->H()) > 1) return; right->super() = this; _right = right; if(_left != NULL) return; add = root()->validator(); if(add == false){ right->super() = NULL; _right = NULL; } } template<class T>bool AVL_tree<T>::validator() const{ if(_super == NULL){ if(this->Binary_tree<T>::isAVL()) return true; else return false; } } #endif)
c'est surtout pour savoir si le code était propre ?, car c'est important et meme si on avait une partie en modèle, je préfère avoir des avis de pro comme vous
d'ailleurs si vous voyez quelquechose qui cloche aussi n'hésitez pas
je suis loin de maitriser les algorithmes et le C++ alors ca me permettra de m'améliorer
merci d'avance !!
Partager