-
Notifications
You must be signed in to change notification settings - Fork 0
/
abc.cpp
222 lines (177 loc) · 5.42 KB
/
abc.cpp
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
#include <iostream.h>
#include <stdlib.h>
#include <time.h>
#ifdef PERFPROFILE
#include <google/heap-profiler.h>
#endif
#define NELEMENTSDEMO 1000000
#define NELEMENTSBASE 100000000
template <class C, class I>
class ABC {
public:
ABC(void) : arrel(NULL), numelements(0), alturaarbre(0) {}
~ABC(void);
void inserir(C pclau, I pinfo);
void inordre(void);
I consultar(C pclau);
C minim(void);
C maxim(void);
void esborrar(C pclau);
int elements(void);
int altura(void);
private:
class Node {
public:
C clau;
I info;
Node *fesq;
Node *fdre;
Node(C pclau, I pinfo, Node *pfesq = NULL, Node *pfdre = NULL) : clau(pclau), info(pinfo), fesq(pfesq), fdre(pfdre) {}
};
Node *arrel;
Node *actual;
int numelements;
int alturaarbre;
void inserir(C pclau, I pinfo, Node **node, int paltura);
void inordre(Node **node);
I consultar(C pclau, Node **node);
C minim(Node **node);
C maxim(Node **node);
void esborrar(C pclau, Node **node);
Node * buscaTreuIRetornaMinim(Node **node);
};
template <class C, class I> ABC<C, I>::~ABC(void) {
delete arrel;
delete actual;
}
template <class C, class I> void ABC<C, I>::inserir(C pclau, I pinfo) {
this->inserir(pclau, pinfo, &arrel, 1);
}
template <class C, class I> void ABC<C, I>::inordre(void) {
this->inordre(&arrel);
}
template <class C, class I> I ABC<C, I>::consultar(C pclau) {
return consultar(pclau, &arrel);
}
template <class C, class I> C ABC<C, I>::minim(void) {
return minim(&arrel);
}
template <class C, class I> C ABC<C, I>::maxim(void) {
return maxim(&arrel);
}
template <class C, class I> int ABC<C, I>::elements(void) {
return numelements;
}
template <class C, class I> void ABC<C, I>::esborrar(C pclau) {
esborrar(pclau, &arrel);
}
template <class C, class I> int ABC<C, I>::altura(void) {
return alturaarbre;
}
template <class C, class I> void ABC<C, I>::inserir(C pclau, I pinfo, Node **node, int paltura) {
if(*node == NULL) {
*node = new Node(pclau, pinfo);
numelements++;
if(alturaarbre<paltura) alturaarbre=paltura;
}
else if(pclau > (*node)->clau) inserir(pclau, pinfo, &(*node)->fdre, paltura+1);
else if(pclau < (*node)->clau) inserir(pclau, pinfo, &(*node)->fesq, paltura+1);
else {
(*node)->clau = pclau;
(*node)->info = pinfo;
}
}
template <class C, class I> void ABC<C, I>::inordre(Node **node) {
if(*node == NULL) return;
inordre(&(*node)->fesq);
cout << "[" << (*node)->clau << ", " << (*node)->info << "]" << endl;
inordre(&(*node)->fdre);
}
template <class C, class I> I ABC<C, I>::consultar(C pclau, Node **node) {
if(*node == NULL) throw "consultar: element no trobat";
if((*node)->clau > pclau) return consultar(pclau, &(*node)->fesq);
if((*node)->clau < pclau) return consultar(pclau, &(*node)->fdre);
return (*node)->info;
}
template <class C, class I> C ABC<C, I>::minim(Node **node) {
if(*node == NULL) throw "minim: arbre buit";
if((*node)->fesq != NULL) return minim(&(*node)->fesq);
return (*node)->clau;
}
template <class C, class I> C ABC<C, I>::maxim(Node **node) {
if(*node == NULL) throw "maxim: arbre buit";
if((*node)->fdre != NULL) return maxim(&(*node)->fdre);
return (*node)->clau;
}
template <class C, class I> void ABC<C, I>::esborrar(C pclau, Node **node) {
if(*node == NULL) return;
if((*node)->clau > pclau) return esborrar(pclau, &(*node)->fesq);
if((*node)->clau < pclau) return esborrar(pclau, &(*node)->fdre);
else {
Node *q = *node;
if((*node)->fdre == NULL)
*node = (*node)->fesq;
else if((*node)->fesq == NULL)
*node = (*node)->fdre;
else {
Node *min = buscaTreuIRetornaMinim(&(*node)->fdre);
min->fesq = (*node)->fesq;
min->fdre = (*node)->fdre;
*node = min;
}
delete q;
numelements--;
}
}
template <class C, class I> typename ABC<C, I>::Node * ABC<C, I>::buscaTreuIRetornaMinim(Node **node) {
if(*node == NULL) return NULL;
if((*node)->fesq != NULL) return buscaTreuIRetornaMinim(&(*node)->fesq);
Node *q = *node;
*node = (*node)->fdre;
return q;
}
int main(int argc, char **argv) {
int unnumero;
#ifdef PERFPROFILE
HeapProfilerStart("/Users/Sergio/Desktop/volcado");
HeapProfilerDump("**Heap a l'inici**");
#endif
cout << "> Demostració d'ABCs" << endl;
cout << "> Inicialització de variables de l'entorn" << endl;
srand(time(NULL));
cout << "> Creem un arbre, buit per defecte" << endl;
ABC<int, int> arbre;
cout << "> Afegim un total de " << NELEMENTSDEMO << " element(s), cadascun un natural de l'1 al " << NELEMENTSBASE << endl;
for(int i=0; i<NELEMENTSDEMO; ++i) {
int tmp = (rand() % NELEMENTSBASE) + 1;
unnumero = tmp;
arbre.inserir(tmp, tmp);
}
cout << "> Altura de l'arbre" << endl;
cout << arbre.altura() << endl;
cout << "> Recorregut en inordre de l'arbre" << endl;
arbre.inordre();
cout << "> Cerca d'un element inexistent" << endl;
try {
arbre.consultar(-1);
} catch (char *m) {
cout << m << endl;
}
cout << "> Cerca d'un element existent" << endl;
cout << arbre.consultar(unnumero) << endl;
cout << "> Mínim i màxim de l'arbre" << endl;
cout << "min = " << arbre.minim() << ", max = " << arbre.maxim() << endl;
cout << "> Buidat de l'arbre" << endl;
while(arbre.elements() > 0) {
cout << ">> Treiem el..." << arbre.minim() << endl;
arbre.esborrar(arbre.minim());
}
cout << "> Recorregut en inordre de l'arbre després del buidat" << endl;
arbre.inordre();
cout << "> Fi de la demostració" << endl;
#ifdef PERFPROFILE
HeapProfilerDump("**Heap al final**");
HeapProfilerStop();
#endif
return 0;
}