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
|
#include <iostream>
#include <sstream>
#include <fstream>
#include <string>
//#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "progListeSC.h"
using namespace std;
typedef struct noeud {
int info;
struct noeud *sag;
struct noeud *sad;} NoeudSC;
typedef NoeudSC *ArbreBin;
ArbreBin creerArbreBin(int e, ArbreBin G, ArbreBin D){
/* Res : renvoie une ArbreBin dont la racine vaut e, le sag G et le sad D */
ArbreBin A = new NoeudSC;
A->info=e; A->sag=G; A->sad=D;
return A;}
void codageABdot(ofst ream& fichier, ArbreBin A){
if (A != NULL){
fichier << (long) A << " [label=\"" << A->info << "\" ] ;\n";
if (A->sag != NULL) {
fichier << (long)A << " -> " << (long)(A->sag) << " [color=\"red\",label=\"g\" ] ;\n";
codageABdot(fichier,A->sag);}
if (A->sad != NULL) {
fichier << (long)A << " -> " << (long)(A->sad) << " [color=\"blue\",label=\"d\" ] ;\n";
codageABdot(fichier,A->sad);}
}
return;}
void dessinerAB(ArbreBin A, const char * nomFic, string titre){
ofstream f(nomFic);
if (!f.is_open()){
cout << "Impossible d'ouvrir le fichier en �criture !" << endl;
}
else {
f<< "digraph G { label = \""<< titre << "\" \n";
codageABdot(f,A);
f << "\n }\n" ;
f.close();}
return;}
/* A COMPLETER */
int sommeNoeuds(ArbreBin A){
/* renvoie la somme des etiquettes des noeuds de l arbre binaire A */
/* A COMPLETER */
return A == NULL ? 0: A->info+sommeNoeuds(A->sag)+sommeNoeuds(A->sad);
}
int profMinFeuille(ArbreBin A){
/* renvoie la profondeur minimum des feuilles de l'arbre A ; A est non vide */
assert(A!=NULL);
/* A COMPLETER */
return A==NULL ? 0: A->info + ( profMinFeuille(A->sag)<= profMinFeuille(A->sad)) ? profMinFeuille(A->sag):profMinFeuille(A->sad));
}
ListeSC parcoursInfixe(ArbreBin A){
/* renvoie la liste composee des etiquettes des noeuds de l'arbre A ordonn�e
selon l'ordre infixe */
/* A COMPLETER */
return A==NULL ? NULL: concatLSC(concatLSC(parcoursInfixe((A->sag), A->info),A->sad));
}
void effeuiller(ArbreBin& A){
/* modifie l'arbre A en supprimant ses feuilles */
/* A COMPLETER */
return;}
void tailler(ArbreBin& A, int p){
/* modifie l'arbre A, en supprimant ses noeuds de profondeur au moins p ; p est un entier positif ou nul */
/* A COMPLETER */
return;}
void tronconner(ArbreBin& A){
/* modifie l'arbre A, en supprimant les noeuds dont un seul sous-arbre est vide */
/* A COMPLETER */
return;}
ArbreBin genereAB(int n){
/* A COMPLETER */
return NULL;}
bool estParfait(ArbreBin A){
// V�rifie si A est un arbre binaire parfait
return true;
}
/*****************************************************************************/
/* */
/* main */
/* */
/*****************************************************************************/
int main(int argc, char *argv[]){
int q,i;
ArbreBin A,B,C;
ostringstream stre;
ListeSC L;
string chaine;
A=creerArbreBin(8,
creerArbreBin(7,
creerArbreBin(4,NULL,NULL),
creerArbreBin(9,NULL,NULL)),
creerArbreBin(3,NULL,NULL));
B=creerArbreBin(8,
creerArbreBin(2,
creerArbreBin(4,NULL,NULL),
creerArbreBin(9,
NULL,
creerArbreBin(1,
NULL,
creerArbreBin(7,
creerArbreBin(11,NULL,NULL),
creerArbreBin(5,
NULL,
NULL))))),
creerArbreBin(3,
creerArbreBin(12,
creerArbreBin(6,NULL,NULL),
NULL),
creerArbreBin(9,NULL,NULL)));
C=NULL;
C= creerArbreBin(1,C,C);
cout << "Numero de la question traitee (1/2/3/4/5/6/7) ? ";
cin >> q;
switch (q){
case 1 :
dessinerAB(A,"arbre.dot","Arbre Bin");
cout << "Somme des noeuds de l'arbre :"<< sommeNoeuds(A) << endl;
cout << "Profondeur minimum des feuilles de l'arbre : " << profMinFeuille(A) << endl;
system("dotty arbre.dot");
break;
case 2 :
dessinerAB(A,"arbre.dot","Arbre Bin");
L=parcoursInfixe(A);
cout << "Liste des noeuds de l'arbre en ordre infixe : ";
afficherLSC(L);
system("dotty arbre.dot");
break;
case 3 :
dessinerAB(B,"arbre.dot","Arbre Bin");
system("dotty arbre.dot&");
effeuiller(B);
dessinerAB(B,"arbre2.dot","Arbre Bin effeuille");
system("dotty arbre2.dot");
break;
case 4 :
dessinerAB(B,"arbre.dot","Arbre Bin");
system("dotty arbre.dot&");
cout << " Donner une profondeur (entier positif) :";
cin >> i;
tailler(B,i);
stre << i;
chaine = stre.str();
chaine = "Arbre Bin taille a la profondeur " + chaine;
dessinerAB(B,"arbre2.dot",chaine);
system("dotty arbre2.dot&");
break;
case 5 :
cout << " Donner un entier positif :";
cin >> i;
stre << i;
chaine = "Arbre Bin " + stre.str();
dessinerAB(genereAB(i),"arbre.dot", "Arbre Bin " + stre.str());
system("dotty arbre.dot&");
break;
case 6 :
dessinerAB(B,"arbre.dot","Arbre Bin");
system("dotty arbre.dot&");
tronconner(B);
dessinerAB(B,"arbre2.dot","Arbre tronconne");
system("dotty arbre2.dot&");
break;
case 7 :
A=genereAB(7);
chaine= estParfait(A) ? "Arbre parfait" : "Arbre non parfait";
dessinerAB(A,"arbre.dot",chaine);
system("dotty arbre.dot");
B=genereAB(8);
chaine= estParfait(B) ? "Arbre parfait" : "Arbre non parfait";
dessinerAB(B,"arbre2.dot",chaine);
system("dotty arbre2.dot&");
break;
}
return 0;
}
|