/* * ===================================================================================== * * Filename: codigotarea.cpp * * Description: editando * * Version: 1.0 * Created: 11/03/2013 05:27:49 PM * Revision: none * Compiler: gcc * * Author: YOUR NAME (), * Organization: * * ===================================================================================== */ // Código para el tercer examen parcial de Estructuras de Datos, semestre AD12, elaborado por RMM #include #include #include #include #include using namespace std; #define TAM 100 //Implementación del nivel físico de un ABB template class NodoArbol { public: T info; NodoArbol *izq, *der; NodoArbol() { izq = der = NULL; } NodoArbol(T dato) { info = dato; izq = der = NULL; } }; template class ABB { private: NodoArbol *raiz; public: ABB() { raiz = NULL; } void inserta (T dato); void despliega() { inorden(raiz); } int cuenta () { return contar(raiz); } int altura () { return niveles(raiz); } bool existe(T dato); ~ABB() { libera(raiz); } }; template void libera (NodoArbol* raiz) { if (raiz != NULL) { libera(raiz->izq); libera(raiz->der); delete(raiz); } } template void inorden (NodoArbol* raiz) { if (raiz != NULL) { inorden(raiz->izq); cout << raiz->info <<" "; inorden(raiz->der); } } template int contar (NodoArbol* raiz) { if (raiz == NULL) return 0; return(1+contar(raiz->izq)+contar(raiz->der)); } template int niveles (NodoArbol* raiz) { int altizq, altder; if (raiz == NULL) return 0; altizq = niveles(raiz->izq); altder = niveles(raiz->der); return(1+ (altizq>altder? altizq : altder)); } template void ABB::inserta (T valor) { NodoArbol *NuevoNodo = new NodoArbol(valor); NodoArbol *actual = raiz, *anterior = NULL; while (actual != NULL) { anterior=actual; actual=(actual->info>valor? actual->izq: actual->der); } if (anterior==NULL) raiz = NuevoNodo; else if (anterior->info > valor) anterior->izq = NuevoNodo; else anterior->der = NuevoNodo; } template bool ABB::existe(T dato) { NodoArbol *aux = raiz; int comparaciones = 1; while (aux != NULL && aux->info != dato){ aux = (dato < aux->info? aux->izq : aux->der); comparaciones++; } if(aux==NULL){ cout< class tablaHash { private: ABB tabla[TAM]; int cantidad; public: tablaHash() {cantidad = 0;} void inserta (T dato); void inserta2 (T dato); bool buscarDato (T dato); bool buscarDato2 (T dato); void despliega (); double tamanioPromedioBuckets (); double alturaPromedioBuckets (); }; template void tablaHash::inserta (T dato) { int posicion = dato / 100 % TAM; tabla[posicion].inserta(dato); cantidad++; } template void tablaHash::inserta2 (T dato) { int posicion = dato % TAM; posicion = pow(posicion,2); posicion%=TAM; cout<<"Se inserta el dato "< void tablaHash::despliega() { cout << "Cantidad de datos: "< double tablaHash::tamanioPromedioBuckets() { int suma = 0, bucketsLlenos = 0; for (int i = 0; i < TAM; i++) { int nodosBucket = tabla[i].cuenta(); if (nodosBucket!=0) { suma += nodosBucket; bucketsLlenos++; } } return ((suma*1.0)/bucketsLlenos); } template double tablaHash::alturaPromedioBuckets() { int suma = 0, bucketsLlenos = 0; for (int i = 0; i < TAM; i++) { int alturaBucket = tabla[i].altura(); if (alturaBucket!=0) { suma += alturaBucket; bucketsLlenos++; } } return ((suma*1.0)/bucketsLlenos); } template bool tablaHash::buscarDato (T dato){ int datoTemp = dato; int indice = datoTemp / 100 % TAM; cout<<"Este es el indice donde esta el dato "< bool tablaHash::buscarDato2 (T dato){ int datoTemp = dato; int indice = datoTemp % TAM; indice = pow(indice,2); indice%=TAM; cout<<"Este es el indice donde esta el dato "< hashing; tablaHash hashing2; ifstream arch; string nomarch; int dato; cout << "Ingrese el nombre del archivo a cargar: "; cin >> nomarch; arch.open(nomarch.c_str()); while (!arch.eof()) { arch >> dato; hashing.inserta(dato); hashing2.inserta2(dato); } arch.close(); cout<<"HASH TABLE 1"<