├── Arboles ├── EjerciciosDeArboles │ ├── .~lock.Ejercicios_con_arboles.doc# │ ├── Ejercicios_con_arboles.doc │ ├── EliminarNodo.cpp │ ├── EspejoArbol.cpp │ └── PlantillaArbolAVL.cpp ├── OwnImplementation │ ├── main.cpp │ └── treeavl.h ├── SimpleTree │ ├── avl.h │ ├── avltest.cpp │ └── compiling_error.txt ├── arboles │ ├── arbol.h │ └── arbolBTest.cc ├── arbolesAVL │ ├── a.out │ ├── arbolBTest.cc │ ├── avl.h │ ├── compiling_error.txt │ └── indexandoando.cc ├── arbolesFacil │ ├── arbol.h │ └── arbolBTest.cc ├── avl.h └── deleteNode │ ├── Source.cpp │ ├── avl.h │ └── compiling_error.txt ├── ContestFinal └── HRECURS - Hello Recursion.cpp ├── Privado pre final └── desdecero.cpp ├── README.md ├── Unidad1 ├── Crecimiento de funciones │ ├── 010104-ejercicios_crecimiento_de_funciones.pdf │ ├── cf_13.cpp │ ├── cf_14.cpp │ ├── cf_15.cpp │ ├── cf_16.cpp │ └── cf_17.cpp ├── MOCKPC1 │ ├── MOCKPC1 │ ├── MOCKPC1.cpp │ ├── MOCKPC1.o │ ├── datos.txt │ └── libros.txt ├── Semana 1 │ ├── Generalizacion de tipos │ │ ├── 010103-ejercicios_generalizacion.pdf │ │ └── f_1.cpp │ └── Tipos de datos abstractos(TDA) │ │ ├── 010102-ejercicios_tda.pdf │ │ ├── 1_FECHA.cpp │ │ └── e2.cpp └── Semana 2 │ ├── Cadenas │ ├── 010203-ejercicios_cadenas.pdf │ ├── 10c.cpp │ ├── 11c.cpp │ ├── 12c.cpp │ ├── 14c.cpp │ ├── 1c.cpp │ ├── 3c.cpp │ └── 8c.cpp │ └── Registro de objetos │ ├── 010204-ejercicios_registros_objetos.pdf │ ├── main │ ├── main.cpp │ ├── main.o │ └── point.h ├── Unidad2 ├── EjerciciosPPT │ ├── 03 - Estructuras de datos base.pptx │ ├── ListaCircularDoblementeEnlazada.cpp │ ├── ejercicio1_listasimple.cpp │ ├── ejercicio4_listacircularsimple.cpp │ ├── l │ └── lcd └── TParcialFinalito │ ├── Apellidos.txt │ ├── Hash.cpp │ ├── Hash.h │ ├── Listas.h │ ├── Nombres.txt │ ├── m.exe │ ├── main.cpp │ ├── output.txt │ ├── salida.js │ ├── script.js │ ├── slides.txt.txt │ ├── stats.html │ └── style.css ├── Unidad3 ├── Ordenamiento │ ├── a.out │ ├── p │ ├── p1 │ ├── p10327.cpp │ └── p110.cpp ├── Recursividad │ ├── e1 │ ├── e2 │ ├── ejercicio1.cpp │ ├── ejercicio2 │ ├── ejercicio2.cpp │ ├── ejercicio2.o │ └── ejercicio3.cpp └── parcial pasado │ ├── Apellidos.txt │ ├── Nombres.txt │ ├── Productos.txt │ ├── ep_pasado.cpp │ ├── input.txt │ └── vip.txt ├── _config.yml └── holakase.js /Arboles/EjerciciosDeArboles/.~lock.Ejercicios_con_arboles.doc#: -------------------------------------------------------------------------------- 1 | ,enzoftware,enzoftware,10.07.2017 01:28,file:///home/enzoftware/.config/libreoffice/4; -------------------------------------------------------------------------------- /Arboles/EjerciciosDeArboles/EliminarNodo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class AVLTree { 8 | struct Node { 9 | int e; 10 | Node *l, *r; 11 | int h; 12 | Node(int e = 0, 13 | Node* l = NULL, 14 | Node* r = NULL) 15 | : e(e), l(l), r(r) { 16 | int hl = l == NULL? -1 : l->h; 17 | int hr = r == NULL? -1 : r->h; 18 | this->h = (hl > hr? hl : hr) + 1; 19 | } 20 | }; 21 | int h(Node* n) { 22 | return n == NULL? -1 : n->h; 23 | } 24 | void fixH(Node* n) { 25 | n->h = (h(n->l) > h(n->r)? h(n->l) : h(n->r)) + 1; 26 | } 27 | void deleteAll(Node* n) { 28 | if (n != NULL) { 29 | deleteAll(n->l); 30 | deleteAll(n->r); 31 | delete n; 32 | } 33 | } 34 | void enOrden(Node* n) { 35 | if (n != NULL) { 36 | enOrden(n->l); 37 | proc(n->e); 38 | enOrden(n->r); 39 | } 40 | } 41 | void rotarIzq(Node*& x) { 42 | Node* y = x->r; 43 | x->r = y->l; 44 | y->l = x; 45 | x = y; 46 | fixH(x->l); 47 | fixH(x); 48 | } 49 | void rotarDer(Node*& y) { 50 | Node* x = y->l; 51 | y->l = x->r; 52 | x->r = y; 53 | y = x; 54 | fixH(y->r); 55 | fixH(y); 56 | } 57 | void balancear(Node*& n) { 58 | if (h(n->l) - h(n->r) > 1) { 59 | if (h(n->l->r) > h(n->l->l)) { 60 | rotarIzq(n->l); 61 | } 62 | rotarDer(n); 63 | } else if (h(n->r) - h(n->l) > 1) { 64 | if (h(n->r->l) > h(n->r->r)) { 65 | rotarDer(n->r); 66 | } 67 | rotarIzq(n); 68 | } 69 | } 70 | bool insertar(int e, Node*& n) { 71 | bool flag = false; 72 | if (n == NULL) { 73 | n = new Node(e); 74 | ++len; 75 | return n != NULL; 76 | } else if (cmp(e, n->e) < 0) { 77 | flag = insertar(e, n->l); 78 | } else { 79 | flag = insertar(e, n->r); 80 | } 81 | if (flag) { 82 | balancear(n); 83 | fixH(n); 84 | } 85 | return flag; 86 | } 87 | 88 | Node* unirNodo(Node* izq , Node* der){ 89 | if(izq == NULL) return der; 90 | if(der == NULL) return izq; 91 | 92 | Node* centr = unirNodo(izq->r, der->l); 93 | izq->r = centr; 94 | der->l = izq; 95 | return der; 96 | } 97 | 98 | void Eliminar(Node *& nodo, int x){ 99 | if(nodo == NULL) return; 100 | 101 | if(x < nodo->e) Eliminar(nodo->l,x); 102 | else if(x > nodo->e) Eliminar(nodo->r,x); 103 | else{ 104 | Node* p = nodo; 105 | nodo = unirNodo(nodo->l,nodo->r); 106 | delete p; 107 | } 108 | } 109 | 110 | public: 111 | AVLTree(void (*proc)(int)): root(NULL), len(0){ 112 | this->proc = proc; 113 | } 114 | ~AVLTree(){ 115 | deleteAll(root); 116 | } 117 | void enOrden() { 118 | enOrden(root); 119 | } 120 | bool insertar(int e){ 121 | return insertar(e, root); 122 | } 123 | int size() { 124 | return len; 125 | } 126 | int height() { 127 | return h(root); 128 | } 129 | 130 | void deleteThis(int x){ 131 | Eliminar(root,x); 132 | } 133 | 134 | 135 | 136 | private: 137 | Node *root; 138 | int len; 139 | void (*proc)(int); 140 | int cmp(int a , int b){ 141 | return a -b ; 142 | } 143 | }; 144 | 145 | 146 | void impri(int e){ cout<<" "<insertar(1); 153 | for (int i = 0; i < 10; ++i) { 154 | long long val = rand() % 1000; 155 | t->insertar(val); 156 | } 157 | t->insertar(1001); 158 | t->enOrden(); 159 | //cout << "\nNumero de elementos:" << t->size() << endl; 160 | //cout << "Altura:" << t->height() << endl; 161 | cout<deleteThis(1); 163 | t->deleteThis(1001); 164 | t->enOrden(); 165 | 166 | return 0; 167 | return 0; 168 | } 169 | -------------------------------------------------------------------------------- /Arboles/EjerciciosDeArboles/EspejoArbol.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | class AVLTree { 8 | struct Node { 9 | int e; 10 | Node *l, *r; 11 | int h; 12 | Node(int e = 0, 13 | Node* l = NULL, 14 | Node* r = NULL) 15 | : e(e), l(l), r(r) { 16 | int hl = l == NULL? -1 : l->h; 17 | int hr = r == NULL? -1 : r->h; 18 | this->h = (hl > hr? hl : hr) + 1; 19 | } 20 | }; 21 | int h(Node* n) { 22 | return n == NULL? -1 : n->h; 23 | } 24 | void fixH(Node* n) { 25 | n->h = (h(n->l) > h(n->r)? h(n->l) : h(n->r)) + 1; 26 | } 27 | void deleteAll(Node* n) { 28 | if (n != NULL) { 29 | deleteAll(n->l); 30 | deleteAll(n->r); 31 | delete n; 32 | } 33 | } 34 | void enOrden(Node* n) { 35 | if (n != NULL) { 36 | enOrden(n->l); 37 | proc(n->e); 38 | enOrden(n->r); 39 | } 40 | } 41 | void rotarIzq(Node*& x) { 42 | Node* y = x->r; 43 | x->r = y->l; 44 | y->l = x; 45 | x = y; 46 | fixH(x->l); 47 | fixH(x); 48 | } 49 | void rotarDer(Node*& y) { 50 | Node* x = y->l; 51 | y->l = x->r; 52 | x->r = y; 53 | y = x; 54 | fixH(y->r); 55 | fixH(y); 56 | } 57 | void balancear(Node*& n) { 58 | if (h(n->l) - h(n->r) > 1) { 59 | if (h(n->l->r) > h(n->l->l)) { 60 | rotarIzq(n->l); 61 | } 62 | rotarDer(n); 63 | } else if (h(n->r) - h(n->l) > 1) { 64 | if (h(n->r->l) > h(n->r->r)) { 65 | rotarDer(n->r); 66 | } 67 | rotarIzq(n); 68 | } 69 | } 70 | bool insertar(int e, Node*& n) { 71 | bool flag = false; 72 | if (n == NULL) { 73 | n = new Node(e); 74 | ++len; 75 | return n != NULL; 76 | } else if (cmp(e, n->e) < 0) { 77 | flag = insertar(e, n->l); 78 | } else { 79 | flag = insertar(e, n->r); 80 | } 81 | if (flag) { 82 | balancear(n); 83 | fixH(n); 84 | } 85 | return flag; 86 | } 87 | 88 | Node* espejoDelArbol(Node* nodo){ 89 | Node* temporal; 90 | if(nodo != NULL){ 91 | temporal = nodo->l; 92 | nodo->l = espejoDelArbol(nodo->r); 93 | nodo->r = espejoDelArbol(temporal); 94 | } 95 | return nodo; 96 | } 97 | 98 | public: 99 | AVLTree(void (*proc)(int)): root(NULL), len(0){ 100 | this->proc = proc; 101 | } 102 | ~AVLTree(){ 103 | deleteAll(root); 104 | } 105 | void enOrden() { 106 | enOrden(root); 107 | } 108 | bool insertar(int e){ 109 | return insertar(e, root); 110 | } 111 | int size() { 112 | return len; 113 | } 114 | int height() { 115 | return h(root); 116 | } 117 | 118 | Node* mirrorTree(){ 119 | return espejoDelArbol(root); 120 | } 121 | 122 | 123 | private: 124 | Node *root; 125 | int len; 126 | void (*proc)(int); 127 | int cmp(int a , int b){ 128 | return a -b ; 129 | } 130 | }; 131 | 132 | 133 | void impri(int e){ cout<<" "<insertar(val); 142 | } 143 | t->enOrden(); 144 | //cout << "\nNumero de elementos:" << t->size() << endl; 145 | //cout << "Altura:" << t->height() << endl; 146 | 147 | cout<mirrorTree(); 149 | t->enOrden(); 150 | 151 | return 0; 152 | return 0; 153 | } 154 | -------------------------------------------------------------------------------- /Arboles/EjerciciosDeArboles/PlantillaArbolAVL.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | // 5 | //TODO: EN ESTE EJEMPLO TRATARE DE SER LO MAS EXPLICATIVO POSIBLE COMENTANDO EL CODIGO 6 | // 7 | 8 | 9 | class ARBOLITOAVL{ // CREACION DEL ARBOL AVL 10 | struct Nodo{ 11 | Nodo* left; //HIJO IZQUIERDO 12 | Nodo* right; // HIJO DERECHO 13 | int elemento; 14 | int altura; //ALTURA DEL ARBOL 15 | Nodo(int elemento = 0 , Nodo* left = NULL ,Nodo* right = NULL ): elemento(elemento),left(left),right(right){ 16 | //INICIALIZANDO LOS VALORES DEL ARBOL 17 | int hnl = (left == NULL)? -1 : left->altura; //ALTURA DEL HIJO IZQUIERDO 18 | int hnr = (right == NULL) ? -1 : right->altura; //ALTURA DEL HIJO DERECHO 19 | this->altura = ((hnl > hnr) ? hnl : hnr ) +1 ; //OBTIENE LA ALTURA DEL MAYOR Y LE AUMENTA SU VALOR EN 1 20 | } 21 | }; 22 | 23 | int getAltura(Nodo* n){ 24 | return (n == NULL) ? -1 : n->altura; 25 | //OBTENEMOS LA ALTURA DE UN NODO 26 | } 27 | 28 | void fixAltura(Nodo *n){ 29 | n->altura = ( ( getAltura(n->left) > getAltura(n->right) ) ? getAltura(n->left) : getAltura(n->right) ) +1; 30 | //ACTUALIZAMOS LA ALTURA DE UN NODO , COMO EN SU INICIALIZACION 31 | } 32 | 33 | void imprimirEnOrden(Nodo*n) { 34 | if(n != NULL) { 35 | imprimirEnOrden(n->left); 36 | imprimir(n->elemento,n->altura); 37 | imprimirEnOrden(n->right); 38 | } 39 | 40 | //FUNCION RECURSIVA QUE PERMITE IMPRIMIR DE MANERA ORDENADA EL ARBOL PORQUE 41 | //VA PRIMERO AL NODO QUE ESTA MAS A LA IZQUIERDA DE CADA NODO 42 | // Y SE VA MOVIENDO A LA DERECHA EN CADA RECORRIDO 43 | 44 | } 45 | 46 | void RotarIzquierda(Nodo*& x){ 47 | Nodo*y = x->right; 48 | x->right = y->left; 49 | y->left = x; 50 | x = y; 51 | fixAltura(x->left); 52 | fixAltura(x); 53 | 54 | //ROTA A LA IZQUIERDA UN NODO 55 | 56 | } 57 | 58 | void RotarDerecha(Nodo*& y){ 59 | Nodo*x = y->left; 60 | y->left = x->right; 61 | x->right = y; 62 | y = x; 63 | fixAltura(y->right); 64 | fixAltura(y); 65 | 66 | //ROTA A LA DERECHA UN NODO 67 | 68 | } 69 | 70 | void BalancearArbol(Nodo*& nodo){ 71 | if(getAltura(nodo->left) - getAltura(nodo->right) > 1){ // PESADO A LA IZQUIERDA 72 | if(getAltura(nodo->left->right) > getAltura(nodo->left->left)){ //EN FORMA DE ZIGZAG 73 | RotarIzquierda(nodo->left); // COMO ESTA EN ZIGZAG SE ROTA A LA DERECHA EL SUBNODO 74 | } 75 | RotarDerecha(nodo); 76 | }else if(getAltura(nodo->right) - getAltura(nodo->left) > 1){ //PESADO A LA DERECHA 77 | if(getAltura(nodo->right->left) > getAltura(nodo->right->right)){ //ZIGZAG 78 | RotarDerecha(nodo->right); // COMO ESTA EN ZIGZAG SE ROTA A LA IZQUIERDA EL SUBNODO 79 | } 80 | RotarIzquierda(nodo); 81 | } 82 | } 83 | 84 | bool insertarNodo(int e , Nodo*& nodo){ 85 | bool flag = false; //INICIAMOS EN FALSO PARA VERIFICAR SI SE HIZO LA INSERCION DE UN NUEVO NODO 86 | if(nodo == NULL){ 87 | nodo = new Nodo(e); //SE CREA UN NUEVO NODO SI ESTA VACIA LA POSICION 88 | numNodos++; // EL NUMERO DE NODOS AUMENTA 89 | return nodo!= NULL; // RETORNARA VERDADERO 90 | }else if(e < nodo->elemento){ 91 | flag= insertarNodo(e,nodo->left); // SI EL ELEMENTO ES MENOR AL NODO BASE SE VA A LA IZQUIERDA 92 | }else{ 93 | flag= insertarNodo(e,nodo->right); // SI EL ELEMENTO ES MAYOR AL NODO BASE SE VA A LA DERECHA 94 | } 95 | 96 | if (flag){ 97 | BalancearArbol(nodo); // BALANCEA EL ARBOL EN INSERCION , MUCHO MAS OPTIMO 98 | fixAltura(nodo); // ACTUALIZA LA ALTURA DEL NODO DESPUES DE BALANCERLO , YA QUE PUEDE VARIAR 99 | } 100 | return flag; 101 | } 102 | 103 | 104 | public: 105 | ARBOLITOAVL(void (*imprimir) (int,int)):raiz(NULL),numNodos(0){ //INICIANDO EL ARBOL CON UN PUNTERO A FUNCION 106 | //QUE IMPRIMA UN ELEMENTO DEL ARBOL 107 | this->imprimir = imprimir; 108 | } 109 | 110 | 111 | bool InsertarNuevoNodo(int e){ 112 | return insertarNodo(e,raiz); //SE INSERTA UN NUEVO NODO TENIENDO COMO BASE EL NODO RAIZ 113 | } 114 | 115 | void ImprimirOrdenadito(){ 116 | imprimirEnOrden(raiz); // SE IMPRIME LOS NODOS EN ORDEN 117 | } 118 | 119 | int getnumNodos(){ 120 | return numNodos; //OBTIENE EL NUMERO DE NODOS DEL ARBOL 121 | } 122 | 123 | int alturaArbol(){ 124 | return getAltura(raiz); // ALTURA DEL ARBOL 125 | } 126 | 127 | 128 | 129 | private: 130 | Nodo* raiz; //LA RAIZ DEL ARBOL 131 | int numNodos; //NUMERO NODOS DEL ARBOL 132 | void (*imprimir)(int,int); //PUNTERO A FUNCION QUE OBTIENE EL VALOR DEL ARBOL 133 | int comparar(int a , int b){ 134 | return a-b; // COMPARA DOS VALORES DEL ARBOL 135 | } 136 | }; 137 | 138 | void printTree(int e , int h){ 139 | cout<InsertarNuevoNodo(5); 145 | ArVoL->InsertarNuevoNodo(3); 146 | ArVoL->InsertarNuevoNodo(7); 147 | ArVoL->InsertarNuevoNodo(77); 148 | ArVoL->ImprimirOrdenadito(); 149 | cout<getnumNodos(); 151 | cout<alturaArbol(); 153 | 154 | 155 | /* 156 | 5 157 | 3 7 158 | 77 159 | 160 | altura = 2 161 | numeronodos = 4 162 | */ 163 | return 0; 164 | } 165 | -------------------------------------------------------------------------------- /Arboles/OwnImplementation/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "treeavl.h" 3 | using namespace std; 4 | 5 | void printTree(int e){ 6 | cout<<" "<* btree = new BinaryTree(printTree,compareElements); 15 | btree->insertElement(5); 16 | btree->insertElement(52); 17 | btree->insertElement(1); 18 | btree->insertElement(100); 19 | btree->insertElement(5); 20 | btree->insertElement(5); 21 | btree->insertElement(5); 22 | btree->insertElement(5); 23 | btree->insertElement(50000); 24 | btree->insertElement(10); 25 | btree->OrderTree(); 26 | cout<deleteElement(1); 28 | btree->OrderTree(); 29 | return 0; 30 | } 31 | /* 32 | int hl = _heightTree(node->leftSon); 33 | int hr = _heightTree(node->rightSon); 34 | int d = hr-hl; 35 | if(d > 1){ // heavy to right 36 | int hnl = _heightTree(node->rightSon->leftSon); 37 | int hnr = _heightTree(node->rightSon->rightSon); 38 | if(hnl > hnr){ // zigzag rig to lef 39 | _rotateRight(node->rightSon->leftSon, node->rightSon , node->rightSon); 40 | } 41 | _rotateLeft(node,node->rightSon,node); 42 | } 43 | else if(d < -1){ //heavy to left 44 | int hnl = _heightTree(node->leftSon->leftSon); 45 | int hnr = _heightTree(node->leftSon->rightSon); 46 | if(hnr > hnl){ //zigzag lef to rig 47 | _rotateLeft(node->leftSon,node->leftSon->rightSon, node->leftSon); 48 | } 49 | _rotateRight(node->leftSon,node,node); 50 | } 51 | hl = _heightTree(node->leftSon); 52 | hr = _heightTree(node->rightSon); 53 | node->height =1+ (hl > hr ? hl : hr); 54 | return true; 55 | */ 56 | -------------------------------------------------------------------------------- /Arboles/OwnImplementation/treeavl.h: -------------------------------------------------------------------------------- 1 | #include 2 | template 3 | struct Node{ 4 | T element; 5 | Node* leftSon; 6 | Node* rightSon ; 7 | int height; 8 | Node(){ 9 | height = 0; 10 | rightSon = NULL; 11 | leftSon = NULL; 12 | } 13 | }; 14 | 15 | template 16 | class BinaryTree{ 17 | Node* root; 18 | void (*process)(T); // Templates of fuctions that print and 19 | int (*compare)(T,T);// compare to be easy to implement. 20 | 21 | bool _insert(Node*& node , T e){ 22 | if(node == NULL){ 23 | node = new Node; 24 | node->element = e; 25 | }else if(compare(e, node->element) < 0){ 26 | _insert(node->leftSon , e); 27 | }else{ 28 | _insert(node->rightSon,e); 29 | } 30 | 31 | 32 | } 33 | 34 | int _heightTree(Node* node){ 35 | if(node == NULL) return -1; 36 | return node->height; 37 | } 38 | 39 | T _search(Node* node , T e){ 40 | if(node == NULL) return 0; 41 | if(compare(e,node->element) == 0){ 42 | return node->element; 43 | }else{ 44 | return _search(compare(e,node->element) > 0 ? node->rightSon : node->leftSon , e); 45 | } 46 | } 47 | 48 | void _inOrder(Node* node){ 49 | if(node == NULL) return; 50 | _inOrder(node->leftSon); 51 | process(node->element); 52 | _inOrder(node->rightSon); 53 | } 54 | 55 | //-><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><- 56 | //TODO : Delete a node and balance the tree 57 | //-><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><--><- 58 | 59 | void _rotateLeft(Node*& x ,Node*& y ,Node*& father ){ 60 | father = y; 61 | x->rightSon = y->leftSon; 62 | father->leftSon = x; 63 | } 64 | 65 | void _rotateRight(Node*& x ,Node*& y ,Node*& father){ 66 | father = x; 67 | y->leftSon = x->rightSon; 68 | father->rightSon = y; 69 | } 70 | 71 | 72 | Node* _quitMin(Node*node , Node*& vinc){ 73 | if(node == NULL) return NULL; 74 | if(node->leftSon == NULL){ // Alredy found the min 75 | vinc = node->rightSon; 76 | node->rightSon = NULL; 77 | return node; 78 | }else{ 79 | return _quitMin(node->leftSon,node->leftSon); 80 | } 81 | } 82 | 83 | Node* _quitMax(Node*node , Node*& vinc){ 84 | if(node == NULL) return NULL; 85 | if(node->rightSon == NULL){ // Alredy found the min 86 | vinc = node->leftSon; 87 | node->leftSon = NULL; 88 | return node; 89 | }else{ 90 | return _quitMax(node->rightSon,node->rightSon); 91 | } 92 | } 93 | 94 | bool _deleteThis(T e , Node* node , Node*& vinc){ 95 | if(compare(e,node->element) == 0){ 96 | int hl = _heightTree(node->leftSon); 97 | int hr = _heightTree(node->rightSon); 98 | if(hl == -1 && hr == -1){ 99 | vinc = NULL; 100 | delete node; 101 | return true; 102 | } 103 | else if(hl > hr){ 104 | Node* replace = _quitMax(node->leftSon,node->leftSon); 105 | replace->leftSon = node->leftSon; 106 | replace->rightSon = node->rightSon; 107 | vinc = replace; 108 | delete node; 109 | } 110 | else{ 111 | Node* replace = _quitMin(node->rightSon, node->rightSon); 112 | replace->leftSon = node->leftSon; 113 | replace->rightSon = node->rightSon; 114 | vinc = replace; 115 | delete node; 116 | } 117 | } 118 | else if(compare(e,node->element) > 0 && node->rightSon != NULL){ 119 | return _deleteThis(e,node->rightSon, node->rightSon); 120 | } 121 | else if(compare(e,node->element) > 0 && node->leftSon != NULL){ 122 | return _deleteThis(e,node->leftSon,node->leftSon); 123 | } 124 | else{ 125 | return false; 126 | } 127 | } 128 | 129 | 130 | 131 | public: 132 | BinaryTree(void (*processF)(T),int (*compareF)(T,T) ){ 133 | this->process = processF; 134 | this->compare = compareF; 135 | root = NULL; 136 | } 137 | 138 | bool insertElement(T e){ 139 | return _insert(root,e); 140 | } 141 | 142 | void OrderTree(){ 143 | _inOrder(root); 144 | } 145 | 146 | int getHeight(){ 147 | _heightTree(root); 148 | } 149 | 150 | T searchElement(T e){ 151 | return _search(root,e); 152 | } 153 | 154 | bool deleteElement(T e){ 155 | return _deleteThis(e,root,root); 156 | } 157 | 158 | 159 | }; 160 | -------------------------------------------------------------------------------- /Arboles/SimpleTree/avl.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | template class AVLTree { 4 | struct Node { 5 | T e; 6 | Node *l, *r; 7 | int h; 8 | 9 | Node(T e = iniT, 10 | Node* l = NULL, 11 | Node* r = NULL) 12 | : e(e), l(l), r(r) { 13 | int hl = l == NULL? -1 : l->h; 14 | int hr = r == NULL? -1 : r->h; 15 | this->h = (hl > hr? hl : hr) + 1; 16 | } 17 | }; 18 | int h(Node* n) { 19 | return n == NULL? -1 : n->h; 20 | } 21 | void fixH(Node* n) { 22 | n->h = (h(n->l) > h(n->r)? h(n->l) : h(n->r)) + 1; 23 | } 24 | void deleteAll(Node* n) { 25 | if (n != NULL) { 26 | deleteAll(n->l); 27 | deleteAll(n->r); 28 | delete n; 29 | } 30 | } 31 | void enOrden(Node* n) { 32 | if (n != NULL) { 33 | enOrden(n->l); 34 | proc(n->e); 35 | enOrden(n->r); 36 | } 37 | } 38 | void rotarIzq(Node*& x) { 39 | Node* y = x->r; // 1 40 | x->r = y->l; // 2 41 | y->l = x; // 3 42 | x = y; // 4 43 | fixH(x->l); 44 | fixH(x); 45 | } 46 | void rotarDer(Node*& y) { 47 | Node* x = y->l; // 1 48 | y->l = x->r; // 2 49 | x->r = y; // 3 50 | y = x; // 4 51 | fixH(y->r); 52 | fixH(y); 53 | } 54 | void balancear(Node*& n) { 55 | if (h(n->l) - h(n->r) > 1) { 56 | if (h(n->l->r) > h(n->l->l)) { 57 | rotarIzq(n->l); 58 | } 59 | rotarDer(n); 60 | } else if (h(n->r) - h(n->l) > 1) { 61 | if (h(n->r->l) > h(n->r->r)) { 62 | rotarDer(n->r); 63 | } 64 | rotarIzq(n); 65 | } 66 | } 67 | bool insertar(T e, Node*& n) { 68 | bool flag = false; 69 | if (n == NULL) { 70 | n = new Node(e); 71 | ++len; 72 | return n != NULL; 73 | } else if (cmp(e, n->e) < 0) { 74 | flag = insertar(e, n->l); 75 | } else { 76 | flag = insertar(e, n->r); 77 | } 78 | if (flag) { 79 | balancear(n); 80 | fixH(n); 81 | } 82 | return flag; 83 | } 84 | public: 85 | AVLTree(std::function proc, std::function cmp) 86 | : root(NULL), len(0), proc(proc), cmp(cmp) {} 87 | ~AVLTree() { deleteAll(root); } 88 | void enOrden() { enOrden(root); } 89 | bool insertar(T e) { return insertar(e, root); } 90 | int size() { return len; } 91 | int height() { return h(root); } 92 | private: 93 | Node *root; 94 | int len; 95 | std::function proc; 96 | std::function cmp; 97 | }; 98 | -------------------------------------------------------------------------------- /Arboles/SimpleTree/avltest.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "avl.h" 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | AVLTree* t = new AVLTree( 9 | [](long long a) { cout << a << " "; }, 10 | [](long long a, int b) { return a - b; } 11 | ); 12 | srand(2017); 13 | for (int i = 0; i < 10000000; ++i) { 14 | long long val = rand() % 10000000; 15 | t->insertar(val); 16 | } 17 | // t->enOrden(); 18 | cout << "\nNumero de elementos:" << t->size() << endl; 19 | cout << "Altura:" << t->height() << endl; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Arboles/SimpleTree/compiling_error.txt: -------------------------------------------------------------------------------- 1 | In file included from /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:3:0: 2 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:85:26: error: expected ‘)’ before ‘<’ token 3 | AVLTree(std::function proc, std::function cmp) 4 | ^ 5 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:95:10: error: ‘function’ in namespace ‘std’ does not name a template type 6 | std::function proc; 7 | ^ 8 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:96:10: error: ‘function’ in namespace ‘std’ does not name a template type 9 | std::function cmp; 10 | ^ 11 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp: In function ‘int main()’: 12 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:9:45: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 13 | [](long long a) { cout << a << " "; }, 14 | ^ 15 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:10:48: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 16 | [](long long a, int b) { return a - b; } 17 | ^ 18 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:11:5: error: new initializer expression list treated as compound expression [-fpermissive] 19 | ); 20 | ^ 21 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:11:5: error: no matching function for call to ‘AVLTree::AVLTree(main()::)’ 22 | In file included from /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:3:0: 23 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:3:39: note: candidate: AVLTree::AVLTree() 24 | template class AVLTree { 25 | ^ 26 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:3:39: note: candidate expects 0 arguments, 1 provided 27 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:3:39: note: candidate: AVLTree::AVLTree(const AVLTree&) 28 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:3:39: note: no known conversion for argument 1 from ‘main()::’ to ‘const AVLTree&’ 29 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h: In instantiation of ‘bool AVLTree::insertar(T, AVLTree::Node*&) [with T = long long int; T iniT = 0ll]’: 30 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:89:49: required from ‘bool AVLTree::insertar(T) [with T = long long int; T iniT = 0ll]’ 31 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avltest.cpp:15:24: required from here 32 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/avl.h:73:23: error: ‘cmp’ was not declared in this scope 33 | } else if (cmp(e, n->e) < 0) { 34 | ^ 35 | -------------------------------------------------------------------------------- /Arboles/arboles/arbol.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | struct Nodo { 7 | T elemento; 8 | Nodo* izq; 9 | Nodo* der; 10 | }; 11 | 12 | template 13 | class ArbolB { 14 | Nodo* raiz; 15 | void (*procesar)(T); 16 | private: 17 | bool _insertar(Nodo*& nodo, T e) { 18 | if (nodo == NULL) { 19 | nodo = new Nodo(); 20 | nodo->elemento = e; 21 | } else if (e < nodo->elemento) { 22 | _insertar(nodo->izq, e); 23 | } else if (e >= nodo->elemento) { 24 | _insertar(nodo->der, e); 25 | } 26 | } 27 | 28 | void _eliminar(Nodo*& nodo , T e){ 29 | if(nodo->elemento != e) { 30 | if(e < nodo->elemento) _eliminar(nodo->izq, e); 31 | else _eliminar(nodo->der ,e); 32 | }else{ 33 | if(nodo->izq == NULL && nodo->der == NULL){ // Eliminando hoja 34 | cout<<"Hola"; 35 | } 36 | } 37 | 38 | } 39 | 40 | void _enOrden(Nodo* nodo) { 41 | if (nodo == NULL) return; 42 | _enOrden(nodo->izq); 43 | procesar(nodo->elemento); 44 | _enOrden(nodo->der); 45 | } 46 | public: 47 | ArbolB(void (*otroPunteroAFuncion)(T)) { 48 | this->procesar = otroPunteroAFuncion; 49 | raiz = NULL; 50 | } 51 | bool insertar(T e) { 52 | return _insertar(raiz, e); 53 | } 54 | void enOrden() { 55 | _enOrden(raiz); 56 | } 57 | 58 | void eliminar(T e){ 59 | _eliminar(raiz,e); 60 | } 61 | 62 | }; 63 | -------------------------------------------------------------------------------- /Arboles/arboles/arbolBTest.cc: -------------------------------------------------------------------------------- 1 | #include "arbol.h" 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void imprimir(int e) { 9 | cout << " " << e; 10 | } 11 | int main() { 12 | srand(time(0)); 13 | ArbolB* arbol = new ArbolB(imprimir); 14 | arbol->insertar(5); 15 | arbol->insertar(1); 16 | arbol->insertar(52); 17 | arbol->insertar(2); 18 | arbol->insertar(511); 19 | arbol->insertar(53); 20 | arbol->eliminar(2); 21 | arbol->enOrden(); 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Arboles/arbolesAVL/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Arboles/arbolesAVL/a.out -------------------------------------------------------------------------------- /Arboles/arbolesAVL/arbolBTest.cc: -------------------------------------------------------------------------------- 1 | #include "avl.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #define MAX 1000000 9 | 10 | using namespace std; 11 | 12 | void imprimir(int e) { 13 | cout << " " << e; 14 | } 15 | 16 | int buscar(vector v, int e) { 17 | for (int i = 0; i < v.size(); ++i) { 18 | if (v[i] == e) { 19 | return v[i]; 20 | } 21 | } 22 | } 23 | 24 | typedef function LambdaProc; 25 | typedef function LambdaComp; 26 | typedef ArbolAVL Arbolito; 27 | 28 | int main() { 29 | srand(time(0)); 30 | auto impr = [](int) { }; 31 | auto comparar = [](int a, int b) { return a - b;}; 32 | // ArbolAVL* arbol = new ArbolAVL(imprimir, comparar); // la vieja escuela 33 | Arbolito arbol(impr, comparar); // la nueva escuela 34 | 35 | vector arreglito(MAX); 36 | 37 | for (int i = 0; i < MAX; ++i) { 38 | arbol.insertar(i + 1); 39 | arreglito[i] = i + 1; 40 | //arbol->insertar(rand() % 100); 41 | } 42 | // arbol->enOrden(); 43 | long start = clock(); 44 | cout << buscar(arreglito, MAX) << endl; 45 | cout << "Tiempo busqueda lineal en arreglo: " << (clock() - start) << endl; 46 | start = clock(); 47 | cout << arbol.buscar(MAX, comparar) << endl; 48 | cout << "Tiempo busqueda binaria en AVLTree: " << (clock() - start) << endl; 49 | 50 | return 0; 51 | } -------------------------------------------------------------------------------- /Arboles/arbolesAVL/avl.h: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | template 5 | struct Nodo { 6 | T elemento; // payload 7 | Nodo* izq; 8 | Nodo* der; 9 | int h; // altura: numero de ramas desde el nodo hasta la hoja más lejana 10 | public: 11 | Nodo() { 12 | h = 0; 13 | izq = NULL; 14 | der = NULL; 15 | } 16 | }; 17 | 18 | template // P para funcion procesar y C para funcion comparar 19 | class ArbolAVL { 20 | Nodo* raiz; 21 | //void(*procesar)(T); //Puntero a funcion 22 | //function comparar; // lambda 23 | P procesar; // templates de punteros a funciones o lambdas 24 | C comparar; // templates de punteros a funciones o lambdas 25 | private: 26 | void _rotarIzq(Nodo* x, Nodo*& y, Nodo*& p) { // p es el padre de x 27 | p = y; 28 | x->der = y->izq; 29 | p->izq = x; 30 | } 31 | void _rotarDer(Nodo*& x, Nodo* y, Nodo*& p) { // p es el padre de y 32 | p = x; 33 | y->izq = x->der; 34 | p->der = y; 35 | } 36 | 37 | bool _insertar(Nodo*& nodo, T e) { 38 | if (nodo == NULL) { 39 | nodo = new Nodo(); // h = 0 40 | nodo->elemento = e; 41 | } 42 | // else if (e < nodo->elemento) { 43 | else if (comparar(e, nodo->elemento) < 0) { // con lambda 44 | _insertar(nodo->izq, e); 45 | } 46 | else /* if (comparar(e, nodo->elemento) > 0) */ { // descomentar para no aceptar duplicados 47 | _insertar(nodo->der, e); 48 | } 49 | int hi = _altura(nodo->izq); 50 | int hd = _altura(nodo->der); 51 | int d = hd - hi; 52 | if (d > 1) { // pesado a la derecha 53 | int hni = _altura(nodo->der->izq); 54 | int hnd = _altura(nodo->der->der); 55 | if (hni > hnd) { // zig zag derecha izquierda 56 | _rotarDer(nodo->der->izq, nodo->der, nodo->der); 57 | } 58 | _rotarIzq(nodo, nodo->der, nodo); 59 | } 60 | else if (d < -1) { // pesado a la izquierda 61 | int hni = _altura(nodo->izq->izq); 62 | int hnd = _altura(nodo->izq->der); 63 | if (hnd > hni) { // zig zag izquierda derecha 64 | _rotarIzq(nodo->izq, nodo->izq->der, nodo->izq); 65 | } 66 | _rotarDer(nodo->izq, nodo, nodo); 67 | } 68 | hi = _altura(nodo->izq); 69 | hd = _altura(nodo->der); 70 | nodo->h = 1 + (hi > hd ? hi : hd); 71 | return true; 72 | } 73 | 74 | 75 | void _enOrden(Nodo* nodo) { 76 | if (nodo == NULL) return; 77 | _enOrden(nodo->izq); 78 | procesar(nodo->elemento); 79 | _enOrden(nodo->der); 80 | } 81 | int _altura(Nodo* nodo) { 82 | if (nodo == NULL) return -1; 83 | /*int hi = _altura(nodo->izq); 84 | int hd = _altura(nodo->der); 85 | return 1 + (hi > hd? hi: hd);*/ // implementacion optima en espacio 86 | return nodo->h; // implementacion optima en tiempo 87 | } 88 | // T _buscar(Nodo* nodo, T e, function comparar) { // con lambda 89 | T _buscar(Nodo* nodo, T e, C comparar) { // template funcional 90 | if (nodo == NULL) return 0; 91 | if (comparar(e, nodo->elemento) == 0) { 92 | return nodo->elemento; 93 | } else { 94 | return _buscar( 95 | comparar(e, nodo->elemento) > 0? 96 | nodo->der : 97 | nodo->izq, 98 | e,comparar); 99 | } 100 | } 101 | public: 102 | // ArbolAVL(void(*funcion1)(T), function funcion2) { // con puntero a funcion y lambda 103 | ArbolAVL(P funcion1, C funcion2) { // con template funcional 104 | this->procesar = funcion1; 105 | this->comparar = funcion2; 106 | raiz = NULL; 107 | } 108 | bool insertar(T e) { 109 | return _insertar(raiz, e); 110 | } 111 | void enOrden() { 112 | _enOrden(raiz); 113 | } 114 | int altura() { 115 | _altura(raiz); 116 | } 117 | // T buscar(T e, function comparar) { // con lambda 118 | T buscar(T e, C comparar) { // con template funcional 119 | return _buscar(raiz, e, comparar); 120 | } 121 | }; 122 | -------------------------------------------------------------------------------- /Arboles/arbolesAVL/compiling_error.txt: -------------------------------------------------------------------------------- 1 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:28:9: error: ‘function’ does not name a type 2 | typedef function LambdaProc; 3 | ^ 4 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:29:9: error: ‘function’ does not name a type 5 | typedef function LambdaComp; 6 | ^ 7 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:30:28: error: ‘LambdaProc’ was not declared in this scope 8 | typedef ArbolAVL Arbolito; 9 | ^ 10 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:30:40: error: ‘LambdaComp’ was not declared in this scope 11 | typedef ArbolAVL Arbolito; 12 | ^ 13 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:30:50: error: template argument 2 is invalid 14 | typedef ArbolAVL Arbolito; 15 | ^ 16 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:30:50: error: template argument 3 is invalid 17 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc: In lambda function: 18 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:77: error: request for member ‘insertar’ in ‘*(Arbolito*)arbol’, which is of non-class type ‘Arbolito {aka int}’ 19 | for_each(canciones.begin(), canciones.end(), [arbol](Cancion* c) { arbol->insertar(c); }); 20 | ^ 21 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc: In function ‘void indexar(std::vector, Arbolito*)’: 22 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:90: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 23 | for_each(canciones.begin(), canciones.end(), [arbol](Cancion* c) { arbol->insertar(c); }); 24 | ^ 25 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:91: error: no matching function for call to ‘for_each(std::vector::iterator, std::vector::iterator, indexar(std::vector, Arbolito*)::)’ 26 | for_each(canciones.begin(), canciones.end(), [arbol](Cancion* c) { arbol->insertar(c); }); 27 | ^ 28 | In file included from /usr/include/c++/5/algorithm:62:0, 29 | from /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:5: 30 | /usr/include/c++/5/bits/stl_algo.h:3761:5: note: candidate: template _Funct std::for_each(_IIter, _IIter, _Funct) 31 | for_each(_InputIterator __first, _InputIterator __last, _Function __f) 32 | ^ 33 | /usr/include/c++/5/bits/stl_algo.h:3761:5: note: template argument deduction/substitution failed: 34 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc: In substitution of ‘template _Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = __gnu_cxx::__normal_iterator >; _Funct = indexar(std::vector, Arbolito*)::]’: 35 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:91: required from here 36 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:91: error: template argument for ‘template _Funct std::for_each(_IIter, _IIter, _Funct)’ uses local type ‘indexar(std::vector, Arbolito*)::’ 37 | for_each(canciones.begin(), canciones.end(), [arbol](Cancion* c) { arbol->insertar(c); }); 38 | ^ 39 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:33:91: error: trying to instantiate ‘template _Funct std::for_each(_IIter, _IIter, _Funct)’ 40 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc: In function ‘int main()’: 41 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:48:8: error: ‘mostrar’ does not name a type 42 | auto mostrar = [](Cancion* c) { /* no es necesario */ }; 43 | ^ 44 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:49:8: error: ‘comparar’ does not name a type 45 | auto comparar = [](Cancion* a, Cancion* b) { return a->getAnio() - b->getAnio(); }; // comparando por año 46 | ^ 47 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:51:34: error: ‘mostrar’ was not declared in this scope 48 | Arbolito* arbol = new Arbolito(mostrar, comparar); // arbolito para indexar 49 | ^ 50 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:51:43: error: ‘comparar’ was not declared in this scope 51 | Arbolito* arbol = new Arbolito(mostrar, comparar); // arbolito para indexar 52 | ^ 53 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:51:51: error: new initializer expression list treated as compound expression [-fpermissive] 54 | Arbolito* arbol = new Arbolito(mostrar, comparar); // arbolito para indexar 55 | ^ 56 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:57:29: error: request for member ‘buscar’ in ‘* arbol’, which is of non-class type ‘Arbolito {aka int}’ 57 | Cancion* cancion = arbol->buscar(dummy, comparar); 58 | ^ 59 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/arbolesAVL/indexandoando.cc:60:18: error: ‘nullptr’ was not declared in this scope 60 | if (cancion != nullptr) { 61 | ^ 62 | -------------------------------------------------------------------------------- /Arboles/arbolesAVL/indexandoando.cc: -------------------------------------------------------------------------------- 1 | #include "avl.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | #define MAX 1000000 11 | 12 | using namespace std; 13 | 14 | class Cancion { 15 | string titulo; 16 | string artista; 17 | int anio; 18 | public: 19 | Cancion(string titulo = "", 20 | string artista = "", 21 | int anio = 1970) : titulo(titulo), artista(artista), anio(anio) {} 22 | ~Cancion() {} 23 | string getTitulo() { return titulo; } 24 | string getArtista() { return artista; } 25 | int getAnio() { return anio; } 26 | }; 27 | 28 | typedef function LambdaProc; 29 | typedef function LambdaComp; 30 | typedef ArbolAVL Arbolito; 31 | 32 | void indexar(vector canciones, Arbolito* arbol) { 33 | for_each(canciones.begin(), canciones.end(), [arbol](Cancion* c) { arbol->insertar(c); }); 34 | } 35 | 36 | int main() { 37 | vector canciones(MAX); 38 | 39 | srand(time(0)); 40 | cout << "Insertando elementos!" << endl; 41 | for (int i = 0; i < MAX; ++i) { // creando datos aleatorios 42 | stringstream titulo; titulo << "cancion " << i; 43 | stringstream artista; artista << "cantante " << (rand() % 1000); 44 | int anio = 2015 - rand() % 60; 45 | canciones[i] = new Cancion(titulo.str(), artista.str(), anio); 46 | } 47 | 48 | auto mostrar = [](Cancion* c) { /* no es necesario */ }; 49 | auto comparar = [](Cancion* a, Cancion* b) { return a->getAnio() - b->getAnio(); }; // comparando por año 50 | 51 | Arbolito* arbol = new Arbolito(mostrar, comparar); // arbolito para indexar 52 | 53 | cout << "Indexando!" << endl; 54 | indexar(canciones, arbol); 55 | 56 | Cancion* dummy = new Cancion("", "", 1999); 57 | Cancion* cancion = arbol->buscar(dummy, comparar); 58 | delete dummy; 59 | 60 | if (cancion != nullptr) { 61 | cout << "Encontrado!" << endl; 62 | cout << "Cancion: " << cancion->getTitulo() << endl 63 | << "Artista: " << cancion->getArtista() << endl; 64 | } 65 | cout << "Borrando todo!" << endl; 66 | for (int i = 0; i < MAX; ++i) { 67 | delete canciones[i]; 68 | } 69 | cout << "Bye bye!" << endl; 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /Arboles/arbolesFacil/arbol.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | template 6 | struct Nodo { 7 | T elemento; 8 | Nodo* izq; 9 | Nodo* der; 10 | }; 11 | 12 | template 13 | class ArbolB { 14 | Nodo* raiz; 15 | void (*procesar)(T); 16 | private: 17 | bool _insertar(Nodo*& nodo, T e) { 18 | if (nodo == NULL) { 19 | nodo = new Nodo(); 20 | nodo->elemento = e; 21 | } else if (e < nodo->elemento) { 22 | _insertar(nodo->izq, e); 23 | } else if (e >= nodo->elemento) { 24 | _insertar(nodo->der, e); 25 | } 26 | } 27 | 28 | void _enOrden(Nodo* nodo) { 29 | if (nodo == NULL) return; 30 | _enOrden(nodo->izq); 31 | procesar(nodo->elemento); 32 | _enOrden(nodo->der); 33 | } 34 | public: 35 | ArbolB(void (*otroPunteroAFuncion)(T)) { 36 | this->procesar = otroPunteroAFuncion; 37 | raiz = NULL; 38 | } 39 | bool insertar(T e) { 40 | return _insertar(raiz, e); 41 | } 42 | void enOrden() { 43 | _enOrden(raiz); 44 | } 45 | 46 | void eliminar(T e){ 47 | _eliminar(raiz,e); 48 | } 49 | 50 | }; 51 | -------------------------------------------------------------------------------- /Arboles/arbolesFacil/arbolBTest.cc: -------------------------------------------------------------------------------- 1 | #include "arbol.h" 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | void imprimir(int e) { 9 | cout << " " << e; 10 | } 11 | int main() { 12 | srand(time(0)); 13 | ArbolB* arbol = new ArbolB(imprimir); 14 | arbol->insertar(5); 15 | arbol->insertar(1); 16 | arbol->insertar(52); 17 | arbol->insertar(2); 18 | arbol->insertar(511); 19 | arbol->insertar(53); 20 | arbol->eliminar(2); 21 | arbol->enOrden(); 22 | cout << endl; 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /Arboles/avl.h: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | template class AVLTree { 4 | struct Node { 5 | T e; 6 | Node *l, *r; 7 | int h; 8 | 9 | Node(T e = iniT, 10 | Node* l = NULL, 11 | Node* r = NULL) 12 | : e(e), l(l), r(r) { 13 | int hl = l == NULL? -1 : l->h; 14 | int hr = r == NULL? -1 : r->h; 15 | this->h = (hl > hr? hl : hr) + 1; 16 | } 17 | }; 18 | int h(Node* n) { 19 | return n == NULL? -1 : n->h; 20 | } 21 | void fixH(Node* n) { 22 | n->h = (h(n->l) > h(n->r)? h(n->l) : h(n->r)) + 1; 23 | } 24 | void deleteAll(Node* n) { 25 | if (n != NULL) { 26 | deleteAll(n->l); 27 | deleteAll(n->r); 28 | delete n; 29 | } 30 | } 31 | void enOrden(Node* n) { 32 | if (n != NULL) { 33 | enOrden(n->l); 34 | proc(n->e); 35 | enOrden(n->r); 36 | } 37 | } 38 | void rotarIzq(Node*& x) { 39 | Node* y = x->r; // 1 40 | x->r = y->l; // 2 41 | y->l = x; // 3 42 | x = y; // 4 43 | fixH(x->l); 44 | fixH(x); 45 | } 46 | void rotarDer(Node*& y) { 47 | Node* x = y->l; // 1 48 | y->l = x->r; // 2 49 | x->r = y; // 3 50 | y = x; // 4 51 | fixH(y->r); 52 | fixH(y); 53 | } 54 | void balancear(Node*& n) { 55 | if (h(n->l) - h(n->r) > 1) { 56 | if (h(n->l->r) > h(n->l->l)) { 57 | rotarIzq(n->l); 58 | } 59 | rotarDer(n); 60 | } else if (h(n->r) - h(n->l) > 1) { 61 | if (h(n->r->l) > h(n->r->r)) { 62 | rotarDer(n->r); 63 | } 64 | rotarIzq(n); 65 | } 66 | } 67 | bool insertar(T e, Node*& n) { 68 | bool flag = false; 69 | if (n == NULL) { 70 | n = new Node(e); 71 | ++len; 72 | return n != NULL; 73 | } else if (cmp(e, n->e) < 0) { 74 | flag = insertar(e, n->l); 75 | } else { 76 | flag = insertar(e, n->r); 77 | } 78 | if (flag) { 79 | balancear(n); 80 | fixH(n); 81 | } 82 | return flag; 83 | } 84 | public: 85 | AVLTree(std::function proc, std::function cmp) 86 | : root(NULL), len(0), proc(proc), cmp(cmp) {} 87 | ~AVLTree() { deleteAll(root); } 88 | void enOrden() { enOrden(root); } 89 | bool insertar(T e) { return insertar(e, root); } 90 | int size() { return len; } 91 | int height() { return h(root); } 92 | private: 93 | Node *root; 94 | int len; 95 | std::function proc; 96 | std::function cmp; 97 | }; 98 | -------------------------------------------------------------------------------- /Arboles/deleteNode/Source.cpp: -------------------------------------------------------------------------------- 1 | #include "avl.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #define MAX 1000000 9 | 10 | using namespace std; 11 | 12 | void imprimir(int e) { 13 | cout << " " << e; 14 | } 15 | 16 | int buscar(vector v, int e) { 17 | for (int i = 0; i < v.size(); ++i) { 18 | if (v[i] == e) { 19 | return v[i]; 20 | } 21 | } 22 | } 23 | 24 | typedef function LambdaProc; 25 | typedef function LambdaComp; 26 | typedef ArbolAVL Arbolito; 27 | 28 | int main() { 29 | srand(time(0)); 30 | auto impr = [](int) {}; 31 | auto comparar = [](int a, int b) { return a - b; }; 32 | // ArbolAVL* arbol = new ArbolAVL(imprimir, comparar); // la vieja escuela 33 | Arbolito arbol(imprimir, comparar); // la nueva escuela 34 | 35 | //vector arreglito(MAX); 36 | 37 | //for (int i = 0; i < MAX; ++i) { 38 | // arbol.insertar(i + 1); 39 | // arreglito[i] = i + 1; 40 | // //arbol->insertar(rand() % 100); 41 | //} 42 | //// arbol->enOrden(); 43 | //long start = clock(); 44 | //cout << buscar(arreglito, MAX) << endl; 45 | //cout << "Tiempo busqueda lineal en arreglo: " << (clock() - start) << endl; 46 | //start = clock(); 47 | //cout << arbol.buscar(MAX, comparar) << endl; 48 | //cout << "Tiempo busqueda binaria en AVLTree: " << (clock() - start) << endl; 49 | 50 | arbol.insertar(50); 51 | arbol.insertar(60); 52 | arbol.insertar(70); 53 | arbol.insertar(80); 54 | arbol.insertar(90); 55 | arbol.insertar(40); 56 | arbol.insertar(30); 57 | arbol.enOrden(); 58 | arbol.eliminar(80); 59 | cout << endl; 60 | arbol.enOrden(); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /Arboles/deleteNode/avl.h: -------------------------------------------------------------------------------- 1 | #ifndef __ARBOLB_H__ 2 | #define __ARBOLB_H__ 3 | #include 4 | // #include 5 | 6 | // using namespace std; 7 | 8 | template 9 | struct Nodo { 10 | T elemento; // payload 11 | Nodo* izq; 12 | Nodo* der; 13 | int h; // altura: numero de ramas desde el nodo hasta la hoja m�s lejana 14 | public: 15 | Nodo() { 16 | h = 0; 17 | izq = NULL; 18 | der = NULL; 19 | } 20 | }; 21 | 22 | template // P para funcion procesar y C para funcion comparar 23 | class ArbolAVL { 24 | Nodo* raiz; 25 | P procesar; // templates de punteros a funciones o lambdas 26 | C comparar; // templates de punteros a funciones o lambdas 27 | private: 28 | void _rotarIzq(Nodo* x, Nodo*& y, Nodo*& p) { // p es el padre de x 29 | p = y; 30 | x->der = y->izq; 31 | p->izq = x; 32 | } 33 | void _rotarDer(Nodo*& x, Nodo* y, Nodo*& p) { // p es el padre de y 34 | p = x; 35 | y->izq = x->der; 36 | p->der = y; 37 | } 38 | 39 | bool _insertar(Nodo*& nodo, T e) { 40 | if (nodo == NULL) { 41 | nodo = new Nodo(); // h = 0 42 | nodo->elemento = e; 43 | } 44 | else if (comparar(e, nodo->elemento) < 0) { // con lambda 45 | _insertar(nodo->izq, e); 46 | } 47 | else /* if (comparar(e, nodo->elemento) > 0) */ { // descomentar para no aceptar duplicados 48 | _insertar(nodo->der, e); 49 | } 50 | int hi = _altura(nodo->izq); 51 | int hd = _altura(nodo->der); 52 | int d = hd - hi; 53 | if (d > 1) { // pesado a la derecha 54 | int hni = _altura(nodo->der->izq); 55 | int hnd = _altura(nodo->der->der); 56 | if (hni > hnd) { // zig zag derecha izquierda 57 | _rotarDer(nodo->der->izq, nodo->der, nodo->der); 58 | } 59 | _rotarIzq(nodo, nodo->der, nodo); 60 | } 61 | else if (d < -1) { // pesado a la izquierda 62 | int hni = _altura(nodo->izq->izq); 63 | int hnd = _altura(nodo->izq->der); 64 | if (hnd > hni) { // zig zag izquierda derecha 65 | _rotarIzq(nodo->izq, nodo->izq->der, nodo->izq); 66 | } 67 | _rotarDer(nodo->izq, nodo, nodo); 68 | } 69 | hi = _altura(nodo->izq); 70 | hd = _altura(nodo->der); 71 | nodo->h = 1 + (hi > hd ? hi : hd); 72 | return true; 73 | } 74 | 75 | 76 | void _enOrden(Nodo* nodo) { 77 | if (nodo == NULL) return; 78 | _enOrden(nodo->izq); 79 | procesar(nodo->elemento); 80 | _enOrden(nodo->der); 81 | } 82 | int _altura(Nodo* nodo) { 83 | if (nodo == NULL) return -1; 84 | return nodo->h; // implementacion optima en tiempo 85 | } 86 | T _buscar(Nodo* nodo, T e, C comparar) { // template funcional 87 | if (nodo == NULL) return 0; 88 | if (comparar(e, nodo->elemento) == 0) { 89 | return nodo->elemento; 90 | } 91 | else { 92 | return _buscar( 93 | comparar(e, nodo->elemento) > 0 ? 94 | nodo->der : 95 | nodo->izq, 96 | e, comparar); 97 | } 98 | } 99 | Nodo* _quitarMenor(Nodo* nodo, Nodo*& vinc) { 100 | if (nodo == NULL) return NULL; // ERROR!! 101 | if (nodo->izq == NULL) { // encontramos el menor!! 102 | vinc = nodo->der; 103 | nodo->der = NULL; 104 | return nodo; 105 | } 106 | else { 107 | return _quitarMenor(nodo->izq, nodo->izq); 108 | } 109 | } 110 | Nodo* _quitarMayor(Nodo* nodo, Nodo*& vinc) { 111 | if (nodo == NULL) return NULL; // ERROR!! 112 | if (nodo->der == NULL) { // encontramos el menor!! 113 | vinc = nodo->izq; 114 | nodo->izq = NULL; 115 | return nodo; 116 | } 117 | else { 118 | return _quitarMayor(nodo->der, nodo->der); 119 | } 120 | } 121 | bool _eliminar(T e, Nodo* nodo, Nodo*& vinc) { 122 | if (comparar(e, nodo->elemento) == 0) { 123 | int hi = _altura(nodo->izq); 124 | int hd = _altura(nodo->der); 125 | if (hi == -1 && hd == -1) { 126 | vinc = NULL; 127 | delete nodo; 128 | return true; 129 | } 130 | else if (hi > hd) { 131 | Nodo* reemplazo = _quitarMayor(nodo->izq, nodo->izq); 132 | reemplazo->izq = nodo->izq; 133 | reemplazo->der = nodo->der; 134 | vinc = reemplazo; 135 | delete nodo; 136 | } 137 | else { 138 | Nodo* reemplazo = _quitarMenor(nodo->der, nodo->der); 139 | reemplazo->izq = nodo->izq; 140 | reemplazo->der = nodo->der; 141 | vinc = reemplazo; 142 | delete nodo; 143 | } 144 | } 145 | else if (comparar(e, nodo->elemento) > 0 && nodo->der != NULL) { 146 | return _eliminar(e, nodo->der, nodo->der); 147 | } 148 | else if (comparar(e, nodo->elemento) < 0 && nodo->izq != NULL) { 149 | return _eliminar(e, nodo->izq, nodo->izq); 150 | } 151 | else { 152 | return false; 153 | } 154 | } 155 | public: 156 | ArbolAVL(P funcion1, C funcion2) { // con template funcional 157 | this->procesar = funcion1; 158 | this->comparar = funcion2; 159 | raiz = NULL; 160 | } 161 | bool insertar(T e) { 162 | return _insertar(raiz, e); 163 | } 164 | void enOrden() { 165 | _enOrden(raiz); 166 | } 167 | int altura() { 168 | _altura(raiz); 169 | } 170 | T buscar(T e, C comparar) { // con template funcional 171 | return _buscar(raiz, e, comparar); 172 | } 173 | bool eliminar(T e) { 174 | return _eliminar(e, raiz, raiz); 175 | } 176 | }; 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /Arboles/deleteNode/compiling_error.txt: -------------------------------------------------------------------------------- 1 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:24:9: error: ‘function’ does not name a type 2 | typedef function LambdaProc; 3 | ^ 4 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:25:9: error: ‘function’ does not name a type 5 | typedef function LambdaComp; 6 | ^ 7 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:26:23: error: ‘LambdaProc’ was not declared in this scope 8 | typedef ArbolAVL Arbolito; 9 | ^ 10 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:26:35: error: ‘LambdaComp’ was not declared in this scope 11 | typedef ArbolAVL Arbolito; 12 | ^ 13 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:26:45: error: template argument 2 is invalid 14 | typedef ArbolAVL Arbolito; 15 | ^ 16 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:26:45: error: template argument 3 is invalid 17 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp: In function ‘int main()’: 18 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:30:7: error: ‘impr’ does not name a type 19 | auto impr = [](int) {}; 20 | ^ 21 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:31:7: error: ‘comparar’ does not name a type 22 | auto comparar = [](int a, int b) { return a - b; }; 23 | ^ 24 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:33:27: error: ‘comparar’ was not declared in this scope 25 | Arbolito arbol(imprimir, comparar); // la nueva escuela 26 | ^ 27 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:33:35: error: expression list treated as compound expression in initializer [-fpermissive] 28 | Arbolito arbol(imprimir, comparar); // la nueva escuela 29 | ^ 30 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:50:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 31 | arbol.insertar(50); 32 | ^ 33 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:51:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 34 | arbol.insertar(60); 35 | ^ 36 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:52:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 37 | arbol.insertar(70); 38 | ^ 39 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:53:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 40 | arbol.insertar(80); 41 | ^ 42 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:54:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 43 | arbol.insertar(90); 44 | ^ 45 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:55:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 46 | arbol.insertar(40); 47 | ^ 48 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:56:8: error: request for member ‘insertar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 49 | arbol.insertar(30); 50 | ^ 51 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:57:8: error: request for member ‘enOrden’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 52 | arbol.enOrden(); 53 | ^ 54 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:58:8: error: request for member ‘eliminar’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 55 | arbol.eliminar(80); 56 | ^ 57 | /home/enzoftware/Projects/AlgoritmosYEstructurasDeDatos_CC68/Arboles/deleteNode/Source.cpp:60:8: error: request for member ‘enOrden’ in ‘arbol’, which is of non-class type ‘Arbolito {aka int}’ 58 | arbol.enOrden(); 59 | ^ 60 | -------------------------------------------------------------------------------- /ContestFinal/HRECURS - Hello Recursion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int sumarecursiva(int arr[] , int s , int pos , int cont){ 5 | if(s == pos) return cont; 6 | else{ 7 | cont+=arr[pos]; 8 | sumarecursiva(arr,s,pos+1,cont); 9 | } 10 | } 11 | 12 | 13 | int main(){ 14 | int casos; 15 | int numcasos = 0; 16 | int contador = 0; 17 | cin>>casos; 18 | while(casos--){ 19 | int tam,posicion = 0; 20 | cin>>tam; 21 | int arreglo[tam]; 22 | for(int i = 0 ; i>arreglo[i]; 24 | } 25 | numcasos++; 26 | cout<<"Case "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | class ArbolAVL { 7 | 8 | struct Nodo{ 9 | Nodo *l; 10 | Nodo* r; 11 | int e; 12 | int h; 13 | Nodo(int e = 0 , Nodo *l = NULL ,Nodo* r = NULL) : e(e),l(l),r(r){ 14 | int hnl = (l == NULL) ? -1 : l->h; 15 | int hnr = (r == NULL) ? -1 : r->h ; 16 | this->h = ( (hnl > hnr) ? hnl : hnr ) + 1 ; 17 | } 18 | }; 19 | 20 | void imprimirOrdenado(Nodo* n){ 21 | if(n != NULL){ 22 | imprimirOrdenado(n->l); 23 | imprimir(n->e); 24 | imprimirOrdenado(n->r); 25 | } 26 | } 27 | 28 | 29 | bool _insertar(Nodo *&nodo , int ele){ 30 | if(nodo == NULL){ 31 | nodo = new Nodo(ele); 32 | nodos++; 33 | return nodo != NULL; 34 | }else if (ele < nodo->e){ 35 | _insertar(nodo->l,ele); 36 | }else{ 37 | _insertar(nodo->r,ele); 38 | } 39 | } 40 | 41 | 42 | 43 | private: 44 | Nodo * raiz; 45 | int nodos; 46 | void (*imprimir)(int); 47 | 48 | public: 49 | ArbolAVL (void (*ptrFunc)(int)){ 50 | this->imprimir = ptrFunc; 51 | } 52 | 53 | int getNumNodos(){ 54 | return nodos; 55 | } 56 | 57 | void printTree(){ 58 | imprimirOrdenado(raiz); 59 | } 60 | 61 | bool insertarNodo(int e){ 62 | return _insertar(raiz,e); 63 | } 64 | 65 | }; 66 | 67 | 68 | void imprimir(int e){ 69 | cout<<" "<insertarNodo(rand()%100); 77 | } 78 | arbol->printTree(); 79 | 80 | return 0; 81 | } 82 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

