118 lines
2.8 KiB
C++
118 lines
2.8 KiB
C++
#include "arbol.hpp"
|
|
|
|
// falta el date y el last
|
|
|
|
Arbol::Arbol() {
|
|
raiz = new Nodo;
|
|
}
|
|
|
|
Arbol::~Arbol() {
|
|
delete raiz;
|
|
}
|
|
|
|
void Arbol::insertar(Cuac* ref){
|
|
Nodo* actual = raiz;
|
|
Fecha f = ref -> get_fecha();
|
|
string conv = f.conv(); // conv devuelve la fecha como un string en formato AAAA/MM/DD HH:MM:SS
|
|
|
|
|
|
for (size_t i = 0; i < conv.length(); i++) {
|
|
char digito = conv[i];
|
|
if (actual -> consulta(digito) == nullptr) {
|
|
actual -> inserta(digito);
|
|
}
|
|
actual = actual -> consulta(digito);
|
|
|
|
}
|
|
actual -> PonerMarca();
|
|
actual -> PonerEnLista(ref);
|
|
}
|
|
|
|
void Arbol::last(int N) {
|
|
if (raiz != nullptr) {
|
|
int contador = 0;
|
|
last_rec(raiz, N, contador);
|
|
cout << "Total: " << contador << " cuac" << endl;
|
|
}
|
|
|
|
}
|
|
|
|
void Arbol::last_rec(Nodo* nodo, int n, int &contador) {
|
|
if (nodo == nullptr || contador >= n) return;
|
|
last_rec(nodo -> getSig(), n, contador);
|
|
if (contador >= n) return;
|
|
list<Cuac*> l = nodo -> getLista();
|
|
if (!l.empty()) {
|
|
list<Cuac*>::iterator it;
|
|
for (it = l.begin(); it != l.end() && contador < n; it++) {
|
|
cout << contador + 1 << ". ";
|
|
(*it) -> escribir();
|
|
contador++;
|
|
}
|
|
}
|
|
last_rec(nodo -> getPtr(), n, contador);
|
|
|
|
}
|
|
|
|
|
|
void Arbol::date(Fecha f1, Fecha f2) {
|
|
int contador = 0;
|
|
string fecha1 = f1.conv();
|
|
string fecha2 = f2.conv();
|
|
date_rec(raiz, fecha1, fecha2, "", contador);
|
|
cout << "Total: " << contador << " cuac" << endl;
|
|
}
|
|
|
|
void Arbol::date_rec(Nodo* nodo, string f1, string f2, string actual, int& contador) {
|
|
/* if (nodo == nullptr) return;
|
|
if (nodo -> getCar() == ' ') {
|
|
date_rec(nodo -> getSig(), f1, f2, actual, contador);
|
|
return;
|
|
}
|
|
|
|
while (nodo != nullptr) {
|
|
|
|
if (nodo -> HayMarca()) {
|
|
list<Cuac*> lista = nodo -> getLista();
|
|
list<Cuac*>::iterator it;
|
|
|
|
for (it = lista.begin(); it != lista.end(); it++) {
|
|
contador++;
|
|
cout << contador << ". ";
|
|
(*it) -> escribir();
|
|
}
|
|
}
|
|
string nueva = actual + nodo -> getCar();
|
|
int nueva_len = nueva.length();
|
|
if (nueva >= f1.substr(0, nueva_len) && nueva <= f2.substr(0, nueva_len)) {
|
|
date_rec(nodo -> getPtr(), f1, f2, nueva, contador);
|
|
}
|
|
nodo = nodo -> getSig();
|
|
} */
|
|
|
|
if (nodo == nullptr) return;
|
|
// aplicamos la técnica de last_rec() para ir de derecha a izquierda
|
|
date_rec(nodo -> getSig(), f1, f2, actual, contador); // realmente se puede devolver el contador
|
|
if (nodo -> getCar() == ' ') {
|
|
date_rec(nodo -> getPtr(), f1, f2, actual, contador);
|
|
return;
|
|
}
|
|
|
|
if (nodo -> HayMarca()) {
|
|
list<Cuac*> lista = nodo -> getLista();
|
|
list<Cuac*>::iterator it;
|
|
|
|
for (it = lista.begin(); it != lista.end(); it++) {
|
|
contador++;
|
|
cout << contador << ". ";
|
|
(*it) -> escribir();
|
|
}
|
|
}
|
|
string nueva = actual + nodo -> getCar();
|
|
int nueva_len = nueva.length();
|
|
if (nueva >= f1.substr(0, nueva_len) && nueva <= f2.substr(0, nueva_len)) {
|
|
date_rec(nodo -> getPtr(), f1, f2, nueva, contador);
|
|
}
|
|
|
|
}
|