Algorithms and data structure - CC68

2 |
    3 |
  • Structs
  • 4 |
  • Temaplates
  • 5 |
  • Linked List - ALL LIKED LIST
  • 6 |
  • Sort Algorithms
  • 7 |
  • Lambdas
  • 8 |
  • Trees
  • 9 |
  • AVL Trees
  • 10 |
11 | 12 |

ALL IN C++ LANGUAGE

13 | -------------------------------------------------------------------------------- /Unidad1/Crecimiento de funciones/010104-ejercicios_crecimiento_de_funciones.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad1/Crecimiento de funciones/010104-ejercicios_crecimiento_de_funciones.pdf -------------------------------------------------------------------------------- /Unidad1/Crecimiento de funciones/cf_13.cpp: -------------------------------------------------------------------------------- 1 | /*Encontrar el número mayor en un arreglo de enteros*/ 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | vector vi; 9 | int valor; 10 | 11 | int main(){ 12 | 13 | cout<<"Ingrese los elementos del arreglo y para finalizar ingrese 0"<>valor; 17 | if(valor == 0) { 18 | break; 19 | }else{ 20 | vi.push_back(valor); 21 | } 22 | } 23 | 24 | cout<<*max_element(vi.begin(),vi.end()); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /Unidad1/Crecimiento de funciones/cf_14.cpp: -------------------------------------------------------------------------------- 1 | /*Ordenar un arreglo de números enteros*/ 2 | 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | vector vi; 10 | int valor; 11 | 12 | void printArreglo(int i){ 13 | cout<>valor; 20 | if(valor == 0)break; 21 | else vi.push_back(valor); 22 | } 23 | 24 | sort(vi.begin(),vi.end()); 25 | cout<<"El arreglo ordenado es : "< 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | vectorvec; 10 | int valor,pos; 11 | 12 | void imprimir(int i){ 13 | cout<>valor; 20 | if(valor == 0)break; 21 | else vec.push_back(valor); 22 | } 23 | 24 | cout<<"Ingresa la POSICION del valor que deseas eliminar"<>pos; 26 | vec.erase(vec.begin()+pos); 27 | for_each(vec.begin(),vec.end(),imprimir); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Unidad1/Crecimiento de funciones/cf_16.cpp: -------------------------------------------------------------------------------- 1 | /*Buscar un número en un arreglo*/ 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | vectorvi; 8 | int valor,val; 9 | 10 | int main(){ 11 | 12 | cout<<"Llenar el arreglo y detenerse con el 0"<>valor; 15 | if(valor == 0)break; 16 | else vi.push_back(valor); 17 | } 18 | 19 | cout<<"Ingresa el valor que deseas encontrar : "; 20 | cin>>val; 21 | 22 | bool encontro = binary_search(vi.begin(),vi.end(),val); 23 | (encontro == true ? cout<<"Si esta el elemento" : cout<<"El elemento no fue encontrado"); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /Unidad1/Crecimiento de funciones/cf_17.cpp: -------------------------------------------------------------------------------- 1 | /*Calcular el factorial de N*/ 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | int N; 9 | 10 | int fact(int n){ 11 | 12 | if(n==1) return 1; 13 | return(n*fact(n-1)); 14 | 15 | } 16 | 17 | int main(){ 18 | cout<<"Ingresa el valor de N y se hallara el factorial"<>N; 20 | cout< 2 | using namespace std; 3 | 4 | ofstream fout("libros.txt"); 5 | 6 | 7 | 8 | struct Libro{ 9 | int isbn; 10 | string titulo; 11 | string autor; 12 | string genero; 13 | }; 14 | 15 | void printBook(Libro l){ 16 | cout<l){ 24 | for_each(l.begin(),l.end(),printBook); 25 | } 26 | 27 | void imprimirLibrostxt(vectorl){ 28 | for_each(l.begin(),l.end(),printText); 29 | } 30 | 31 | 32 | 33 | class GestorLibros{ 34 | 35 | private: 36 | 37 | vectorlibros; 38 | 39 | public: 40 | 41 | GestorLibros(){} 42 | 43 | bool existe(int isbn){ 44 | for(int i = 0 ; i < libros.size() ; ++i){ 45 | if(libros[i].isbn == isbn ) return true; 46 | } 47 | return false; 48 | } 49 | void AnadirLibro(int isbn , string t ,string a ,string g){ 50 | Libro l; 51 | l.isbn = isbn; 52 | l.titulo = t; 53 | l.autor = a; 54 | l.genero = g; 55 | libros.push_back(l); 56 | 57 | } 58 | 59 | void EliminarLibro(int isbn){ 60 | for(int i = 0 ; i< libros.size(); ++i){ 61 | if(libros[i].isbn == isbn){ 62 | libros.erase(libros.begin()+i); 63 | return; 64 | } 65 | } 66 | 67 | cout<<"Este ISBN de libro no existe"< ListarLibros(){ 71 | return libros; 72 | } 73 | 74 | void ModificarLibro(Libro l){ 75 | for(int i = 0 ; i < libros.size() ; ++i){ 76 | if(l.isbn == libros[i].isbn){ 77 | libros[i].isbn = l.isbn; 78 | libros[i].titulo = l.titulo; 79 | libros[i].autor = l.autor; 80 | libros[i].genero = l.genero; 81 | return; 82 | } 83 | } 84 | 85 | cout<<"Este ISBN de libro no existe"<LeerArchivo(); 127 | while(cont){ 128 | cout<>op; 137 | 138 | switch(op){ 139 | case 1: 140 | cout<<"Ingresa el ISBN : ";cin>>isbn; cin.ignore(); 141 | cout<<"Ingresa el titulo : ";getline(cin,t); 142 | cout<<"Ingresa el autor : ";getline(cin,a); 143 | cout<<"Ingresa el genero : ";getline(cin,g); 144 | if(gestor->existe(isbn)) cout<<"Ya existe el isbn "<AnadirLibro(isbn,t,a,g); 146 | break; 147 | case 2: 148 | cout<<"Ingresa el ISBN del libro a eliminar "; 149 | cin>>v; 150 | gestor->EliminarLibro(v); 151 | break; 152 | case 3 : 153 | cout<<"Ingresa el isbn del libro a modificar : "; cin>>book.isbn; cin.ignore(); 154 | cout<<"Ingresa el titulo del libro a modificar : "; getline(cin,book.titulo); 155 | cout<<"Ingresa el autor del libro a modificar : "; getline(cin,book.autor); 156 | cout<<"Ingresa el genero del libro a modificar : "; getline(cin,book.genero); 157 | gestor->ModificarLibro(book); 158 | break; 159 | case 4: 160 | cout<<"Ingresa el nombre del autor del libro a buscar : "; cin>>aut; 161 | gestor->BuscarLibro(aut); 162 | break; 163 | case 5: 164 | imprimirLibros(gestor->ListarLibros()); 165 | break; 166 | case 6: 167 | cout<<"Gracias por usar la biblioteca"<ListarLibros()); 176 | 177 | return 0; 178 | } 179 | -------------------------------------------------------------------------------- /Unidad1/MOCKPC1/MOCKPC1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad1/MOCKPC1/MOCKPC1.o -------------------------------------------------------------------------------- /Unidad1/MOCKPC1/datos.txt: -------------------------------------------------------------------------------- 1 | 1,El arte de viajar en combi,Pedro Shiguihara,ficción 2 | 123,Introduction to Algorithms,Cormen,Programacion 3 | 41324,A song of ice and fire,George R R Martin,ficcion 4 | -------------------------------------------------------------------------------- /Unidad1/MOCKPC1/libros.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad1/MOCKPC1/libros.txt -------------------------------------------------------------------------------- /Unidad1/Semana 1/Generalizacion de tipos/010103-ejercicios_generalizacion.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad1/Semana 1/Generalizacion de tipos/010103-ejercicios_generalizacion.pdf -------------------------------------------------------------------------------- /Unidad1/Semana 1/Generalizacion de tipos/f_1.cpp: -------------------------------------------------------------------------------- 1 | /* Encontrar el elemento mayor en un arreglo. */ 2 | 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | double maxi; 8 | double v; 9 | 10 | vector vd; 11 | 12 | template 13 | T hallarMaximo(T i){ 14 | maxi = (i > maxi ? i : maxi); 15 | } 16 | 17 | int main(){ 18 | 19 | cout<<"Ingrese los elementos del vector y para finalizar ingrese 0"<>v; 23 | if(v == 0) { 24 | break; 25 | }else{ 26 | vd.push_back(v); 27 | } 28 | } 29 | maxi= vd[0]; 30 | for_each(vd.begin(),vd.end(),hallarMaximo); 31 | 32 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | 8 | 9 | class Fecha{ 10 | int dia, mes , anio, diasTotales; 11 | public: 12 | Fecha(int dia = 1 , int mes = 1 , int anio = 1, int diasTotales = 0 ) : dia(dia) , mes(mes) , anio(anio), diasTotales(diasTotales){} 13 | 14 | void calcDif(){ 15 | for(int i = 1 ; i<= this->anio ; i++){ 16 | if(esBisiesto(i)) diasTotales += 366; 17 | else diasTotales += 365; 18 | } 19 | for( int i = 1 ; i<= this->mes ; i++){ 20 | switch(i){ 21 | case 2 : 22 | if(esBisiesto(this->anio)) diasTotales+=29; 23 | else diasTotales += 28 ; 24 | break; 25 | case 1 : case 3 : case 5: case 7 : case 8 : case 10 : case 12 : diasTotales += 31; break; 26 | case 4 : case 6 : case 9 : case 11: diasTotales += 30; break; 27 | } 28 | } 29 | 30 | diasTotales += this->dia; 31 | } 32 | 33 | bool esBisiesto(int a){ 34 | return((a%400 == 0 && a%4==0) || a%100 != 0 ? true : false); 35 | } 36 | 37 | Fecha operator -(Fecha f){ 38 | return Fecha(0,0,0,this->diasTotales - f.diasTotales); 39 | } 40 | 41 | int imprimirResultado(){ 42 | return(this->diasTotales); 43 | } 44 | }; 45 | 46 | int d,m,a; 47 | 48 | int main() { 49 | 50 | cout<<"Ingresa datos de fecha 1 "<>d>>m>>a; 52 | Fecha f1(d,m,a,0); 53 | f1.calcDif(); 54 | cout<<"Ingresa datos de fecha 2 "<>d>>m>>a; 56 | Fecha f2(d,m,a,0); 57 | f2.calcDif(); 58 | Fecha f3 = f1 - f2; 59 | cout<<"La diferencia entre f1 y f2 es "< 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Imaginario { 9 | float real; 10 | float img; 11 | public: 12 | Imaginario(float real = 0, float img = 0) : real(real), img(img) {} 13 | 14 | Imaginario operator +(Imaginario otro) { 15 | return Imaginario(this->real + otro.real, this->img + otro.img); 16 | } 17 | string toStr() { 18 | stringstream ss; 19 | ss << this->real << " + " << this->img << "i"; 20 | return ss.str(); 21 | } 22 | }; 23 | 24 | int main() { 25 | Imaginario a(1, 2); 26 | Imaginario b(2, 3); 27 | 28 | Imaginario c = a + b; 29 | cout << c.toStr(); 30 | return 0; 31 | } 32 | 33 | -------------------------------------------------------------------------------- /Unidad1/Semana 2/Cadenas/010203-ejercicios_cadenas.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad1/Semana 2/Cadenas/010203-ejercicios_cadenas.pdf -------------------------------------------------------------------------------- /Unidad1/Semana 2/Cadenas/10c.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | string str; 4 | 5 | int main(){ 6 | getline(cin,str); 7 | for(int i = 0 ; i < str.size() ; i++){ 8 | str[i] = toupper(str[i]); 9 | } 10 | cout< 2 | using namespace std; 3 | string str; 4 | 5 | int main(){ 6 | 7 | getline(cin,str); 8 | for(int i = 0 ; i < str.size() ; i++){ 9 | str[i] = tolower(str[i]); 10 | } 11 | cout< 2 | using namespace std; 3 | string str; 4 | 5 | int main(){ 6 | getline(cin,str); 7 | 8 | for(int i = 0 ; i < str.size() ; i++){ 9 | 10 | if(i==0){str[i]=toupper(str[i]); continue;} 11 | 12 | if(int(str[i])==32 && int(str[i+1]) > 96){ 13 | str[i+1] = toupper(str[i+1]); 14 | }else if(int(str[i-1]) != 32 ){ 15 | str[i] = tolower(str[i]); 16 | } 17 | 18 | 19 | } 20 | cout< 2 | #include 3 | using namespace std; 4 | 5 | string str,str2,str3; 6 | 7 | int main(){ 8 | cout<<"Oracion inicial : "; 9 | getline(cin,str); 10 | cout<<"Palbra a encontrar "; 11 | cin>>str2; 12 | cout<<"Palbra reemplazadora "; 13 | cin>>str3; 14 | 15 | str.replace(str.find(str2),str2.length(),str3); 16 | cout< 3 | 4 | using namespace std; 5 | 6 | int contarString(string str , char x){ 7 | 8 | for(int i = 0 ; i< str.size() ; i++){ 9 | if(str[i]==x) return i; 10 | else{ 11 | return -1; 12 | } 13 | } 14 | } 15 | 16 | int main(){ 17 | 18 | cout< 2 | using namespace std; 3 | 4 | string reverse(string str){ 5 | string x; 6 | for(int i = str.size()-1 ; i >= 0 ; i--){ 7 | x+=str[i]; 8 | } 9 | return x; 10 | } 11 | 12 | int main(){ 13 | string x = "Hola como estas"; 14 | cout< 2 | using namespace std; 3 | 4 | 5 | string convertir(int d , int m , int an){ 6 | string f; 7 | f += to_string(d); 8 | f+=" de "; 9 | string ms; 10 | switch(m){ 11 | case 1 : ms = "enero"; 12 | break; 13 | case 2 : ms = "febrero"; 14 | break; 15 | case 3 : ms = "marzo"; 16 | break; 17 | case 4 : ms = "abril"; 18 | break; 19 | case 5 : ms = "mayo"; 20 | break; 21 | case 6 : ms = "junio"; 22 | break; 23 | case 7 : ms = "julio"; 24 | break; 25 | case 8 : ms = "agosto"; 26 | break; 27 | case 9 : ms = "septiembre"; 28 | break; 29 | case 10 : ms = "octubre"; 30 | break; 31 | case 11 : ms = "noviembre"; 32 | break; 33 | case 12 : ms = "diciembre"; 34 | break; 35 | } 36 | 37 | f+=ms; 38 | f+=" del "; 39 | f+=to_string(an); 40 | 41 | return f; 42 | 43 | } 44 | 45 | int main(){ 46 | 47 | cout< 2 | #include "point.h" 3 | using namespace std; 4 | 5 | void imprimirPuntos(Point p){ 6 | cout<<" X : "< points; 13 | 14 | public: 15 | 16 | Path(){} 17 | void agregarPoint(int x , int y){ 18 | Point p; 19 | p.x = x; p.y = y; 20 | points.push_back(p); 21 | } 22 | 23 | Point getPoint(int pos){ 24 | if(pos < points.size()){ 25 | return points[pos]; 26 | }else{ 27 | cout<<"Posicion invalida"<>op; 57 | 58 | return op; 59 | } 60 | 61 | int main(){ 62 | int x,y,pos; 63 | Path *path = new Path(); 64 | int xd; 65 | 66 | do{ 67 | xd = menu(); 68 | 69 | switch(xd){ 70 | 71 | case 1 : 72 | cout<<"Ingresa coordenadas (X,Y)"; cin>>x>>y; 73 | path->agregarPoint(x,y); 74 | break; 75 | 76 | case 2 : 77 | cout<<"Ingresa posicion a eliminar"; cin>>pos; 78 | path->borrarPoint(pos); 79 | break; 80 | 81 | case 3 : 82 | path->mostrarPoints(); 83 | break; 84 | 85 | case 4 : 86 | cout<<"La cantidad de memoria que usa la clase es de : "< 2 | #include 3 | 4 | using namespace std; 5 | 6 | template 7 | 8 | class ListaCircularDE { 9 | struct Nodo { 10 | T elemento; 11 | Nodo *next; 12 | Nodo *prev; 13 | Nodo(T elemento = 0, Nodo *next = NULL, Nodo *prev = NULL) : elemento(elemento), next(next), prev(prev) {} 14 | }; 15 | Nodo *inicio; 16 | //Nodo *ultimo; 17 | int n; 18 | public: 19 | class Iterador { 20 | Nodo *aux; 21 | int cont; 22 | public: 23 | Iterador(Nodo *aux = NULL):aux(aux) {} 24 | void operator ++(int dummy) { aux = aux->next; cont++; } 25 | bool operator !=(Iterador it) { return aux != it.aux; } 26 | T operator *() { return aux->elemento; } 27 | }; 28 | public: 29 | ListaCircularDE() :inicio(NULL), n(0) {} 30 | ~ListaCircularDE() { 31 | while (inicio != NULL) { 32 | Nodo *aux = inicio; 33 | inicio = inicio->next; 34 | delete aux; 35 | } 36 | } 37 | Iterador inicial() { 38 | return Iterador(inicio); 39 | } 40 | Iterador ultimito() { 41 | return Iterador(inicio->prev); 42 | } 43 | int tamannio() { 44 | return n; 45 | } 46 | /*T ultimoNext() { 47 | Nodo *aux = ultimo->next; 48 | return aux->elemento; 49 | }*/ 50 | T inicioPrev() { 51 | Nodo *aux = inicio->prev; 52 | return aux->elemento; 53 | } 54 | bool insertarInicio(T elemento) { 55 | Nodo *nuevo = new Nodo(elemento, inicio, inicio->prev/* inicio -> prev = ultimo */); 56 | if (nuevo == NULL) { return false; } 57 | if (inicio == NULL) { Nodo*a = inicio->prev;/* inicio -> prev = ultimo */ inicio = nuevo; a->next = inicio; ++n; return true; } 58 | Nodo*a = inicio->prev;/* inicio -> prev = ultimo */ 59 | inicio->prev = nuevo; 60 | inicio = nuevo; 61 | a->next = inicio; 62 | ++n; 63 | return true; 64 | } 65 | /*bool insertarFinal(T elemento) { 66 | Nodo *nuevo = new Nodo(elemento, inicio, ultimo); 67 | if (inicio == NULL) { inicio = ultimo = nuevo; ultimo->next = inicio; ++n; return true; } 68 | if (nuevo == NULL) { return false; } 69 | ultimo->next = nuevo; 70 | inicio->prev = nuevo; 71 | ultimo = nuevo; 72 | ++n; 73 | return true; 74 | } 75 | bool insertarPOS(T elemento, int pos) { 76 | if (pos < 0 || pos > n) return false; 77 | if (pos == 0) { insertarInicio(elemento); return true; } 78 | if (pos == n) { insertarFinal(elemento); return true; } 79 | Nodo *aux = inicio; 80 | int c = 1; 81 | while (c++ < pos) { 82 | aux = aux->next; 83 | } 84 | Nodo *aux2 = aux->next; 85 | Nodo *nuevo = new Nodo(elemento, aux2, aux); 86 | if (nuevo == NULL) return false; 87 | aux->next = nuevo; 88 | aux2->prev = nuevo; 89 | ++n; 90 | return true; 91 | } 92 | bool eliminarInicio() { 93 | if (n == 0) { return false; } 94 | Nodo *aux = inicio; 95 | inicio = inicio->next; 96 | inicio->prev = ultimo; 97 | ultimo->next = inicio; 98 | delete aux; 99 | --n; 100 | return true; 101 | } 102 | bool eliminarFinal() { 103 | if (n == 0) { return false; } 104 | Nodo *aux = ultimo; 105 | Nodo *aux2 = ultimo->prev; 106 | inicio->prev = aux2; 107 | aux2->next = inicio; 108 | ultimo = aux2; 109 | delete aux; 110 | --n; 111 | return true; 112 | } 113 | bool eliminarPOS(int pos) { 114 | if (pos < 0 || pos > n) return false; 115 | if (n == 0) return false; 116 | if (pos == 0) { eliminarInicio(); return true; } 117 | if (pos == n - 1) { eliminarFinal(); return true; } 118 | Nodo *aux = inicio; 119 | int c = 1; 120 | while (c++ < pos) { 121 | aux = aux->next; 122 | } 123 | Nodo *aux2 = aux->next; 124 | Nodo *aux3 = aux2->next; 125 | aux->next = aux3; 126 | aux3->prev = aux; 127 | delete aux2; 128 | --n; 129 | return true; 130 | } 131 | T buscarElemento(int pos) { 132 | if (n == 0) return 0; 133 | if (pos < 0 || pos > n) return 0; 134 | Nodo *aux = inicio; 135 | int c = 1; 136 | while (c++ <= pos) { 137 | aux = aux-> next; 138 | } 139 | return aux->elemento; 140 | } 141 | int encontrarDato(T elemento) { 142 | if (n == 0) { return 0; } 143 | Nodo *aux = inicio; 144 | int c = 0; int pos = 0; 145 | while (c++ < n) { 146 | if (aux->elemento == elemento) return pos; 147 | else { 148 | ++pos; 149 | aux = aux->next; 150 | } 151 | } 152 | return -1; 153 | }*/ 154 | }; 155 | 156 | 157 | int main() 158 | { 159 | ListaCircularDElcde; 160 | 161 | char x; 162 | 163 | cin >> x; 164 | lcde.insertarInicio(x); 165 | cin >> x; 166 | lcde.insertarInicio(x); 167 | cin >> x; 168 | lcde.insertarInicio(x); 169 | cin >> x; 170 | lcde.insertarInicio(x); 171 | cin >> x; 172 | lcde.insertarInicio(x); 173 | cin >> x; 174 | lcde.insertarInicio(x); 175 | 176 | /*lcde.eliminarInicio(); 177 | lcde.eliminarFinal(); 178 | lcde.eliminarPOS(2); 179 | lcde.insertarPOS('h', 2); 180 | lcde.insertarFinal('k');*/ 181 | 182 | 183 | ListaCircularDE::Iterador it = lcde.inicial(); 184 | for (; it != lcde.ultimito(); it++) 185 | { 186 | cout << *it << " "; 187 | } 188 | cout << *it; 189 | cout << endl << endl; 190 | 191 | /*cout << "Elemento en la posicion 4: " << lcde.buscarElemento(4) << endl; 192 | cout << "El elemento siguiente del final es: " << lcde.ultimoNext() << endl; 193 | cout << "El elemento anterior del inicio es: " << lcde.inicioPrev() << endl; 194 | 195 | cout << "Ingrese dato a encontrar: "; cin >> x; 196 | cout << "\nEl dato se encontro en la posicion: " << lcde.encontrarDato(x) << endl;*/ 197 | 198 | return 0; 199 | } 200 | -------------------------------------------------------------------------------- /Unidad2/EjerciciosPPT/ejercicio1_listasimple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | template 4 | 5 | class ListaSimple{ 6 | private: 7 | struct Nodo{ 8 | Nodo *next; 9 | T elemento; 10 | Nodo(T elemento = 0 , Nodo * next = NULL) : elemento(elemento) , next(next) {} 11 | }; 12 | 13 | Nodo *inicio; 14 | int n; 15 | 16 | /* Clase iterador para poder acceder a los elemnetos de nuestra propia lista */ 17 | 18 | public: 19 | 20 | class iterador{ 21 | Nodo* auxi; 22 | public: 23 | iterador(Nodo *auxi = NULL) : auxi(auxi){} 24 | void operator ++(int dummy){ auxi = auxi->next; } 25 | bool operator !=(iterador t){ return auxi != t.auxi; } 26 | T operator*(){ return auxi->elemento; } 27 | }; 28 | 29 | /* Clase iterador para poder acceder a los elemnetos de nuestra propia lista */ 30 | 31 | 32 | public: 33 | ListaSimple() : n(0), inicio(NULL){} 34 | ~ListaSimple(){ 35 | while(inicio != NULL){ 36 | Nodo * aux = inicio; 37 | inicio = inicio->next; 38 | delete aux; 39 | } 40 | } 41 | 42 | 43 | /* INICIO METODOS Iterador */ 44 | 45 | iterador Inicio(){ 46 | return iterador(inicio); 47 | } 48 | 49 | iterador Finalito(){ 50 | return iterador(NULL); 51 | } 52 | 53 | /* FIN METODOS Iterador */ 54 | 55 | bool insertarInicio(T data){ 56 | Nodo * nuevo = new Nodo(data,inicio); 57 | if(nuevo == NULL) return false; 58 | inicio = nuevo; 59 | ++n; 60 | return true; 61 | } 62 | 63 | bool insertarPosicion(T data , int pos){ 64 | if(pos > n || pos < 0) return false; 65 | if(pos == 0 ) insertarInicio(data); 66 | int cont = 1; 67 | Nodo *auxi = inicio; 68 | /* Se aumenta el contador dentro del while por el '++' */ 69 | while(cont++ < pos){ 70 | auxi = auxi->next; 71 | } 72 | 73 | Nodo* nuevo = new Nodo(data, auxi->next); 74 | if(nuevo == NULL) return false; 75 | auxi->next = nuevo; 76 | ++n; 77 | return true; 78 | } 79 | 80 | bool insertarOrdenado( T data){ 81 | Nodo * nuevo = new Nodo(data,inicio); 82 | Nodo *temp = inicio; 83 | if(nuevo == NULL) return false; 84 | if(n == 0 ) insertarInicio(data); 85 | if(inicio->elemento > data){ 86 | nuevo->next = inicio; 87 | inicio = nuevo; 88 | }else{ 89 | while(temp->next != NULL && temp->next->elemento < data){ 90 | temp = temp->next; 91 | } 92 | nuevo->next = temp->next; 93 | temp->next = nuevo; 94 | } 95 | ++n; 96 | return true; 97 | } 98 | 99 | bool Ordenar(){ 100 | if(n==0) return false; 101 | T temp_data; 102 | Nodo * aux_node = inicio; 103 | Nodo *temp = aux_node; 104 | while (aux_node){ 105 | temp = aux_node; 106 | while (temp->next){ 107 | temp = temp->next; 108 | if(aux_node->elemento > temp->elemento){ 109 | temp_data = aux_node->elemento ; 110 | aux_node->elemento = temp->elemento; 111 | temp->elemento = temp_data; 112 | } 113 | } 114 | aux_node = aux_node->next; 115 | } 116 | return true; 117 | } 118 | 119 | int EncontrarDato(T data){ 120 | Nodo*temp = inicio; 121 | int pos = 0 , cont = 0; 122 | while (temp){ 123 | if(temp->elemento == data){ 124 | return pos; 125 | cont++; 126 | } 127 | temp = temp->next; 128 | pos++; 129 | } 130 | 131 | if(cont == 0) return -1; 132 | } 133 | 134 | 135 | bool insertarFinal(T data){ 136 | insertarPosicion(data,n); 137 | return true; 138 | } 139 | 140 | bool eliminarInicio(){ 141 | if(n == 0 )return false; 142 | Nodo* aux = inicio; 143 | inicio = inicio->next; 144 | delete aux; 145 | --n; 146 | return true; 147 | } 148 | 149 | bool eliminarPosicion(int pos){ 150 | if(pos == 0) eliminarInicio(); 151 | if(pos < 0 || pos > n ) return false; 152 | Nodo *aux1 = inicio; 153 | int conta = 1; 154 | while(conta++ < pos ){ 155 | aux1 = aux1 ->next; 156 | } 157 | Nodo* aux2 = aux1 -> next; 158 | aux1->next = aux2->next; 159 | delete aux2; 160 | --n; 161 | return true; 162 | } 163 | 164 | bool eliminarFinal(){ 165 | eliminarPosicion(n-1); 166 | --n; 167 | return true; 168 | } 169 | 170 | int tamanio(){ 171 | return n; 172 | } 173 | 174 | }; 175 | 176 | 177 | int main(){ 178 | int x; 179 | ListaSimple ls; 180 | cin>>x; 181 | ls.insertarInicio(x); 182 | cin>>x; 183 | ls.insertarInicio(x); 184 | cin>>x; 185 | ls.insertarFinal(x); 186 | cin>>x; 187 | ls.insertarFinal(x); 188 | cin>>x; 189 | ls.insertarFinal(x); 190 | cin>>x; 191 | ls.insertarFinal(x); 192 | ls.Ordenar(); 193 | 194 | cin>>x; 195 | ls.insertarOrdenado(x); 196 | 197 | ListaSimple::iterador it= ls.Inicio(); 198 | 199 | for( ; it != ls.Finalito() ; it++){ 200 | cout<< *it << " "; 201 | } 202 | 203 | if(ls.EncontrarDato(25) < 0 ) cout<<" Dato no encontrado"< 2 | using namespace std; 3 | 4 | template 5 | class ListaCircular{ 6 | private: 7 | struct Nodo{ 8 | Nodo* next; 9 | T dato; 10 | Nodo(T dato = 0, Nodo * next = NULL ) : dato(dato) , next(next){} 11 | }; 12 | 13 | Nodo * primero; 14 | Nodo * ultimo; 15 | int n; 16 | 17 | public: 18 | 19 | class iterador{ 20 | private: 21 | Nodo *aux; 22 | public: 23 | iterador(Nodo * aux = NULL) : aux(aux){} 24 | bool operator !=(iterador it){ return (aux != it.aux); } 25 | T operator *(){ return aux->dato; } 26 | void operator ++(int dummy){ aux = aux->next; } 27 | }; 28 | 29 | public: 30 | ListaCircular():primero(NULL),ultimo(NULL),n(0){} 31 | 32 | ~ListaCircular(){ 33 | 34 | while(primero != NULL){ 35 | Nodo * aux = primero; 36 | primero = primero->next; 37 | delete aux; 38 | } 39 | } 40 | 41 | bool vacio(){ 42 | return (primero == NULL); 43 | } 44 | 45 | iterador first(){ 46 | return primero; 47 | } 48 | 49 | iterador last(){ 50 | return ultimo; 51 | } 52 | 53 | int tamanio(){ return n; } 54 | 55 | bool insertarInicio(T data){ 56 | Nodo * nuevo = new Nodo(data,primero); 57 | if(nuevo == NULL) return false; 58 | if(vacio()){ 59 | primero = ultimo = nuevo; 60 | ultimo->next = primero; 61 | }else{ 62 | ultimo->next = nuevo; 63 | nuevo->next = primero; 64 | primero = nuevo; 65 | } 66 | ++n; 67 | return true; 68 | } 69 | 70 | bool insertarFinal(T data){ 71 | Nodo *nuevo = new Nodo(data,primero); 72 | if(nuevo == NULL) return false; 73 | if(vacio()){ 74 | ultimo = primero = nuevo; 75 | ultimo->next = primero; 76 | }else{ 77 | ultimo->next = nuevo; 78 | ultimo = nuevo; 79 | nuevo->next = primero; 80 | } 81 | ++n; 82 | return true; 83 | } 84 | 85 | bool eliminarInicio(){ 86 | if(vacio()) return false; 87 | if(primero == ultimo){ 88 | primero = ultimo = NULL; 89 | }else{ 90 | Nodo * temp = primero; 91 | primero = primero->next; 92 | ultimo->next = primero; 93 | delete temp; 94 | } 95 | --n; 96 | return true; 97 | } 98 | 99 | bool eliminarFinal(){ 100 | 101 | if(vacio()) return false; 102 | if(primero == ultimo){ 103 | primero = ultimo = NULL; 104 | }else{ 105 | Nodo * temp = primero; 106 | bool seguir = true; 107 | while(seguir){ 108 | if(temp->next == ultimo){ 109 | Nodo *aux = ultimo; 110 | ultimo = temp; 111 | temp->next = primero; 112 | delete aux; 113 | seguir = false; 114 | }else 115 | temp = temp->next; 116 | } 117 | } 118 | --n; 119 | return true; 120 | } 121 | 122 | int EncontrarDato(T dat){ 123 | if(vacio() ) return -1; 124 | Nodo * aux = primero; 125 | int pos = 0 , c = 0 ; 126 | while(c++ < n ){ 127 | if(aux->dato == dat) 128 | return pos; 129 | else{ 130 | ++pos; 131 | aux = aux->next; 132 | } 133 | } 134 | return -1; 135 | } 136 | 137 | }; 138 | 139 | int main(){ 140 | ListaCircular lc; 141 | char x; 142 | 143 | cin>>x; 144 | lc.insertarInicio(x); 145 | cin>>x; 146 | lc.insertarInicio(x); 147 | cin>>x; 148 | lc.insertarInicio(x); 149 | cin>>x; 150 | lc.insertarInicio(x); 151 | cin>>x; 152 | lc.insertarFinal(x); 153 | 154 | lc.eliminarInicio(); 155 | lc.eliminarFinal(); 156 | 157 | ListaCircular::iterador it = lc.first(); 158 | for( ; it != lc.last() ; it++){ 159 | cout<< *it << " "; 160 | } 161 | /* Ultimo elmento no se imprime dentro del for se imprime individual saliendo */ 162 | cout<< *it << " "<oPersona=new Persona();//Se instancia vacio 9 | HashTable[i]->next=NULL;//El siguiente es NULL 10 | } 11 | } 12 | 13 | void hash::Insertar_Datos(){ 14 | 15 | vector nombres; 16 | vector apellidos; 17 | long int dni; 18 | string nom,ape; 19 | Persona* oPersona; 20 | Cargar_Datos(nombres,apellidos); 21 | 22 | for(int i=0;i<10000;++i) 23 | { 24 | 25 | 26 | nom = nombres[rndbtwn(0,nombres.size()-1)] + " " + nombres[rndbtwn(0,nombres.size()-1)]; 27 | ape = apellidos[rndbtwn(0,apellidos.size()-1)] + " " + apellidos[rndbtwn(0,apellidos.size()-1)]; 28 | dni = rndbtwn(10000000,99999999); 29 | oPersona=new Persona(nom,ape,dni); 30 | 31 | //Insercción hash 32 | 33 | int index= Hash(oPersona->GetNombres());//En el index "X" se pone el HASH en string 34 | 35 | if(HashTable[index]->oPersona->GetNombres()=="vacio"){//Si es vacio se agrega el item 36 | 37 | HashTable[index]->oPersona=oPersona; 38 | } 39 | else{ 40 | 41 | item* Puntero=HashTable[index];//El comienzo 42 | item* n= new item; 43 | n->oPersona= oPersona; 44 | n->next=Puntero->next; 45 | Puntero->next = n; 46 | /*while(Puntero->next!=NULL){ 47 | Puntero=Puntero->next; 48 | } 49 | Puntero->next=n;*/ 50 | } 51 | 52 | //Fin insercción hash 53 | 54 | //delete oPersona; 55 | } 56 | 57 | Vaciar_Datos(nombres,apellidos); 58 | } 59 | 60 | 61 | int hash::Numero_Items_Index(int index){ 62 | 63 | int contador=0; 64 | if(HashTable[index]->oPersona->GetNombres()=="vacio"){ 65 | return contador; 66 | } 67 | else{ 68 | contador++; 69 | item *Puntero= HashTable[index]; 70 | while(Puntero->next!=NULL){ 71 | contador++; 72 | Puntero=Puntero->next; 73 | } 74 | } 75 | return contador; 76 | } 77 | 78 | void hash::ImprimirTabla(){ 79 | int number; 80 | for(int i=0;ioPersona->GetNombres()=="vacio"){ 96 | cout<<"Index: "<oPersona!=NULL){ 101 | cout<<"--------------------------\n"; 102 | cout<oPersona->GetNombres()<oPersona->GetApellidos()<oPersona->GetDni()<next; 107 | } 108 | } 109 | } 110 | 111 | Persona* hash::Encontrar_Item(string nombre){ 112 | 113 | int index= Hash(nombre); 114 | 115 | item* Puntero = HashTable[index]; 116 | while(Puntero!=NULL){ 117 | 118 | if(Puntero->oPersona->GetNombres()==nombre){ 119 | return Puntero->oPersona; 120 | } 121 | Puntero=Puntero->next; 122 | } 123 | return NULL; 124 | } 125 | 126 | bool hash::Eliminar_Item(string nombre){ 127 | int index= Hash(nombre);//Se eliminará por nombre, la funcion hash lo convierte en entero y se guarda en el index 128 | item* BorrarPtr; 129 | item* P1; 130 | item* P2; 131 | 132 | //Caso 0 = caja está vacia 133 | //Caso 1 = cuando solo tiene 1 item la caja y tiene el mismo nombre 134 | //Caso 2 = cuando se encuentra el ITEM pero tiene muchos items en la caja(Caja es donde se almacena) o Contenedor 135 | //Caso 3 = La caja contiene elementos pero el primer elemento no es coincidente 136 | //Caso 3.1= no se encuentran 137 | //Caso 3.2= se emparejan 138 | 139 | if(HashTable[index]->oPersona->GetNombres()=="vacio")//Caso 0 140 | { 141 | return false; 142 | } 143 | else if(HashTable[index]->oPersona->GetNombres()==nombre && HashTable[index]->next==NULL){//Caso 1 144 | Persona* aux=HashTable[index]->oPersona; 145 | delete aux; 146 | HashTable[index]->oPersona=NULL; 147 | return true; 148 | } 149 | else if(HashTable[index]->oPersona->GetNombres()==nombre){//Caso 2 150 | 151 | BorrarPtr=HashTable[index]; 152 | HashTable[index]=HashTable[index]->next; 153 | delete BorrarPtr; 154 | return true; 155 | } 156 | else{//Caso 3 157 | P1= HashTable[index]->next; 158 | P2= HashTable[index]; 159 | while(P1!=NULL && P1->oPersona->GetNombres()!=nombre){ 160 | P2=P1; 161 | P1=P1->next; 162 | } 163 | //Caso 3.1 164 | if(P1==NULL){ 165 | return false; 166 | } 167 | //Caso 3.2 168 | else{ 169 | BorrarPtr=P1; 170 | P1=P1->next; 171 | P2->next=P1; 172 | delete BorrarPtr; 173 | return true; 174 | } 175 | } 176 | } 177 | 178 | int hash::Hash(string key){ 179 | int hash=0; 180 | int index; 181 | 182 | for(int i=0;i 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | long int rndbtwn(long int a, long int b) 11 | { 12 | return rand() % (b - a + 1) + a; 13 | } 14 | 15 | class Persona{ 16 | 17 | string nombres; 18 | string apellidos; 19 | long int dni; 20 | 21 | public: 22 | 23 | Persona(string nombres="vacio", string apellidos="vacio", long int dni=0):nombres(nombres),apellidos(apellidos),dni(dni){} 24 | string GetNombres(){return nombres;} 25 | string GetApellidos(){return apellidos;} 26 | long int GetDni(){return dni;} 27 | void Imprimir(){cout<& nombres, vector& apellidos) 31 | { 32 | string dato; 33 | ifstream fn("Nombres.txt"); 34 | while(getline(fn,dato)){nombres.push_back(dato);} 35 | fn.close(); 36 | ifstream fa("Apellidos.txt"); 37 | while(getline(fa,dato)){apellidos.push_back(dato);} 38 | fa.close(); 39 | } 40 | 41 | void Vaciar_Datos(vector& nombres, vector& apellidos) 42 | { 43 | nombres.clear(); 44 | apellidos.clear(); 45 | } 46 | 47 | //Arreglo 48 | 49 | void Insertar_Datos_Arr(Persona *Arreglo[10000]) 50 | { 51 | vector nombres; 52 | vector apellidos; 53 | long int dni; 54 | string nom,ape; 55 | 56 | Cargar_Datos(nombres,apellidos); 57 | 58 | for(int i=0;i<10000;++i) 59 | { 60 | 61 | Persona* oPersona; 62 | nom = nombres[rndbtwn(0,nombres.size()-1)] + " " + nombres[rndbtwn(0,nombres.size()-1)]; 63 | ape = apellidos[rndbtwn(0,apellidos.size()-1)] + " " + apellidos[rndbtwn(0,apellidos.size()-1)]; 64 | dni = rndbtwn(10000000,99999999); 65 | oPersona=new Persona(nom,ape,dni); 66 | Arreglo[i]=oPersona; 67 | delete oPersona; 68 | } 69 | 70 | Vaciar_Datos(nombres,apellidos); 71 | } 72 | 73 | Persona* Buscar_Datos_Arr(string dato, Persona *Arreglo[10000]) 74 | { 75 | for(int i=0;i<10000;++i) 76 | { 77 | if(Arreglo[i]->GetNombres() == dato) 78 | { 79 | return Arreglo[i]; 80 | } 81 | } 82 | 83 | return NULL; 84 | } 85 | 86 | bool Eliminar_Datos_Arr(string nombre, Persona *Arreglo[10000]) 87 | { 88 | Persona *Arr_aux[10000-1]; 89 | Persona *aux; 90 | bool eliminado=false;; 91 | 92 | if(Buscar_Datos_Arr(nombre,Arreglo)==NULL) 93 | { 94 | return false; 95 | } 96 | else{ 97 | 98 | for(int i=0;i<10000;++i) 99 | { 100 | if(Arreglo[i]->GetNombres()==nombre && eliminado==false) 101 | { 102 | aux=Arreglo[i]; 103 | delete aux; 104 | eliminado=true; 105 | } 106 | else if(eliminado==false) 107 | { 108 | Arr_aux[i]=Arreglo[i]; 109 | } 110 | else 111 | { 112 | Arr_aux[i-1]=Arreglo[i]; 113 | } 114 | } 115 | 116 | } 117 | return true; 118 | } 119 | 120 | //Lista simple enlazada 121 | 122 | template 123 | 124 | class ListaSimple{ 125 | struct Nodo 126 | { 127 | Nodo*next; 128 | Persona *elemento; 129 | Nodo(Persona *elemento = NULL , Nodo*next = NULL):elemento(elemento),next(next){} 130 | }; 131 | int n; 132 | Nodo *inicio; 133 | 134 | public: 135 | 136 | class Iterador{ 137 | 138 | Nodo*aux; 139 | 140 | public: 141 | 142 | Iterador(Nodo *aux = NULL): aux(aux){} 143 | bool operator !=(Iterador it){ return aux != it.aux; } 144 | T operator *(){ return aux->elemento; } 145 | void operator ++(int dummy){ aux = aux->next; } 146 | }; 147 | 148 | Iterador Primero() 149 | { 150 | return inicio; 151 | } 152 | 153 | Iterador Ultimo() 154 | { 155 | return NULL; 156 | } 157 | 158 | public: 159 | 160 | ListaSimple():n(0),inicio(NULL){} 161 | ~ListaSimple(){ 162 | while(inicio != NULL){ 163 | Nodo *aux = inicio; 164 | inicio = inicio->next; 165 | delete aux; 166 | } 167 | } 168 | 169 | void Insertar_Datos_Inicio() 170 | { 171 | vector nombres; 172 | vector apellidos; 173 | long int dni; 174 | string nom,ape; 175 | 176 | Cargar_Datos(nombres,apellidos); 177 | 178 | for(int i=0;i<10000;++i) 179 | { 180 | Persona* data; 181 | nom = nombres[rndbtwn(0,nombres.size()-1)] + " " + nombres[rndbtwn(0,nombres.size()-1)]; 182 | ape = apellidos[rndbtwn(0,apellidos.size()-1)] + " " + apellidos[rndbtwn(0,apellidos.size()-1)]; 183 | dni = rndbtwn(10000000,99999999); 184 | data=new Persona(nom,ape,dni); 185 | Nodo*nuevo = new Nodo(data,inicio); 186 | inicio = nuevo; 187 | ++n; 188 | delete data,nuevo; 189 | } 190 | 191 | Vaciar_Datos(nombres,apellidos); 192 | } 193 | 194 | void Insertar_Datos_Final() 195 | { 196 | vector nombres; 197 | vector apellidos; 198 | long int dni; 199 | string nom,ape; 200 | 201 | Cargar_Datos(nombres,apellidos); 202 | 203 | for(int i=0;i<10000;++i) 204 | { 205 | Persona* data; 206 | nom = nombres[rndbtwn(0,nombres.size()-1)] + " " + nombres[rndbtwn(0,nombres.size()-1)]; 207 | ape = apellidos[rndbtwn(0,apellidos.size()-1)] + " " + apellidos[rndbtwn(0,apellidos.size()-1)]; 208 | dni = rndbtwn(10000000,99999999); 209 | data=new Persona(nom,ape,dni); 210 | 211 | if(n==0){ 212 | Nodo*nuevo = new Nodo(data,inicio); 213 | inicio = nuevo; 214 | ++n; 215 | delete nuevo; 216 | } 217 | else{ 218 | 219 | Nodo *aux = inicio; int c = 1; 220 | while(c++ < n) 221 | { 222 | aux = aux->next; 223 | } 224 | Nodo *nuevo = new Nodo(data,aux->next); 225 | aux->next = nuevo; 226 | ++n; 227 | delete nuevo; 228 | } 229 | delete data; 230 | } 231 | 232 | Vaciar_Datos(nombres,apellidos); 233 | } 234 | 235 | bool Eliminar_Inicio() 236 | { 237 | if(n == 0) return false; 238 | Nodo *aux = inicio; 239 | inicio = inicio->next; 240 | delete aux; 241 | --n; 242 | return true; 243 | } 244 | 245 | bool Eliminar_Posicion(int pos) 246 | { 247 | if(pos < 0 || pos > n ) return false; 248 | if(pos == 0) { Eliminar_Inicio(); return true; } 249 | int c = 1; Nodo*aux = inicio; 250 | while(c++ < pos) 251 | { 252 | aux = aux->next; 253 | } 254 | Nodo *aux2 = aux->next; 255 | aux->next = aux2->next; 256 | delete aux2; 257 | --n; 258 | return true; 259 | } 260 | 261 | bool Eliminar_Final(){ 262 | Eliminar_Posicion(n-1); 263 | } 264 | 265 | bool Eliminar_Datos(string data){ 266 | 267 | int cont=-1,pos=-1; 268 | Nodo*aux = inicio; 269 | 270 | while(aux != NULL){ 271 | 272 | if(aux->elemento->GetNombres() == data){ 273 | pos=cont; 274 | } 275 | else{ 276 | aux = aux->next; 277 | cont++; 278 | } 279 | } 280 | 281 | if(pos != -1){ 282 | Eliminar_Posicion(pos); 283 | return true; 284 | } 285 | else{ 286 | return false; 287 | } 288 | 289 | } 290 | 291 | Persona* Recuperar_Dato(string data){ 292 | 293 | Nodo*aux = inicio; 294 | while(aux != NULL){ 295 | 296 | if(aux->elemento->GetNombres() == data){ 297 | 298 | return aux->elemento; 299 | } 300 | else{ 301 | aux = aux->next; 302 | } 303 | } 304 | 305 | return NULL; 306 | } 307 | 308 | }; 309 | 310 | -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/Nombres.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad2/TParcialFinalito/Nombres.txt -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/m.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad2/TParcialFinalito/m.exe -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/main.cpp: -------------------------------------------------------------------------------- 1 | #include "hash.cpp" 2 | #include 3 | #include 4 | 5 | Persona *arreglo[10000]; 6 | hash *tabla = new hash(); 7 | ListaSimple ls; 8 | 9 | using namespace std; 10 | ofstream lout("salida.js"); 11 | 12 | double iht = 0; 13 | double ile = 0; 14 | double iar = 0; 15 | 16 | double eht = 0; 17 | double ele = 0; 18 | double ear = 0; 19 | 20 | double bht = 0; 21 | double ble = 0; 22 | double bar = 0; 23 | 24 | stringstream sht,sle,sar; 25 | stringstream sht1,sle1,sar1; 26 | stringstream sht2,sle2,sar2; 27 | unsigned t0, t1; 28 | 29 | 30 | int main(){ 31 | 32 | srand(time(0)); 33 | 34 | /* HASH TABLES */ 35 | t0=clock(); 36 | tabla->Insertar_Datos(); 37 | t1 = clock(); 38 | iht = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 39 | 40 | t0=clock(); 41 | tabla->Eliminar_Item("Tito Susana"); 42 | t1 = clock(); 43 | eht = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 44 | 45 | t0=clock(); 46 | tabla->Encontrar_Item("Eva Susana"); 47 | t1 = clock(); 48 | bht = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 49 | 50 | /*----------------------------*/ 51 | 52 | 53 | /* ARREGLOS */ 54 | t0=clock(); 55 | Insertar_Datos_Arr(arreglo); 56 | t1 = clock(); 57 | iar = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 58 | 59 | t0=clock(); 60 | Eliminar_Datos_Arr("Tito Susana",arreglo); 61 | t1 = clock(); 62 | ear = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 63 | 64 | t0=clock(); 65 | Buscar_Datos_Arr("Eva Susana",arreglo); 66 | t1 = clock(); 67 | bar = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 68 | 69 | /*----------------------------*/ 70 | 71 | 72 | /* LISTAS ENLAZADAS */ 73 | t0=clock(); 74 | ls.Insertar_Datos_Inicio(); 75 | t1 = clock(); 76 | ile = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 77 | 78 | t0=clock(); 79 | ls.Eliminar_Datos("Tito Susana"); 80 | t1 = clock(); 81 | ele = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 82 | 83 | t0=clock(); 84 | ls.Recuperar_Dato("Eva Susana"); 85 | t1 = clock(); 86 | ble = (double(t1-t0)/CLOCKS_PER_SEC)*1000; 87 | 88 | /*----------------------------*/ 89 | 90 | 91 | 92 | tabla->ImprimirTabla(); 93 | 94 | 95 | sht< " << iht <<" milisegundos"< " << ile <<" milisegundos"< " << iar <<" milisegundos"< " << eht <<" milisegundos"< " << ele <<" milisegundos"< " << ear <<" milisegundos"< " << bht <<" milisegundos"< " << ble <<" milisegundos"< " << bar <<" milisegundos"< 152 | 153 | 154 | 155 | */ 156 | -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/output.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad2/TParcialFinalito/output.txt -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/salida.js: -------------------------------------------------------------------------------- 1 | var ht = '26'; 2 | var le = '21'; 3 | var ar = '20'; 4 | var ht1 = '0'; 5 | var le1 = '1'; 6 | var ar1 = '2'; 7 | var ht2 = '0'; 8 | var le2 = '2'; 9 | var ar2 = '1'; 10 | -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/script.js: -------------------------------------------------------------------------------- 1 | function viewGraph(){ 2 | 3 | var p1 = '79%'; 4 | var p2 = '51%'; 5 | var p3 = '45%'; 6 | 7 | document.getElementById("p1").innerHTML = p1; 8 | document.getElementById("p2").innerHTML = p2; 9 | document.getElementById("p3").innerHTML = p3; 10 | 11 | $('.column').css('height','0'); 12 | 13 | $('table tr').each(function(index) { 14 | var ha = $(this).children('td').eq(1).text(); 15 | $('#col'+index).animate({height: ha}, 1500).html("
"+ha+"
"); 16 | 17 | }); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/slides.txt.txt: -------------------------------------------------------------------------------- 1 | https://slides.com/enzolizama -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/stats.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Proyecto Parcial 5 | 6 | 7 | 8 | 9 |
10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 |
Eficiencia en tiempos
Hash Table_
Listas Enlazadas_
Arreglos_
21 | 22 | 23 |
Insertar Datos

24 |
Eliminar Datos

25 |
Buscar Datos

26 |
27 | 28 |
29 |
30 |
0,075
31 |
0,050
32 |
0,025
33 |
34 |
35 |
36 |
37 |
38 | 39 |
40 |
41 | 42 | 94 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /Unidad2/TParcialFinalito/style.css: -------------------------------------------------------------------------------- 1 | #contenitore{ 2 | position: relative; 3 | width: 800px; 4 | margin: 20px auto; 5 | text-align:center; 6 | overflow:hidden; 7 | font: 14px 'Trebuchet MS', sans serif; 8 | } 9 | .left{ 10 | float:left; 11 | width:48%; 12 | } 13 | #grafico{ 14 | position:relative; 15 | height:300px; 16 | border-left:2px solid #000000; 17 | border-bottom: 2px solid #000000; 18 | width:100%; 19 | margin-top:20px; 20 | } 21 | .riga{ 22 | position:absolute; 23 | left:0; 24 | height: 1px; 25 | background-color:gray; 26 | width: 100%; 27 | } 28 | .riga div{ 29 | float:left; 30 | margin: -8px 0 0 -40px; 31 | } 32 | .canc{ 33 | clear:both; 34 | } 35 | table{ 36 | width:60%; 37 | background-color: white; 38 | color: #000; 39 | margin: 1em auto; 40 | } 41 | table caption{ 42 | background-color: #D8EED8; 43 | color: #004156; 44 | text-align: left; 45 | } 46 | table tr:nth-child(2n){ 47 | background-color: #D8EED8; 48 | } 49 | table tr:nth-child(2n+1){ 50 | background-color: #BFDFFF; 51 | } 52 | table td{ 53 | text-align:center; 54 | border-bottom: 1px solid #CDCDCD; 55 | padding: 6px; 56 | } 57 | .column{ 58 | position:absolute; 59 | width: 16%; 60 | bottom: 0; 61 | background-color: #003366; 62 | margin-left:5%; 63 | } 64 | div.button { 65 | margin: 0 auto; 66 | text-align: center; 67 | width: 100px; 68 | background-color:#003366; 69 | border: 1px solid #003366; 70 | border-radius: 5px; 71 | padding: 8px; 72 | color: #E1E2CF; 73 | cursor: pointer; 74 | } 75 | .column div{ 76 | margin-top:-20px; 77 | height:20px; 78 | } 79 | -------------------------------------------------------------------------------- /Unidad3/Ordenamiento/a.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad3/Ordenamiento/a.out -------------------------------------------------------------------------------- /Unidad3/Ordenamiento/p: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad3/Ordenamiento/p -------------------------------------------------------------------------------- /Unidad3/Ordenamiento/p1: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/enzoftware/algorithms-and-data-structures/0f955a1632caaeba33ca3021d3dcbcfd5c3bd051/Unidad3/Ordenamiento/p1 -------------------------------------------------------------------------------- /Unidad3/Ordenamiento/p10327.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | 7 | int main(){ 8 | 9 | int n; 10 | while(cin>>n){ 11 | vector v; 12 | int count = 0,aux; 13 | for(int i = 0 ; i>aux; 15 | v.push_back(aux); 16 | } 17 | 18 | for(int i = n-1 ; i>=0 ; i--){ 19 | for(int j = 0 ; j< i ; j++){ 20 | if(v[j] > v[j+1]){ 21 | swap(v[j],v[j+1]); 22 | count++; 23 | } 24 | } 25 | } 26 | 27 | cout<<"Minimum exchange operations : "< 2 | #include 3 | #include 4 | using namespace std; 5 | 6 | char letters[9] = "abcdefgh" , aux[9]; 7 | int test; 8 | 9 | void programline(int x , int y){ 10 | if(x == test){ 11 | for (int i = 0; i < y; i++){ 12 | cout<<" "; 13 | } 14 | cout<<"writeln("; 15 | for(int i = 0 ; i < test-1 ; i++){ 16 | cout<= 0 ; i--){ 23 | if(i == x) aux[i] = letters[x]; 24 | else swap(aux[i],aux[i+1]); 25 | if(x>0) 26 | if(i==x){ 27 | for (int k = 0; k < y; k++) cout << " "; 28 | cout << "if " << aux[i - 1] << " < " << aux[i] << " then" << endl; 29 | }else { 30 | for (int k = 0; k < y; k++) cout << " "; 31 | if (i>0) cout << "else if " << aux[i - 1] << " < " << aux[i] << " then" << endl; 32 | else cout << "else" << endl; 33 | } 34 | programline(x+1,y+2); 35 | } 36 | 37 | for(int i = 0 ; i>numcases; 44 | 45 | while(numcases--){ 46 | cin>>test; 47 | cout<<"program sort(input,output);"<0) cout< 6 | using namespace std; 7 | 8 | void piramide(int i , int j){ 9 | int auxi = 0 ; int auxj =0; 10 | if(i>0){ 11 | cout<<"*"; 12 | piramide(--i,j); 13 | 14 | }else{ 15 | if(j>0){ 16 | i = j; 17 | cout< 6 | #include 7 | using namespace std; 8 | 9 | void piramide(int numcar , string entrada , string actual){ 10 | if(actual.length() == numcar){ 11 | cout< 7 | using namespace std; 8 | typedef unsigned long long ull 9 | ull invertirNumero(ull n , ull aux){ 10 | if(n/10==0){ 11 | return aux + n; 12 | }else{ 13 | 14 | } 15 | } 16 | 17 | int main(int argc, char const *argv[]){ 18 | 19 | 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Unidad3/parcial pasado/Apellidos.txt: -------------------------------------------------------------------------------- 1 | Gonzales 2 | Guadalupe 3 | Medina 4 | Lopez 5 | Lizama 6 | Paredes 7 | Hurtado 8 | Vives 9 | Canaval 10 | Salas 11 | Ruiz 12 | Navarro 13 | Huaman 14 | Villaroel 15 | De la Cruz 16 | Barrientos 17 | Kopecek 18 | Rios 19 | Alosilla 20 | Lajo 21 | Sotomayor -------------------------------------------------------------------------------- /Unidad3/parcial pasado/Nombres.txt: -------------------------------------------------------------------------------- 1 | Rodrigo 2 | Luis 3 | Carlos 4 | Cesar 5 | Andres 6 | Andre 7 | Enzo 8 | Alberto 9 | Julio 10 | Anthony 11 | Rosa 12 | Maria 13 | Silvio 14 | Lizbeth 15 | Luisa 16 | Marcos 17 | Martin 18 | John 19 | Martha 20 | Naomy 21 | Jovanna 22 | Eloy 23 | Jose 24 | Juan -------------------------------------------------------------------------------- /Unidad3/parcial pasado/Productos.txt: -------------------------------------------------------------------------------- 1 | Tamal 2 | Huevo 3 | Papas 4 | Chocolate 5 | Galleta 6 | Torta 7 | Donas 8 | Caramelos 9 | Manzana 10 | Platano 11 | Pera 12 | Uva 13 | Pan 14 | Arroz 15 | Queso 16 | Leche 17 | Yogurt 18 | Azucar -------------------------------------------------------------------------------- /Unidad3/parcial pasado/ep_pasado.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class Cliente{ 11 | int id,precio; 12 | string nombre,apellido,producto; 13 | public: 14 | Cliente(int id = 0 , string nombre = "" , string apellido = "" , string producto ="", int precio = 0):id(id),nombre(nombre),apellido(apellido),producto(producto),precio(precio){} 15 | int getID(){return id;} 16 | int getPrecio(){return precio;} 17 | string getNombre(){return nombre;} 18 | string getApellido(){return apellido;} 19 | string getProducto(){return producto;} 20 | }; 21 | 22 | class Lista{ 23 | 24 | struct Nodo{ 25 | Nodo*next; 26 | Cliente*elemento; 27 | Nodo(Cliente*elemento = NULL , Nodo*next = NULL):elemento(elemento),next(next){} 28 | }; 29 | Nodo*ini; 30 | Nodo*fin; 31 | int n; 32 | 33 | public: 34 | Lista():ini(NULL),n(0){} 35 | ~Lista(){ 36 | while(ini != NULL){ 37 | Nodo*aux = ini; 38 | ini = ini->next; 39 | delete aux; 40 | } 41 | } 42 | 43 | bool insertar(Cliente* dato){ 44 | Nodo* nuevo = new Nodo(dato,ini); 45 | ini = nuevo; 46 | n++; 47 | } 48 | 49 | int sumaPrecioC(string nombre , string apellido){ 50 | Nodo*aux = ini; 51 | int suma = 0 ; 52 | for(int i = 0 ; ielemento->getNombre() == nombre && aux->elemento->getApellido() == apellido){ 54 | suma += aux->elemento->getPrecio(); 55 | } 56 | aux = aux->next; 57 | } 58 | return suma; 59 | } 60 | 61 | Cliente * encontrarCvip(){ 62 | Nodo*aux = ini; 63 | int mayor = 0 ; 64 | string nvip = "vacio"; 65 | string avip = "vacio"; 66 | for(int i = 0 ; ielemento->getNombre(),aux->elemento->getApellido()) > mayor){ 68 | nvip = aux->elemento->getNombre(); 69 | avip = aux->elemento->getApellido(); 70 | mayor = sumaPrecioC(aux->elemento->getNombre(),aux->elemento->getApellido()); 71 | } 72 | aux = aux->next; 73 | } 74 | if(nvip != "vacio" && avip != "vacio" && mayor != 0){ 75 | Cliente * c = new Cliente(0,nvip,avip,"",mayor); 76 | return c; 77 | } 78 | 79 | return NULL; 80 | } 81 | 82 | int sumaPrecProd(string prod){ 83 | Nodo *auxi = ini; 84 | int suma = 0 ; 85 | for(int i = 0 ; ielemento->getProducto() == prod){ 87 | suma += auxi->elemento->getPrecio(); 88 | } 89 | auxi = auxi->next; 90 | } 91 | return suma; 92 | } 93 | 94 | Cliente * enconPvip(){ 95 | Nodo*aux = ini; 96 | string prodvip = "vacio"; 97 | int mayor = 0; 98 | for(int i = 0 ; ielemento->getProducto()) > mayor){ 100 | prodvip = aux->elemento->getProducto(); 101 | mayor = sumaPrecProd(aux->elemento->getProducto()); 102 | } 103 | aux = aux->next; 104 | } 105 | 106 | if(prodvip != "vacio" && mayor != 0){ 107 | Cliente * c = new Cliente(0,"","",prodvip,mayor); 108 | return c; 109 | } 110 | 111 | return NULL; 112 | } 113 | 114 | }; 115 | 116 | 117 | int randbtn(int a , int b){ 118 | return rand()%(b-a+1)+a; 119 | } 120 | 121 | 122 | void LoadDatos(vector& nombres ,vector& apellidos ,vector& productos ){ 123 | string dato; 124 | ifstream ifn("Nombres.txt"); 125 | while(getline(ifn,dato)) nombres.push_back(dato); 126 | ifn.close(); 127 | ifstream ifa("Apellidos.txt"); 128 | while(getline(ifa,dato)) apellidos.push_back(dato); 129 | ifa.close(); 130 | ifstream ifp("Productos.txt"); 131 | while(getline(ifp,dato)) productos.push_back(dato); 132 | ifp.close(); 133 | } 134 | 135 | void GenerateDatos(int n){ 136 | vector nombres; 137 | vector apellidos; 138 | vector productos; 139 | LoadDatos(nombres,apellidos,productos); 140 | string name,apel,prod; 141 | int id, prc; 142 | ofstream ofi("input.txt"); 143 | for(int i = 0 ; igetNombre()<<" "<getApellido()<<" ($ "<getPrecio()<<" ) "<getProducto()<<" ($"<getPrecio()<<")"<