├── .gitignore ├── LICENSE ├── README.md ├── c ├── 01-stack-heap ├── 01-stack-heap.c ├── 02-pointers-addr ├── 02-pointers-addr.c ├── 03-array ├── 03-array.c ├── 04-2d-array ├── 04-2d-array.c ├── 05-dyn-array ├── 05-dyn-array.c ├── 06-binary-search ├── 06-binary-search.c ├── 07-sorts ├── 07-sorts.c ├── 08-2d-major-order ├── 08-2d-major-order.c ├── 09-3d-major-order ├── 09-3d-major-order.c ├── 10-recursion ├── 10-recursion.c ├── 11-structs ├── 11-structs.c ├── 12-linked-list └── 12-linked-list.c ├── cpp ├── class-diagram │ └── mitologico │ │ ├── MundoMitologico.cpp │ │ ├── MundoMitologico.jpg │ │ ├── README.md │ │ └── output │ │ └── MundoMitologico ├── linear-structures │ ├── ArrayListMod.cpp │ ├── LinkedList.cpp │ └── output │ │ ├── ArrayListMod │ │ ├── LinkedList │ │ └── SparseMatrix ├── merkle │ ├── dot.png │ ├── merkle.cpp │ └── output │ │ └── merkle ├── misc │ ├── major-order │ │ ├── col-major-2d.cpp │ │ ├── col-major-3d.cpp │ │ ├── output │ │ │ ├── col-major-2d │ │ │ ├── col-major-3d │ │ │ ├── row-major-2d │ │ │ └── row-major-3d │ │ ├── row-major-2d.cpp │ │ └── row-major-3d.cpp │ ├── recursive │ │ ├── output │ │ │ └── recursive │ │ └── recursive.cpp │ ├── sha256 │ │ ├── output │ │ │ └── sha │ │ ├── sha.cpp │ │ └── sha.h │ └── sort │ │ ├── output │ │ └── sort │ │ └── sort.cpp ├── non-linear-structures │ ├── avl.cpp │ ├── bst.cpp │ ├── btree.cpp │ ├── btree.png │ ├── graph.cpp │ ├── hash.cpp │ ├── output │ │ ├── README.me │ │ ├── avl │ │ ├── bst │ │ ├── btree │ │ ├── graph │ │ ├── hash │ │ └── uniformcost │ └── uniformcost.cpp └── sparse-matrix │ ├── DynamicOrthoSparseMatrix.cpp │ ├── DynamicSparseMatrix.cpp │ ├── StaticSparseMatrix.cpp │ └── output │ ├── DynamicOrthoSparseMatrix │ ├── DynamicSparseMatrix │ ├── Orthogonal │ └── StaticSparseMatrix ├── csharp ├── 01-miscellaneous │ ├── 01-StackHeap │ │ ├── Program.cs │ │ ├── StackHeap.csproj │ │ └── run.png │ ├── 02-DynArray │ │ ├── DynArray.csproj │ │ ├── Program.cs │ │ └── run.png │ ├── 03-MajorOrder2D │ │ ├── MajorOrder2D.csproj │ │ ├── Program.cs │ │ └── run.png │ ├── 04-MythologyClasses │ │ ├── Program.cs │ │ ├── mythology.jpg │ │ └── run.png │ ├── 05-Sort │ │ ├── Program.cs │ │ └── run.png │ ├── 06-Recursive │ │ ├── Program.cs │ │ └── run.png │ └── 07-BinarySearch │ │ └── Program.cs ├── 02-linear-structures │ ├── List │ │ ├── List.csproj │ │ ├── Program.cs │ │ └── run.png │ └── SparseMatrix │ │ ├── Program.cs │ │ ├── SparseMatrix.csproj │ │ └── dotnet-run.png ├── 03-tree-structures │ ├── AVL │ │ ├── Program.cs │ │ └── dotnet-run.png │ ├── BSTree │ │ ├── Program.cs │ │ └── dotnet-run.png │ ├── BTree │ │ ├── Program.cs │ │ ├── btree.png │ │ └── dotnet-run.png │ └── ListBSTree │ │ ├── ListBSTree.png │ │ ├── Program.cs │ │ └── dotnet-run.png ├── 04-nonlinear-structures │ ├── Crypto │ │ ├── Program.cs │ │ └── dotnet-run.png │ ├── Graph │ │ ├── Dijkstra │ │ │ ├── Program.cs │ │ │ ├── dotnet-run.png │ │ │ └── graph.png │ │ ├── Search │ │ │ ├── Program.cs │ │ │ ├── dotnet-run.png │ │ │ └── graph.png │ │ ├── Traversal │ │ │ ├── Program.cs │ │ │ ├── dotnet-run.png │ │ │ └── graph.png │ │ └── UniformCost │ │ │ ├── Program.cs │ │ │ ├── dotnet-run.png │ │ │ └── graph.png │ ├── Hash │ │ ├── Program.cs │ │ └── dotnet-run.png │ ├── Huffman │ │ ├── Program.cs │ │ ├── dotnet-run.png │ │ └── huffman.png │ └── Merkle │ │ ├── Hash.cs │ │ ├── Program.cs │ │ ├── dotnet-run.png │ │ └── merkle.png ├── README.md └── img │ ├── screenshot1.png │ ├── screenshot2.png │ └── screenshot3.png ├── fortran ├── README.md ├── linear-structures │ └── linkedlist │ │ ├── README.md │ │ ├── list.f90 │ │ └── listdef.f90 ├── non-linear-structures │ ├── avl │ │ ├── README.md │ │ ├── avl.f90 │ │ ├── avldef.f90 │ │ ├── output.dot │ │ └── output.svg │ ├── bst │ │ ├── README.md │ │ ├── bst.f90 │ │ ├── bstdef.90 │ │ ├── output.dot │ │ └── output.svg │ ├── btree │ │ ├── btree.f90 │ │ ├── btree.png │ │ └── execution.png │ ├── graph │ │ ├── longest_path.f90 │ │ ├── longest_path_execution.png │ │ ├── longest_path_graph.png │ │ ├── traversal.f90 │ │ ├── traversal_output.png │ │ ├── uniform_cost.f90 │ │ ├── uniform_cost_input_graph.png │ │ └── uniform_cost_output.png │ ├── hash │ │ ├── execution.png │ │ └── hash.f90 │ └── merkle │ │ ├── execution.png │ │ ├── merkle.f90 │ │ ├── output.dot │ │ └── output.svg ├── parser │ ├── parser │ ├── parser.f90 │ └── parser.mod ├── sha256 │ ├── output.png │ └── sha256.f90 ├── test │ ├── test01.f90 │ └── test01_execution.png └── xdialog │ └── xreadnwrite │ ├── console.png │ ├── output.txt │ ├── xreadnwrite │ ├── xreadnwrite.f90 │ └── xreadnwrite.png ├── go ├── class-diagram │ └── mitologico │ │ ├── MundoMitologico.go │ │ ├── MundoMitologico.jpg │ │ └── README.md ├── linear-structures │ ├── arraylistmod.go │ └── list.go ├── nonlinear-structures │ ├── bstree-of-lists.go │ ├── bstree-of-lists.png │ └── bstree.go ├── order │ ├── rowmajor2d.go │ └── rowmajor3d.go ├── pointers │ └── pointers.go └── sparse-matrix │ └── sparse.go ├── java ├── algorithms │ ├── Recursion.java │ ├── Sort.java │ ├── majororder2d.java │ └── majororder3d.java ├── class-diagram │ └── mitologico │ │ ├── MundoMitologico.java │ │ ├── MundoMitologico.jpg │ │ └── README.md ├── linear-structures │ ├── ArrayListMod.java │ ├── List.java │ ├── Queue.java │ ├── SparseMatrix.java │ ├── Stack.java │ └── StackList.java ├── non-linear-structures │ └── hash │ │ └── Hash.java └── tree-structures │ ├── AVL.java │ ├── BST.java │ └── BST.png ├── javascript ├── README.md ├── class-diagram │ └── mitologico │ │ ├── README.md │ │ ├── mitologico.html │ │ ├── mitologico.jpg │ │ └── mitologico.js ├── graph │ ├── breadth_depth_search.html │ ├── breadth_depth_search.js │ ├── breadth_depth_search.png │ ├── uniform_cost.html │ ├── uniform_cost.js │ ├── uniform_cost.png │ └── vis-network.min.js ├── lineal-structures │ ├── array-list-mod.html │ ├── array-list-mod.js │ ├── list.html │ └── list.js └── nonlineal-structures │ ├── avl.html │ ├── avl.js │ ├── avlinmuta.html │ ├── avlinmuta.js │ ├── bst.html │ ├── bst.js │ ├── merkle.html │ ├── merkle.js │ └── vis-network.min.js ├── lisp ├── README.md ├── breadth-search.lisp ├── breadth-search.png ├── depth-search.lisp └── depth-search.png ├── python ├── README.md ├── linear-structures │ └── list.py └── nonlinear-structures │ ├── graph │ ├── anchura_profundidad.py │ ├── costo_uniforme.py │ └── dijkstra.py │ └── tree │ ├── avltree.py │ ├── bstree.py │ ├── bstree_gviz.py │ ├── bstree_of_lists.png │ └── bstree_of_lists.py └── rust ├── 01-stack-heap ├── 01-stack-heap.png ├── 01-stack-heap.rs ├── 11-structs ├── 11-structs.png ├── 11-structs.rs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .vscode/settings.json 3 | .vscode/tasks.json 4 | cpp/lineal-structures/LinkedList.exe 5 | .vscode/c_cpp_properties.json 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Luis Espino 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![C](https://img.shields.io/badge/C-00599C?style=flat&logo=c&logoColor=white) ![C++](https://img.shields.io/badge/C%2B%2B-00599C?style=flat&logo=c%2B%2B&logoColor=white) ![C#](https://img.shields.io/badge/C%23-239120?style=flat&logo=csharp&logoColor=white) ![Fortran](https://img.shields.io/badge/Fortran-005F9E?style=flat&logo=fortran&logoColor=white) ![Go](https://img.shields.io/badge/Go-00ADD8?style=flat&logo=go&logoColor=white) ![Java](https://img.shields.io/badge/Java-007396?style=flat&logo=java&logoColor=white) ![JavaScript](https://img.shields.io/badge/JavaScript-F7DF1E?style=flat&logo=javascript&logoColor=black) ![Lisp](https://img.shields.io/badge/Lisp-3e9f65?style=flat&logo=lisp&logoColor=white) ![Python](https://img.shields.io/badge/Python-3776AB?style=flat&logo=python&logoColor=white) ![Rust](https://img.shields.io/badge/Rust-000000?style=flat&logo=rust&logoColor=white) 2 | 3 | 4 | # Data Structures 5 | 6 | ### Examples of Programming with Abstract Data Types (ADTs) 7 | - Algorithms 8 | - Linked Lists 9 | - Stacks and Queues 10 | - Trees 11 | - Graphs 12 | - Hash Tables 13 | - Huffman Coding 14 | - Cryptography 15 | - Blockchain 16 | 17 | [JavaScript Examples](javascript) 18 | -------------------------------------------------------------------------------- /c/01-stack-heap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/01-stack-heap -------------------------------------------------------------------------------- /c/01-stack-heap.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() { 5 | int stackVar = 10; 6 | printf("Stack value: %d\n", stackVar); 7 | 8 | int *heapVar = (int *)malloc(sizeof(int)); 9 | *heapVar = 20; 10 | printf("Heap value: %d\n", *heapVar); 11 | free(heapVar); 12 | 13 | return 0; 14 | } -------------------------------------------------------------------------------- /c/02-pointers-addr: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/02-pointers-addr -------------------------------------------------------------------------------- /c/02-pointers-addr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int *a, b, **c, *d; 6 | b = 10; 7 | a = &b; 8 | c = &a; 9 | d = a; 10 | 11 | printf("b %d\n", b); 12 | printf("&b %p\n", (void*)&b); 13 | printf("a %p\n", (void*)a); 14 | printf("&a %p\n", (void*)&a); 15 | printf("*a %d\n", *a); 16 | printf("c %p\n", (void*)c); 17 | printf("&c %p\n", (void*)&c); 18 | printf("*c %p\n", (void*)*c); 19 | printf("**c %d\n", **c); 20 | printf("*&c %p\n", (void*)*&c); 21 | printf("&*c %p\n", (void*)&*c); 22 | printf("d %p\n", (void*)d); 23 | printf("&d %p\n", (void*)&d); 24 | printf("*d %d\n", *d); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c/03-array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/03-array -------------------------------------------------------------------------------- /c/03-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[5]; 6 | 7 | for (int i = 0; i < 5; i++) 8 | a[i] = i; 9 | 10 | for (int i = 0; i < 5; i++) 11 | printf("%d \n", a[i]); 12 | 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /c/04-2d-array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/04-2d-array -------------------------------------------------------------------------------- /c/04-2d-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() 4 | { 5 | int a[5][5]; 6 | int v = 0; 7 | 8 | for (int i = 0; i < 5; i++) 9 | for (int j = 0; j < 5; j++) 10 | a[i][j] = v++; 11 | 12 | for (int i = 0; i < 5; i++) { 13 | for (int j = 0; j < 5; j++) 14 | printf("%02d ", a[i][j]); 15 | printf("\n"); 16 | } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /c/05-dyn-array: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/05-dyn-array -------------------------------------------------------------------------------- /c/05-dyn-array.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main() 5 | { 6 | int n; 7 | printf("Enter size: "); 8 | scanf("%d", &n); 9 | 10 | int *a = (int *)malloc(n * sizeof(int)); 11 | 12 | for (int i = 0; i < n; i++) 13 | a[i] = i; 14 | 15 | for (int i = 0; i < n; i++) 16 | printf("%d \n", a[i]); 17 | 18 | free(a); 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /c/06-binary-search: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/06-binary-search -------------------------------------------------------------------------------- /c/06-binary-search.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int ini = 0, fin = 99, pos = 0, itera = 0, valor = 76; 5 | int a[fin + 1]; 6 | 7 | for (int i = 0; i <= fin; i++) { 8 | a[i] = i; 9 | } 10 | 11 | while (ini <= fin) { 12 | itera++; 13 | pos = (fin - ini) / 2 + ini; 14 | if (a[pos] == valor) { 15 | printf("%d iterations\n", itera); 16 | break; 17 | } 18 | else if (valor < a[pos]) { 19 | fin = pos - 1; 20 | } 21 | else { 22 | ini = pos + 1; 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /c/07-sorts: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/07-sorts -------------------------------------------------------------------------------- /c/07-sorts.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void displayArray(int a[], int size) { 4 | printf("["); 5 | for (int i = 0; i < size; i++) { 6 | printf("%d ", a[i]); 7 | } 8 | printf("]\n"); 9 | } 10 | 11 | void bubbleSort(int a[], int size) { 12 | int iter = 0; 13 | int aux; 14 | for (int i = 0; i < size - 1; i++) { 15 | for (int j = i + 1; j < size; j++) { 16 | if (a[i] > a[j]) { 17 | aux = a[i]; 18 | a[i] = a[j]; 19 | a[j] = aux; 20 | displayArray(a, size); 21 | // iter++; // Uncomment if you want to track iterations 22 | } 23 | } 24 | } 25 | // printf("%d", iter); // Uncomment if you want to display the number of iterations 26 | } 27 | 28 | void selectionSort(int a[], int size) { 29 | int iter = 0; 30 | int min, aux; 31 | for (int i = 0; i < size - 1; i++) { 32 | min = i; 33 | for (int j = i + 1; j < size; j++) { 34 | // iter++; // Uncomment if you want to track iterations 35 | if (a[min] > a[j]) { 36 | min = j; 37 | } 38 | } 39 | aux = a[i]; 40 | a[i] = a[min]; 41 | a[min] = aux; 42 | } 43 | // printf("%d", iter); // Uncomment if you want to display the number of iterations 44 | } 45 | 46 | void insertionSort(int a[], int size) { 47 | int iter = 0; 48 | int aux, j; 49 | for (int i = 0; i < size; i++) { 50 | j = i; 51 | aux = a[i]; 52 | while (j > 0 && a[j - 1] > aux) { 53 | a[j] = a[j - 1]; 54 | j--; 55 | // iter++; // Uncomment if you want to track iterations 56 | } 57 | a[j] = aux; 58 | } 59 | // printf("%d", iter); // Uncomment if you want to display the number of iterations 60 | } 61 | 62 | void quickSort(int a[], int first, int last) { 63 | int iter = 0; 64 | int pivot, aux; 65 | int i = first, j = last; 66 | int center = (first + last) / 2; 67 | pivot = a[center]; 68 | 69 | do { 70 | while (a[i] < pivot) i++; 71 | while (a[j] > pivot) j--; 72 | if (i <= j) { 73 | aux = a[i]; 74 | a[i] = a[j]; 75 | a[j] = aux; 76 | i++; 77 | j--; 78 | // iter++; // Uncomment if you want to track iterations 79 | displayArray(a, 5); // This line can be used to display the array at each iteration 80 | } 81 | } while (i <= j); 82 | 83 | if (first < j) 84 | quickSort(a, first, j); 85 | if (i < last) 86 | quickSort(a, i, last); 87 | 88 | // printf("%d", iter); // Uncomment if you want to display the number of iterations 89 | } 90 | 91 | int main() { 92 | int a[] = {17, 10, 12, 7, 11}; 93 | int size = sizeof(a) / sizeof(a[0]); 94 | 95 | displayArray(a, size); 96 | bubbleSort(a, size); 97 | displayArray(a, size); 98 | 99 | return 0; 100 | } 101 | -------------------------------------------------------------------------------- /c/08-2d-major-order: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/08-2d-major-order -------------------------------------------------------------------------------- /c/08-2d-major-order.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int m[4][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}}; 5 | int rowmajor[16]; 6 | int colmajor[16]; 7 | 8 | for (int i = 0; i < 4; i++) 9 | for (int j = 0; j < 4; j++) { 10 | rowmajor[j + 4 * i] = m[i][j]; 11 | colmajor[i + 4 * j] = m[i][j]; 12 | } 13 | 14 | for (int i = 0; i < 16; i++) 15 | printf("%d ", rowmajor[i]); 16 | printf("\n"); 17 | 18 | for (int i = 0; i < 16; i++) 19 | printf("%d ", colmajor[i]); 20 | printf("\n"); 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /c/09-3d-major-order: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/09-3d-major-order -------------------------------------------------------------------------------- /c/09-3d-major-order.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main() { 4 | int m[2][2][2] = {{{0, 1}, {2, 3}}, {{4, 5}, {6, 7}}}; 5 | int rowmajor[8]; 6 | int colmajor[8]; 7 | 8 | for (int k = 0; k < 2; k++) { 9 | printf("array: %d\n", k); 10 | for (int i = 0; i < 2; i++) { 11 | for (int j = 0; j < 2; j++) { 12 | printf("%d ", m[k][i][j]); 13 | rowmajor[j + 2 * (i + 2 * k)] = m[k][i][j]; 14 | colmajor[i + 2 * (j + 2 * k)] = m[k][i][j]; 15 | } 16 | printf("\n"); 17 | } 18 | } 19 | 20 | printf("Row Major 3D:\n"); 21 | for (int i = 0; i < 8; i++) 22 | printf("%d ", rowmajor[i]); 23 | printf("\n"); 24 | 25 | printf("Col Major 3D:\n"); 26 | for (int i = 0; i < 8; i++) 27 | printf("%d ", colmajor[i]); 28 | printf("\n"); 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /c/10-recursion: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/10-recursion -------------------------------------------------------------------------------- /c/10-recursion.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int multi(int a, int b) { 5 | if (b > 0) 6 | return a + multi(a, b-1); 7 | return 0; 8 | } 9 | 10 | int divi(int a, int b) { 11 | if (a > 1) 12 | return 1 + divi(a-b, b); 13 | return 0; 14 | } 15 | 16 | int factorial(int n) { 17 | if (n > 1) 18 | return n * factorial(n-1); 19 | return 1; 20 | } 21 | 22 | int fibonacci(int n) { 23 | if (n > 1) 24 | return fibonacci(n-1) + fibonacci(n-2); 25 | return n; 26 | } 27 | 28 | int ackermann(int m, int n) { 29 | if (m == 0) 30 | return n + 1; 31 | else if (n == 0) 32 | return ackermann(m-1, 1); 33 | else 34 | return ackermann(m-1, ackermann(m, n-1)); 35 | } 36 | 37 | 38 | bool odd(int n); 39 | bool even(int n); 40 | 41 | bool even(int n) { 42 | if (n == 0) 43 | return true; 44 | return odd(n - 1); 45 | } 46 | 47 | bool odd(int n) { 48 | if (n == 0) 49 | return false; 50 | return even(n - 1); 51 | } 52 | 53 | int main() { 54 | printf("3*2 : %d\n", multi(3, 2)); 55 | printf("6/3 : %d\n", divi(6, 3)); 56 | printf("fact(5) : %d\n", factorial(5)); 57 | printf("fibo(5) : %d\n", fibonacci(5)); 58 | printf("ack(2,2): %d\n", ackermann(2, 2)); 59 | printf("even(3): %d\n", even(3)); 60 | printf("odd(3) : %d\n", odd(3)); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /c/11-structs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/11-structs -------------------------------------------------------------------------------- /c/11-structs.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | typedef struct Caballo { 7 | char* color; 8 | void (*caminar)(void); 9 | void (*correr)(void); 10 | void (*parar)(void); 11 | } Caballo; 12 | 13 | 14 | typedef struct Pegaso { 15 | Caballo base; 16 | void (*volar)(void); 17 | } Pegaso; 18 | 19 | 20 | typedef struct Unicornio { 21 | Caballo base; 22 | void (*cornear)(void); 23 | } Unicornio; 24 | 25 | 26 | typedef struct Centauro { 27 | Caballo base; 28 | void (*hablar)(void); 29 | } Centauro; 30 | 31 | 32 | Caballo* crearCaballo() { 33 | Caballo* c = (Caballo*)malloc(sizeof(Caballo)); 34 | c->color = "blanco"; 35 | c->caminar = NULL; 36 | c->correr = NULL; 37 | c->parar = NULL; 38 | return c; 39 | } 40 | 41 | 42 | Pegaso* crearPegaso(const char* color) { 43 | Pegaso* p = (Pegaso*)malloc(sizeof(Pegaso)); 44 | p->base.color = (char*)color; 45 | p->base.caminar = NULL; 46 | p->base.correr = NULL; 47 | p->base.parar = NULL; 48 | p->volar = NULL; 49 | return p; 50 | } 51 | 52 | 53 | Unicornio* crearUnicornio() { 54 | Unicornio* u = (Unicornio*)malloc(sizeof(Unicornio)); 55 | u->base.color = "blanco"; 56 | u->base.caminar = NULL; 57 | u->base.correr = NULL; 58 | u->base.parar = NULL; 59 | u->cornear = NULL; 60 | return u; 61 | } 62 | 63 | 64 | Centauro* crearCentauro(const char* color) { 65 | Centauro* c = (Centauro*)malloc(sizeof(Centauro)); 66 | c->base.color = (char*)color; 67 | c->base.caminar = NULL; 68 | c->base.correr = NULL; 69 | c->base.parar = NULL; 70 | c->hablar = NULL; 71 | return c; 72 | } 73 | 74 | void mostrarColor(char* color) { 75 | printf("%s\n", color); 76 | } 77 | 78 | int main() { 79 | Pegaso* p = crearPegaso("negro"); 80 | Unicornio* u = crearUnicornio(); 81 | Centauro* c = crearCentauro("cafe"); 82 | 83 | mostrarColor(p->base.color); 84 | mostrarColor(u->base.color); 85 | mostrarColor(c->base.color); 86 | 87 | free(p); 88 | free(u); 89 | free(c); 90 | 91 | return 0; 92 | } 93 | -------------------------------------------------------------------------------- /c/12-linked-list: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/c/12-linked-list -------------------------------------------------------------------------------- /c/12-linked-list.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | typedef struct Node { 5 | int data; 6 | struct Node* next; 7 | } Node; 8 | 9 | typedef struct LinkedList { 10 | Node* head; 11 | } LinkedList; 12 | 13 | Node* createNode(int data) { 14 | Node* newNode = (Node*)malloc(sizeof(Node)); 15 | newNode->data = data; 16 | newNode->next = NULL; 17 | return newNode; 18 | } 19 | 20 | void addStart(LinkedList* list, int data) { 21 | Node* tmp = createNode(data); 22 | tmp->next = list->head; 23 | list->head = tmp; 24 | } 25 | 26 | void show(LinkedList* list) { 27 | Node* tmp = list->head; 28 | while (tmp != NULL) { 29 | printf("%d\n", tmp->data); 30 | tmp = tmp->next; 31 | } 32 | } 33 | 34 | // Función principal 35 | int main() { 36 | LinkedList* list1 = (LinkedList*)malloc(sizeof(LinkedList)); 37 | list1->head = NULL; 38 | 39 | addStart(list1, 10); 40 | addStart(list1, 20); 41 | addStart(list1, 30); 42 | 43 | show(list1); 44 | 45 | Node* tmp; 46 | while (list1->head != NULL) { 47 | tmp = list1->head; 48 | list1->head = list1->head->next; 49 | free(tmp); 50 | } 51 | free(list1); 52 | 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /cpp/class-diagram/mitologico/MundoMitologico.cpp: -------------------------------------------------------------------------------- 1 | // Luis Espino 2020 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | // clase abstracta 8 | class Caballo 9 | { 10 | void caminar(){} 11 | void correr(){} 12 | virtual void parar() = 0; 13 | protected: 14 | string color; 15 | public: 16 | Caballo(){ 17 | color = "blanco"; 18 | } 19 | }; 20 | 21 | // interfaces 22 | class Alas 23 | { 24 | virtual void volar() = 0; 25 | }; 26 | 27 | class Cuerno 28 | { 29 | virtual void cornear() = 0; 30 | }; 31 | 32 | class Torso 33 | { 34 | virtual void hablar() = 0; 35 | }; 36 | 37 | // clase concreta 38 | class Pegaso: Caballo, Alas 39 | { 40 | string color; 41 | public: 42 | Pegaso(string color_): color(color_) {} 43 | void show() 44 | { 45 | cout << color << endl; 46 | } 47 | void parar(){} 48 | void volar(){} 49 | }; 50 | 51 | // clase concreta 52 | class Unicornio: Caballo, Cuerno 53 | { 54 | void parar(){} 55 | void cornear(){} 56 | public: 57 | Unicornio():Caballo(){} //super 58 | void show() 59 | { 60 | cout << color << endl; 61 | } 62 | 63 | }; 64 | 65 | // clase concreta 66 | class Centauro: Caballo, Torso 67 | { 68 | string color; 69 | void parar(){} 70 | void hablar(){} 71 | public: 72 | Centauro(string color_): color(color_) {} 73 | void show() 74 | { 75 | cout << color << endl; 76 | } 77 | }; 78 | 79 | // clase principal 80 | class MundoMitologico 81 | { 82 | public: 83 | Pegaso *p = new Pegaso("negro"); 84 | Unicornio *u = new Unicornio(); 85 | Centauro *c = new Centauro("cafe"); 86 | }; 87 | 88 | int main() 89 | { 90 | // declaración en el heap 91 | MundoMitologico *m = new MundoMitologico(); 92 | m->p->show(); 93 | m->u->show(); 94 | 95 | // declaración en el stack 96 | Centauro c2("cafe"); 97 | c2.show(); 98 | } 99 | -------------------------------------------------------------------------------- /cpp/class-diagram/mitologico/MundoMitologico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/class-diagram/mitologico/MundoMitologico.jpg -------------------------------------------------------------------------------- /cpp/class-diagram/mitologico/README.md: -------------------------------------------------------------------------------- 1 | # Mundo mitológico: enunciado 2 | Se desean crear tres seres en un mundo mitológico: pegaso, unicornio y centauro. 3 | Los tres comparten tener la apariencia de un caballo, pegaso adicionalmente tiene alas, el unicornio tiene un cuerno y el centauro tiene torso humano. 4 | Normalmente pegaso es color blanco pero también hay representaciones en color negro, mientras que el unicornio normalmente es de color blanco y el centauro de color café. 5 | El caballo puede caminar, correr o parar. Pero la operación parar no está implementada inicialmente, ya que puede parar por su voluntad o por fuerza externa, o por otras condiciones. 6 | Además, si tiene alas puede volar si ya alcanzo cierta velocidad mediante la operación correr. Los seres con alas puede volar y tienen dos alas, los seres con cuernos pueden cornear y tiene un solo cuerno, y los seres con torso humano pueden hablar y tienen la característica de tener género masculino o femenino, las operaciones de los seres son abstractas y que cada uno debe implementar en su existencia. 7 | En el mundo mitológico existen simultáneamente los tres seres y no pueden existir en otros mundos. 8 | -------------------------------------------------------------------------------- /cpp/class-diagram/mitologico/output/MundoMitologico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/class-diagram/mitologico/output/MundoMitologico -------------------------------------------------------------------------------- /cpp/linear-structures/ArrayListMod.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | class Node 6 | { 7 | public: 8 | int data; 9 | Node* next; 10 | Node (int data_) : data(data_), next(0) {} 11 | }; 12 | 13 | class LinkedList 14 | { 15 | public: 16 | Node* head = 0; 17 | void addStart(int data) 18 | { 19 | Node* tmp = new Node(data); 20 | tmp->next = head; 21 | head = tmp; 22 | } 23 | void show() 24 | { 25 | Node* tmp = head; 26 | while (tmp != 0) 27 | { 28 | cout << tmp->data << " "; 29 | tmp = tmp->next; 30 | } 31 | } 32 | 33 | }; 34 | 35 | int main() 36 | { 37 | LinkedList *a[5]; 38 | for (int i=0; i<5; i++) 39 | a[i] = new LinkedList(); 40 | a[1%5]->addStart(1); 41 | a[3%5]->addStart(3); 42 | a[6%5]->addStart(6); 43 | a[8%5]->addStart(8); 44 | a[14%5]->addStart(14); 45 | for (int i=0; i<5; i++) 46 | { 47 | cout << i << " -> "; 48 | a[i]->show(); 49 | cout << endl; 50 | } 51 | return 0; 52 | } 53 | 54 | -------------------------------------------------------------------------------- /cpp/linear-structures/LinkedList.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | class Node 4 | { 5 | public: 6 | int data; 7 | Node* next; 8 | Node (int data_) : data(data_), next(0) {} 9 | }; 10 | 11 | class LinkedList 12 | { 13 | public: 14 | Node* head = 0; 15 | void addStart(int data) 16 | { 17 | Node* tmp = new Node(data); 18 | tmp->next = head; 19 | head = tmp; 20 | } 21 | 22 | void show() 23 | { 24 | Node* tmp = head; 25 | while (tmp != 0) 26 | { 27 | std::cout << tmp->data << std::endl; 28 | tmp = tmp->next; 29 | } 30 | } 31 | }; 32 | 33 | int main() 34 | { 35 | LinkedList *list1 = new LinkedList(); 36 | list1->addStart(5); 37 | list1->addStart(10); 38 | list1->addStart(7); 39 | list1->show(); 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /cpp/linear-structures/output/ArrayListMod: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/linear-structures/output/ArrayListMod -------------------------------------------------------------------------------- /cpp/linear-structures/output/LinkedList: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/linear-structures/output/LinkedList -------------------------------------------------------------------------------- /cpp/linear-structures/output/SparseMatrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/linear-structures/output/SparseMatrix -------------------------------------------------------------------------------- /cpp/merkle/dot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/merkle/dot.png -------------------------------------------------------------------------------- /cpp/merkle/output/merkle: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/merkle/output/merkle -------------------------------------------------------------------------------- /cpp/misc/major-order/col-major-2d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int m[4][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 8 | 9, 10, 11, 12, 13, 14, 15}; 9 | int a[16]; 10 | for (int i = 0; i < 4; i++) 11 | for (int j = 0; j < 4; j++) 12 | a[i + j * 4] = m[i][j]; 13 | 14 | for (int i = 0; i < 16; i++) 15 | cout << a[i] << " "; 16 | cout << endl; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cpp/misc/major-order/col-major-3d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int m[2][2][2] = {0, 1, 2, 3, 4, 5, 6, 7}; 8 | int a[8]; 9 | for (int i = 0; i < 2; i++) 10 | for (int j = 0; j < 2; j++) 11 | for (int k = 0; k < 2; k++) 12 | a[i + 2 * (j + 2 * k)] = m[k][i][j]; 13 | 14 | for (int i = 0; i < 8; i++) 15 | cout << a[i] << " "; 16 | cout << endl; 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /cpp/misc/major-order/output/col-major-2d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/major-order/output/col-major-2d -------------------------------------------------------------------------------- /cpp/misc/major-order/output/col-major-3d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/major-order/output/col-major-3d -------------------------------------------------------------------------------- /cpp/misc/major-order/output/row-major-2d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/major-order/output/row-major-2d -------------------------------------------------------------------------------- /cpp/misc/major-order/output/row-major-3d: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/major-order/output/row-major-3d -------------------------------------------------------------------------------- /cpp/misc/major-order/row-major-2d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int m[4][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 8 | 9, 10, 11, 12, 13, 14, 15}; 9 | int a[16]; 10 | for (int i = 0; i < 4; i++) 11 | for (int j = 0; j < 4; j++) 12 | a[i * 4 + j] = m[i][j]; 13 | 14 | for (int i = 0; i < 16; i++) 15 | cout << a[i] << " "; 16 | cout << endl; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cpp/misc/major-order/row-major-3d.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int m[2][2][2] = {0, 1, 2, 3, 4, 5, 6, 7}; 8 | int a[8]; 9 | for (int i = 0; i < 2; i++) 10 | for (int j = 0; j < 2; j++) 11 | for (int k = 0; k < 2; k++) 12 | a[j + 2 * (i + 2 * k)] = m[k][i][j]; 13 | 14 | for (int i = 0; i < 8; i++) 15 | cout << a[i] << " "; 16 | cout << endl; 17 | } 18 | 19 | -------------------------------------------------------------------------------- /cpp/misc/recursive/output/recursive: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/recursive/output/recursive -------------------------------------------------------------------------------- /cpp/misc/recursive/recursive.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int multi(int a, int b) 6 | { 7 | if (b > 0) 8 | return a+multi(a, b-1); 9 | return 0; 10 | } 11 | 12 | int divi(int a, int b) 13 | { 14 | if (a > 1) 15 | return 1+divi(a-b,b); 16 | return 0; 17 | } 18 | 19 | int factorial(int n) 20 | { 21 | if (n > 1) 22 | return multi(n,factorial(n-1)); 23 | return 1; 24 | } 25 | 26 | int fibonacci(int n) 27 | { 28 | if (n > 1) 29 | return fibonacci(n-1) + fibonacci(n-2); 30 | return n; 31 | } 32 | 33 | int ackermann(int m, int n) 34 | { 35 | if (m == 0) 36 | return n + 1; 37 | else if (n == 0) 38 | return ackermann(m - 1, 1); 39 | return ackermann(m - 1, ackermann(m, n - 1)); 40 | 41 | } 42 | 43 | bool par(int n); 44 | bool impar(int n); 45 | 46 | bool par(int n) 47 | { 48 | if (n == 0) 49 | return true; 50 | return impar(n - 1); 51 | } 52 | 53 | bool impar(int n) 54 | { 55 | if (n == 0) 56 | return false; 57 | return par(n - 1); 58 | } 59 | 60 | int main() 61 | { 62 | cout << "multi(2, 3): " << multi(2, 3) << endl; 63 | cout << "divi(10, 3): " << divi(10, 3) << endl; 64 | cout << "factorial(5): " << factorial(5) << endl; 65 | cout << "fibonacci(8): " << fibonacci(8) << endl; 66 | cout << "ackermann(3, 2): " << ackermann(3, 2) << endl; 67 | cout << "par(4): " << boolalpha << par(4) << endl; 68 | return 0; 69 | } -------------------------------------------------------------------------------- /cpp/misc/sha256/output/sha: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/sha256/output/sha -------------------------------------------------------------------------------- /cpp/misc/sha256/sha.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "sha.h" 3 | 4 | int main() 5 | { 6 | char data[] = "Hello World!"; 7 | string sha256 = SHA256(data); 8 | cout << sha256; 9 | } 10 | -------------------------------------------------------------------------------- /cpp/misc/sort/output/sort: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/misc/sort/output/sort -------------------------------------------------------------------------------- /cpp/misc/sort/sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void mostrarArreglo(int a[], int size) 4 | { 5 | std::cout << "["; 6 | for (int i=0; i=size-1) return; 14 | if (j>=size) 15 | { 16 | i++; 17 | j=i+1; 18 | } 19 | if (a[i]>a[j]) 20 | { 21 | int aux = a[i]; 22 | a[i]=a[j]; 23 | a[j]=aux; 24 | } 25 | ordena(a, size, i, j+1); 26 | } 27 | 28 | void ordenaBurbuja(int a[], int size) 29 | { 30 | int itera = 0; 31 | int aux; 32 | for(int i=0; ia[j]) 35 | { 36 | aux = a[i]; 37 | a[i]=a[j]; 38 | a[j]=aux; 39 | mostrarArreglo(a,size); 40 | itera++; 41 | } 42 | //std::cout << itera; 43 | } 44 | 45 | void ordenaSeleccion(int a[], int size) 46 | { 47 | int itera = 0; 48 | int menor, aux; 49 | for(int i=0; ia[j]) 56 | menor = j; 57 | 58 | } 59 | aux = a[i]; 60 | a[i] = a[menor]; 61 | a[menor] = aux; 62 | } 63 | std::cout << itera; 64 | 65 | } 66 | 67 | void ordenaInsercion(int a[], int size) 68 | { 69 | 70 | int itera = 0; 71 | int aux,j; 72 | for(int i=0; i0 && a[j-1]>aux) 77 | { 78 | a[j]=a[j-1]; 79 | j--; 80 | //itera++; 81 | } 82 | a[j] = aux; 83 | } 84 | std::cout << itera; 85 | } 86 | 87 | void ordenaRapido(int a[], int primero, int ultimo) 88 | { 89 | int itera = 0; 90 | int central = (primero+ultimo)/2; 91 | int pivote = a[central]; 92 | int aux; 93 | int i = primero, j = ultimo; 94 | do{ 95 | while (a[i]pivote) j--; 97 | if (i<=j) 98 | { 99 | aux = a[i]; 100 | a[i] = a[j]; 101 | a[j] = aux; 102 | i++; 103 | j--; 104 | itera++; 105 | mostrarArreglo(a,5); 106 | } 107 | } 108 | while(i<=j); 109 | if (primero 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Node 7 | { 8 | public: 9 | int val; 10 | Node* left; 11 | Node* right; 12 | Node (int val_) : val(val_), left(0), right(0) {} 13 | }; 14 | 15 | class BSTree 16 | { 17 | public: 18 | Node* root; 19 | BSTree () : root(0) {} 20 | 21 | void add(int val) 22 | { 23 | if (root != 0) add(val, root); 24 | else root = new Node(val); 25 | } 26 | 27 | void add(int val, Node* tmp) 28 | { 29 | if (valval) 30 | { 31 | if (tmp->left!=0) add(val, tmp->left); 32 | else tmp->left = new Node(val); 33 | } 34 | else 35 | { 36 | if (tmp->right!=0) add(val, tmp->right); 37 | else tmp->right = new Node(val); 38 | } 39 | } 40 | 41 | // also called depth traversal with backtracking 42 | void preorder(Node* tmp) 43 | { 44 | if (tmp!=0) 45 | { 46 | cout << tmp->val << " "; 47 | preorder(tmp->left); 48 | preorder(tmp->right); 49 | } 50 | } 51 | 52 | void inorder(Node* tmp) 53 | { 54 | if (tmp!=0) 55 | { 56 | inorder(tmp->left); 57 | cout << tmp->val << " "; 58 | inorder(tmp->right); 59 | } 60 | } 61 | 62 | void postorder(Node* tmp) 63 | { 64 | if (tmp!=0) 65 | { 66 | postorder(tmp->left); 67 | postorder(tmp->right); 68 | cout << tmp->val<< " "; 69 | } 70 | } 71 | 72 | // also called breadth traversal 73 | void levelorder(Node* tmp) 74 | { 75 | if (tmp == 0) return; 76 | 77 | queue q; 78 | q.push(tmp); 79 | 80 | while (!q.empty()) 81 | { 82 | Node *node = q.front(); 83 | cout << node->val << " "; 84 | q.pop(); 85 | 86 | if (node->left != 0) q.push(node->left); 87 | if (node->right != 0) q.push(node->right); 88 | } 89 | } 90 | 91 | }; 92 | 93 | int main() 94 | { 95 | BSTree* t = new BSTree(); 96 | t->add(25);t->add(10);t->add(35); t->add(5); 97 | t->add(20); t->add(30);t->add(40); 98 | t->preorder(t->root);cout << endl; 99 | t->inorder(t->root);cout << endl; 100 | t->postorder(t->root);cout << endl; 101 | t->levelorder(t->root);cout << endl; 102 | } 103 | 104 | -------------------------------------------------------------------------------- /cpp/non-linear-structures/btree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/btree.png -------------------------------------------------------------------------------- /cpp/non-linear-structures/graph.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | 6 | class Grafo 7 | { 8 | int n; 9 | std::list *ady; 10 | public: 11 | Grafo(int n_): n(n_), ady(new std::list[n_]) {} 12 | void add(int origen, int destino) 13 | { 14 | ady[origen].push_back(destino); 15 | } 16 | void anchura(int inicio) 17 | { 18 | bool *visitados = new bool[n]; 19 | for(int i = 0; i < n; i++) 20 | visitados[i] = false; 21 | std::list cola; 22 | visitados[inicio] = true; 23 | cola.push_back(inicio); 24 | std::list::iterator i; 25 | while(!cola.empty()) 26 | { 27 | inicio = cola.front(); 28 | std::cout << inicio << " "; 29 | cola.pop_front(); 30 | for(i = ady[inicio].begin(); i != ady[inicio].end(); i++) 31 | { 32 | if(!visitados[*i]) 33 | { 34 | visitados[*i] = true; 35 | cola.push_back(*i); 36 | } 37 | } 38 | } 39 | } 40 | void profundidad(int inicio) 41 | { 42 | bool *visitados = new bool[n]; 43 | for(int i = 0; i < n; i++) 44 | visitados[i] = false; 45 | std::list cola; 46 | visitados[inicio] = true; 47 | cola.push_front(inicio); 48 | std::list::iterator i; 49 | while(!cola.empty()) 50 | { 51 | inicio = cola.front(); 52 | std::cout << inicio << " "; 53 | cola.pop_front(); 54 | for(i = ady[inicio].begin(); i != ady[inicio].end(); i++) 55 | { 56 | if(!visitados[*i]) 57 | { 58 | visitados[*i] = true; 59 | cola.push_front(*i); 60 | } 61 | } 62 | } 63 | } 64 | }; 65 | 66 | 67 | int main() 68 | { 69 | Grafo g(8); 70 | g.add(0, 1);g.add(0, 4); 71 | g.add(1, 0);g.add(1, 2);g.add(1, 4); 72 | g.add(2, 3);g.add(2, 5); 73 | g.add(3, 6);g.add(3, 7); 74 | g.add(4, 2);g.add(4, 5); 75 | g.add(5, 2);g.add(5, 6); 76 | g.add(6, 2);g.add(6, 7); 77 | g.anchura(0); 78 | cout << "\n"; 79 | g.profundidad(0); 80 | cout << "\n"; 81 | return 0; 82 | } -------------------------------------------------------------------------------- /cpp/non-linear-structures/hash.cpp: -------------------------------------------------------------------------------- 1 | // Luis Espino (c) 2024 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Hash 8 | { 9 | public: 10 | int n, m; 11 | int *h; 12 | int min, max; 13 | 14 | Hash(int m, int min, int max) 15 | { 16 | this->m = m; 17 | this->min = min; 18 | this->max = max; 19 | init(); 20 | } 21 | 22 | int division(int k) 23 | { 24 | return (k % m); 25 | } 26 | 27 | int linear(int k) 28 | { 29 | return ((k+1) % m); 30 | } 31 | 32 | void init() 33 | { 34 | n = 0; 35 | h = new int[m]; 36 | for(int i=0; i=max) 53 | { 54 | //array copy 55 | int *temp = h; 56 | print(); 57 | //rehashing 58 | int mprev = m; 59 | m = n*100/min; 60 | init(); 61 | for (int i=0; iinsert(5); 80 | hash->insert(10); 81 | hash->insert(15); 82 | hash->insert(20); 83 | hash->insert(25); 84 | hash->insert(30); 85 | return 0; 86 | } 87 | -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/README.me: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/avl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/avl -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/bst: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/bst -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/btree: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/btree -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/graph: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/graph -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/hash: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/hash -------------------------------------------------------------------------------- /cpp/non-linear-structures/output/uniformcost: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/non-linear-structures/output/uniformcost -------------------------------------------------------------------------------- /cpp/sparse-matrix/DynamicSparseMatrix.cpp: -------------------------------------------------------------------------------- 1 | // Luis Espino 2024 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | class NodoC{ 8 | public: 9 | NodoC *derecha; 10 | NodoC *abajo; 11 | string valor; 12 | NodoC(string valor){ 13 | this->valor = valor; 14 | derecha = NULL; 15 | abajo = NULL; 16 | } 17 | }; 18 | 19 | class NodoFila{ 20 | public: 21 | int numFila; 22 | NodoFila *siguiente; 23 | NodoC *derecha; 24 | NodoFila(int numFila){ 25 | this->numFila = numFila; 26 | siguiente = NULL; 27 | derecha = NULL; 28 | } 29 | }; 30 | 31 | class NodoColumna{ 32 | public: 33 | char numColumna; 34 | NodoColumna *siguiente; 35 | NodoC *abajo; 36 | NodoColumna(char numColumna){ 37 | this->numColumna = numColumna; 38 | siguiente = NULL; 39 | abajo = NULL; 40 | } 41 | }; 42 | 43 | class DynamicSparseMatrix { 44 | public: 45 | NodoFila *primeroFila; 46 | NodoColumna *primeroColumna; 47 | 48 | void agregarNodoFila(int numFila) { 49 | if (primeroFila==NULL) 50 | primeroFila = new NodoFila(numFila); 51 | else { 52 | NodoFila *temp = primeroFila; 53 | primeroFila = new NodoFila(numFila); 54 | primeroFila->siguiente = temp; 55 | } 56 | } 57 | 58 | void agregarNodoColumna(char numColumna) { 59 | if (primeroColumna==NULL) 60 | primeroColumna = new NodoColumna(numColumna); 61 | else { 62 | NodoColumna *temp = primeroColumna; 63 | primeroColumna = new NodoColumna(numColumna); 64 | primeroColumna->siguiente = temp; 65 | } 66 | } 67 | 68 | void agregar(string valor, char columna, int fila){ 69 | NodoColumna *tempColumna = primeroColumna; 70 | NodoFila *tempFila = primeroFila; 71 | 72 | NodoC *nuevo = new NodoC(valor); 73 | 74 | while (tempColumna!=NULL) { 75 | if (tempColumna->numColumna==columna) { 76 | NodoC *temp = tempColumna->abajo; 77 | if (temp==NULL) 78 | tempColumna->abajo = nuevo; 79 | else { 80 | while (temp->abajo!=NULL) temp = temp->abajo; 81 | temp->abajo = nuevo; 82 | 83 | } 84 | break; 85 | } 86 | tempColumna = tempColumna->siguiente; 87 | } 88 | 89 | while (tempFila!=NULL) { 90 | if (tempFila->numFila==fila) { 91 | NodoC *temp = tempFila->derecha; 92 | if (temp==NULL) 93 | tempFila->derecha = nuevo; 94 | else { 95 | while (temp->derecha!=NULL) temp = temp->derecha; 96 | temp->derecha = nuevo; 97 | } 98 | break; 99 | } 100 | tempFila = tempFila->siguiente; 101 | } 102 | } 103 | 104 | void mostrarColumnas() { 105 | NodoColumna *temp = primeroColumna; 106 | cout << " "; 107 | while(temp!=NULL) { 108 | cout << temp->numColumna << " "; 109 | temp = temp->siguiente; 110 | } 111 | cout << endl; 112 | } 113 | 114 | void mostrarFilas() { 115 | NodoFila *temp = primeroFila; 116 | while(temp!=NULL) { 117 | cout << temp->numFila << " "; 118 | NodoC *t = temp->derecha; 119 | while (t!=NULL){ 120 | cout << t->valor << " "; 121 | t = t->derecha; 122 | } 123 | cout << endl; 124 | temp = temp->siguiente; 125 | } 126 | cout << endl; 127 | } 128 | }; 129 | 130 | 131 | int main() 132 | { 133 | DynamicSparseMatrix *sm = new DynamicSparseMatrix(); 134 | sm->agregarNodoColumna('C'); 135 | sm->agregarNodoColumna('B'); 136 | sm->agregarNodoColumna('A'); 137 | sm->agregarNodoFila(3); 138 | sm->agregarNodoFila(2); 139 | sm->agregarNodoFila(1); 140 | sm->agregar("5",'A', 1); 141 | sm->agregar("8",'A', 2); 142 | sm->agregar("7",'B', 2); 143 | sm->agregar("1",'A', 3); 144 | sm->agregar("3",'B', 3); 145 | sm->agregar("9",'C', 3); 146 | sm->mostrarColumnas(); 147 | sm->mostrarFilas(); 148 | } -------------------------------------------------------------------------------- /cpp/sparse-matrix/StaticSparseMatrix.cpp: -------------------------------------------------------------------------------- 1 | // Luis Espino 2024 2 | 3 | #include 4 | 5 | #define MAX 4 6 | 7 | using namespace std; 8 | 9 | // coordinate list implementation 10 | class StaticSparseMatrix 11 | { 12 | public: 13 | int *val, *row, *col, values; 14 | 15 | StaticSparseMatrix(int m[MAX][MAX]) 16 | { 17 | // get number of values 18 | values = 0; 19 | for (int i = 0; i < MAX; i++) 20 | for (int j = 0; j < MAX; j++) 21 | if (m[i][j] != 0) 22 | values++; 23 | 24 | // init arrays 25 | val = new int[values]; 26 | row = new int[values]; 27 | col = new int[values]; 28 | 29 | // build coordinate list 30 | int nvalues = 0; 31 | for (int i = 0; i < MAX; i++) 32 | for (int j = 0; j < MAX; j++) 33 | if (m[i][j]!=0) 34 | { 35 | val[nvalues] = m[i][j]; 36 | row[nvalues] = i; 37 | col[nvalues++] = j; 38 | } 39 | } 40 | 41 | ~StaticSparseMatrix() 42 | { 43 | delete[] val; 44 | delete[] row; 45 | delete[] col; 46 | } 47 | 48 | void print() 49 | { 50 | cout << "val: "; 51 | for (int i = 0; i < values; i++) 52 | cout << val[i] << " "; 53 | cout << endl << "row: "; 54 | for (int i = 0; i < values; i++) 55 | cout << row[i] << " "; 56 | cout << endl << "col: "; 57 | for (int i = 0; i < values; i++) 58 | cout << col[i] << " "; 59 | cout << endl; 60 | } 61 | }; 62 | 63 | int main() 64 | { 65 | int m[MAX][MAX] = { {5, 4, 0, 0}, 66 | {0, 1, 0, 0}, 67 | {0, 0, 6, 0}, 68 | {0, 0, 3, 2} }; 69 | StaticSparseMatrix *s = new StaticSparseMatrix(m); 70 | s->print(); 71 | return 0; 72 | } -------------------------------------------------------------------------------- /cpp/sparse-matrix/output/DynamicOrthoSparseMatrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/sparse-matrix/output/DynamicOrthoSparseMatrix -------------------------------------------------------------------------------- /cpp/sparse-matrix/output/DynamicSparseMatrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/sparse-matrix/output/DynamicSparseMatrix -------------------------------------------------------------------------------- /cpp/sparse-matrix/output/Orthogonal: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/sparse-matrix/output/Orthogonal -------------------------------------------------------------------------------- /cpp/sparse-matrix/output/StaticSparseMatrix: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/cpp/sparse-matrix/output/StaticSparseMatrix -------------------------------------------------------------------------------- /csharp/01-miscellaneous/01-StackHeap/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | class Program 5 | { 6 | unsafe static void Main(string[] args) 7 | { 8 | // Stack variable 9 | int stackVar = 10; 10 | Console.WriteLine($"Stack value: {stackVar}"); 11 | 12 | // Heap variable using Marshal 13 | int* heapVar = (int*)Marshal.AllocHGlobal(sizeof(int)); 14 | *heapVar = 20; 15 | Console.WriteLine($"Heap value: { *heapVar }"); 16 | 17 | // Free allocated memory 18 | Marshal.FreeHGlobal((IntPtr)heapVar); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/01-StackHeap/StackHeap.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0 6 | enable 7 | enable 8 | true 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/01-StackHeap/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/01-StackHeap/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/02-DynArray/DynArray.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0 6 | enable 7 | enable 8 | true 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/02-DynArray/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | class Program 5 | { 6 | static void Main() 7 | { 8 | Console.Write("Enter size: "); 9 | int n = int.Parse(Console.ReadLine()); 10 | 11 | IntPtr a = Marshal.AllocHGlobal(n * sizeof(int)); 12 | 13 | for (int i = 0; i < n; i++) 14 | { 15 | Marshal.WriteInt32(a, i * sizeof(int), i); 16 | } 17 | 18 | for (int i = 0; i < n; i++) 19 | { 20 | int value = Marshal.ReadInt32(a, i * sizeof(int)); 21 | Console.WriteLine(value); 22 | } 23 | 24 | Marshal.FreeHGlobal(a); 25 | } 26 | } 27 | 28 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/02-DynArray/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/02-DynArray/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/03-MajorOrder2D/MajorOrder2D.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/03-MajorOrder2D/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Program 4 | { 5 | static void Main() 6 | { 7 | int[,] m = { { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 }, { 12, 13, 14, 15 } }; 8 | int[] rowmajor = new int[16]; 9 | int[] colmajor = new int[16]; 10 | 11 | for (int i = 0; i < 4; i++) 12 | { 13 | for (int j = 0; j < 4; j++) 14 | { 15 | rowmajor[i * 4 + j] = m[i, j]; 16 | colmajor[i + j * 4] = m[i, j]; 17 | } 18 | } 19 | 20 | for (int i = 0; i < 16; i++) 21 | { 22 | Console.Write(rowmajor[i] + " "); 23 | } 24 | Console.WriteLine(); 25 | 26 | for (int i = 0; i < 16; i++) 27 | { 28 | Console.Write(colmajor[i] + " "); 29 | } 30 | Console.WriteLine(); 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/03-MajorOrder2D/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/03-MajorOrder2D/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/04-MythologyClasses/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | abstract class Caballo 4 | { 5 | public string Color; 6 | 7 | public Caballo() 8 | { 9 | this.Color = "blanco"; 10 | } 11 | 12 | public void Caminar() { } 13 | public void Correr() { } 14 | 15 | public abstract void Parar(); 16 | } 17 | 18 | interface IAlas 19 | { 20 | void Volar(); 21 | } 22 | 23 | interface ICuerno 24 | { 25 | void Cornear(); 26 | } 27 | 28 | interface ITorso 29 | { 30 | void Hablar(); 31 | } 32 | 33 | class Pegaso : Caballo, IAlas 34 | { 35 | public string Color; 36 | 37 | public Pegaso(string color) 38 | { 39 | this.Color = color; 40 | } 41 | 42 | public void Show() 43 | { 44 | Console.WriteLine(Color); 45 | } 46 | 47 | public override void Parar() { } 48 | 49 | public void Volar() { } 50 | } 51 | 52 | class Unicornio : Caballo, ICuerno 53 | { 54 | public Unicornio() : base() { } 55 | 56 | public override void Parar() { } 57 | 58 | public void Cornear() { } 59 | 60 | public void Show() 61 | { 62 | Console.WriteLine(Color); 63 | } 64 | } 65 | 66 | class Centauro : Caballo, ITorso 67 | { 68 | public string Color; 69 | 70 | public Centauro(string color) 71 | { 72 | this.Color = color; 73 | } 74 | 75 | public override void Parar() { } 76 | 77 | public void Hablar() { } 78 | 79 | public void Show() 80 | { 81 | Console.WriteLine(Color); 82 | } 83 | } 84 | 85 | class MundoMitologico 86 | { 87 | public Pegaso P = new Pegaso("negro"); 88 | public Unicornio U = new Unicornio(); 89 | public Centauro C = new Centauro("cafe"); 90 | 91 | public static void Main(string[] args) 92 | { 93 | MundoMitologico M = new MundoMitologico(); 94 | M.P.Show(); 95 | M.U.Show(); 96 | M.C.Show(); 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/04-MythologyClasses/mythology.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/04-MythologyClasses/mythology.jpg -------------------------------------------------------------------------------- /csharp/01-miscellaneous/04-MythologyClasses/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/04-MythologyClasses/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/05-Sort/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Program 4 | { 5 | static void DisplayArray(int[] a) 6 | { 7 | Console.Write("["); 8 | for (int i = 0; i < a.Length; i++) 9 | { 10 | Console.Write(a[i] + " "); 11 | } 12 | Console.WriteLine("]"); 13 | } 14 | 15 | static void BubbleSort(int[] a) 16 | { 17 | int aux; 18 | for (int i = 0; i < a.Length - 1; i++) 19 | { 20 | for (int j = i + 1; j < a.Length; j++) 21 | { 22 | if (a[i] > a[j]) 23 | { 24 | aux = a[i]; 25 | a[i] = a[j]; 26 | a[j] = aux; 27 | DisplayArray(a); // Display array after each swap 28 | } 29 | } 30 | } 31 | } 32 | 33 | static void SelectionSort(int[] a) 34 | { 35 | int min, aux; 36 | for (int i = 0; i < a.Length - 1; i++) 37 | { 38 | min = i; 39 | for (int j = i + 1; j < a.Length; j++) 40 | { 41 | if (a[min] > a[j]) 42 | { 43 | min = j; 44 | } 45 | } 46 | aux = a[i]; 47 | a[i] = a[min]; 48 | a[min] = aux; 49 | } 50 | } 51 | 52 | static void InsertionSort(int[] a) 53 | { 54 | int aux, j; 55 | for (int i = 0; i < a.Length; i++) 56 | { 57 | j = i; 58 | aux = a[i]; 59 | while (j > 0 && a[j - 1] > aux) 60 | { 61 | a[j] = a[j - 1]; 62 | j--; 63 | } 64 | a[j] = aux; 65 | } 66 | } 67 | 68 | static void QuickSort(int[] a, int first, int last) 69 | { 70 | int pivot, aux; 71 | int i = first, j = last; 72 | int center = (first + last) / 2; 73 | pivot = a[center]; 74 | 75 | do 76 | { 77 | while (a[i] < pivot) i++; 78 | while (a[j] > pivot) j--; 79 | if (i <= j) 80 | { 81 | aux = a[i]; 82 | a[i] = a[j]; 83 | a[j] = aux; 84 | i++; 85 | j--; 86 | DisplayArray(a); // Display array during quicksort 87 | } 88 | } while (i <= j); 89 | 90 | if (first < j) 91 | QuickSort(a, first, j); 92 | if (i < last) 93 | QuickSort(a, i, last); 94 | } 95 | 96 | static void Main() 97 | { 98 | int[] a = { 17, 10, 12, 7, 11 }; 99 | 100 | DisplayArray(a); 101 | BubbleSort(a); 102 | DisplayArray(a); 103 | } 104 | } 105 | 106 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/05-Sort/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/05-Sort/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/06-Recursive/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Program 4 | { 5 | static int Multi(int a, int b) 6 | { 7 | if (b > 0) 8 | return a + Multi(a, b - 1); 9 | return 0; 10 | } 11 | 12 | static int Divi(int a, int b) 13 | { 14 | if (a > 1) 15 | return 1 + Divi(a - b, b); 16 | return 0; 17 | } 18 | 19 | static int Factorial(int n) 20 | { 21 | if (n > 1) 22 | return Multi(n, Factorial(n - 1)); 23 | return 1; 24 | } 25 | 26 | static int Fibonacci(int n) 27 | { 28 | if (n > 1) 29 | return Fibonacci(n - 1) + Fibonacci(n - 2); 30 | return n; 31 | } 32 | 33 | static int Ackermann(int m, int n) 34 | { 35 | if (m == 0) 36 | return n + 1; 37 | else if (n == 0) 38 | return Ackermann(m - 1, 1); 39 | return Ackermann(m - 1, Ackermann(m, n - 1)); 40 | } 41 | 42 | static bool Par(int n) 43 | { 44 | if (n == 0) 45 | return true; 46 | return Impar(n - 1); 47 | } 48 | 49 | static bool Impar(int n) 50 | { 51 | if (n == 0) 52 | return false; 53 | return Par(n - 1); 54 | } 55 | 56 | static void Main() 57 | { 58 | Console.WriteLine("multi(2, 3): " + Multi(2, 3)); 59 | Console.WriteLine("divi(10, 3): " + Divi(10, 3)); 60 | Console.WriteLine("factorial(5): " + Factorial(5)); 61 | Console.WriteLine("fibonacci(8): " + Fibonacci(8)); 62 | Console.WriteLine("ackermann(3, 2): " + Ackermann(3, 2)); 63 | Console.WriteLine("par(4): " + Par(4)); 64 | } 65 | } 66 | 67 | -------------------------------------------------------------------------------- /csharp/01-miscellaneous/06-Recursive/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/01-miscellaneous/06-Recursive/run.png -------------------------------------------------------------------------------- /csharp/01-miscellaneous/07-BinarySearch/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Program 4 | { 5 | static void Main() 6 | { 7 | int ini = 0, fin = 99, pos = 0, itera = 0, valor = 76; 8 | int[] a = new int[fin + 1]; 9 | 10 | for (int i = 0; i <= fin; i++) 11 | { 12 | a[i] = i; 13 | } 14 | 15 | while (ini <= fin) 16 | { 17 | itera++; 18 | pos = (fin - ini) / 2 + ini; 19 | if (a[pos] == valor) 20 | { 21 | Console.WriteLine($"{itera} iterations"); 22 | break; 23 | } 24 | else if (valor < a[pos]) 25 | { 26 | fin = pos - 1; 27 | } 28 | else 29 | { 30 | ini = pos + 1; 31 | } 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /csharp/02-linear-structures/List/List.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | Exe 4 | net9.0 5 | enable 6 | enable 7 | true 8 | 9 | 10 | -------------------------------------------------------------------------------- /csharp/02-linear-structures/List/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | class Program 5 | { 6 | public unsafe class Node 7 | { 8 | public int Val; 9 | public Node* Next; 10 | } 11 | 12 | public unsafe class List 13 | { 14 | public Node* Head; 15 | 16 | public List() 17 | { 18 | Head = null; 19 | } 20 | 21 | public unsafe void Insert(int val) 22 | { 23 | Node* Tmp = (Node*)Marshal.AllocHGlobal(sizeof(Node)); 24 | *Tmp = new Node{ Val = val, Next = null }; 25 | 26 | if (Head == null) 27 | { 28 | Head = Tmp; 29 | } 30 | else 31 | { 32 | Tmp->Next = Head; 33 | Head = Tmp; 34 | } 35 | } 36 | 37 | public unsafe void Print() 38 | { 39 | Node* Tmp = Head; 40 | while (Tmp != null) 41 | { 42 | Console.Write(Tmp->Val + " "); 43 | Tmp = Tmp->Next; 44 | } 45 | Console.WriteLine(); 46 | } 47 | } 48 | 49 | static void Main() 50 | { 51 | unsafe 52 | { 53 | List list = new List(); 54 | list.Insert(10); 55 | list.Insert(20); 56 | list.Insert(30); 57 | list.Insert(40); 58 | 59 | list.Print(); 60 | } 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /csharp/02-linear-structures/List/run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/02-linear-structures/List/run.png -------------------------------------------------------------------------------- /csharp/02-linear-structures/SparseMatrix/SparseMatrix.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | net9.0 6 | enable 7 | enable 8 | true 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /csharp/02-linear-structures/SparseMatrix/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/02-linear-structures/SparseMatrix/dotnet-run.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/AVL/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Node 4 | { 5 | public int val; 6 | public Node izq; 7 | public Node der; 8 | public int alt; 9 | 10 | public Node(int val_) 11 | { 12 | val = val_; 13 | izq = null; 14 | der = null; 15 | alt = 0; 16 | } 17 | } 18 | 19 | class AVLTree 20 | { 21 | public Node raiz; 22 | 23 | public AVLTree() 24 | { 25 | raiz = null; 26 | } 27 | 28 | public void Add(int val) 29 | { 30 | if (raiz != null) 31 | Add(val, ref raiz); 32 | else 33 | raiz = new Node(val); 34 | } 35 | 36 | public void Add(int val, ref Node tmp) 37 | { 38 | if (tmp == null) 39 | tmp = new Node(val); 40 | else if (val < tmp.val) 41 | { 42 | Add(val, ref tmp.izq); 43 | if ((Altura(tmp.izq) - Altura(tmp.der)) == 2) 44 | { 45 | if (val < tmp.izq.val) 46 | tmp = Srl(tmp); 47 | else 48 | tmp = Drl(tmp); 49 | } 50 | } 51 | else if (val > tmp.val) 52 | { 53 | Add(val, ref tmp.der); 54 | if ((Altura(tmp.der) - Altura(tmp.izq)) == 2) 55 | { 56 | if (val > tmp.der.val) 57 | tmp = Srr(tmp); 58 | else 59 | tmp = Drr(tmp); 60 | } 61 | } 62 | 63 | int d = Altura(tmp.der); 64 | int i = Altura(tmp.izq); 65 | int m = Maxi(d, i); 66 | tmp.alt = m + 1; 67 | } 68 | 69 | public int Altura(Node tmp) 70 | { 71 | if (tmp == null) 72 | return -1; 73 | else 74 | return tmp.alt; 75 | } 76 | 77 | public void Preorden(Node tmp) 78 | { 79 | if (tmp != null) 80 | { 81 | Console.Write(tmp.val + " "); 82 | Preorden(tmp.izq); 83 | Preorden(tmp.der); 84 | } 85 | } 86 | 87 | public void Enorden(Node tmp) 88 | { 89 | if (tmp != null) 90 | { 91 | Enorden(tmp.izq); 92 | Console.Write(tmp.val + " "); 93 | Enorden(tmp.der); 94 | } 95 | } 96 | 97 | public void Postorden(Node tmp) 98 | { 99 | if (tmp != null) 100 | { 101 | Postorden(tmp.izq); 102 | Postorden(tmp.der); 103 | Console.Write(tmp.val + " "); 104 | } 105 | } 106 | 107 | public Node Srl(Node t1) 108 | { 109 | Node t2 = t1.izq; 110 | t1.izq = t2.der; 111 | t2.der = t1; 112 | 113 | t1.alt = Maxi(Altura(t1.izq), Altura(t1.der)) + 1; 114 | t2.alt = Maxi(Altura(t2.izq), t1.alt) + 1; 115 | 116 | return t2; 117 | } 118 | 119 | public Node Srr(Node t1) 120 | { 121 | Node t2 = t1.der; 122 | t1.der = t2.izq; 123 | t2.izq = t1; 124 | 125 | t1.alt = Maxi(Altura(t1.izq), Altura(t1.der)) + 1; 126 | t2.alt = Maxi(Altura(t2.der), t1.alt) + 1; 127 | 128 | return t2; 129 | } 130 | 131 | public Node Drl(Node tmp) 132 | { 133 | tmp.izq = Srr(tmp.izq); 134 | return Srl(tmp); 135 | } 136 | 137 | public Node Drr(Node tmp) 138 | { 139 | tmp.der = Srl(tmp.der); 140 | return Srr(tmp); 141 | } 142 | 143 | public int Maxi(int val1, int val2) 144 | { 145 | return (val1 > val2) ? val1 : val2; 146 | } 147 | } 148 | 149 | class Program 150 | { 151 | static void Main() 152 | { 153 | AVLTree t = new AVLTree(); 154 | t.Add(5); 155 | t.Add(10); 156 | t.Add(15); 157 | t.Add(20); 158 | t.Add(25); 159 | t.Add(30); 160 | t.Add(35); 161 | 162 | t.Preorden(t.raiz); 163 | Console.WriteLine(); 164 | 165 | t.Enorden(t.raiz); 166 | Console.WriteLine(); 167 | 168 | t.Postorden(t.raiz); 169 | Console.WriteLine(); 170 | } 171 | } 172 | 173 | -------------------------------------------------------------------------------- /csharp/03-tree-structures/AVL/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/AVL/dotnet-run.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/BSTree/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | 4 | class Node 5 | { 6 | public int val; 7 | public Node left; 8 | public Node right; 9 | 10 | public Node(int val_) 11 | { 12 | val = val_; 13 | left = null; 14 | right = null; 15 | } 16 | } 17 | 18 | class BSTree 19 | { 20 | public Node root; 21 | public BSTree() 22 | { 23 | root = null; 24 | } 25 | 26 | public void Add(int val) 27 | { 28 | if (root != null) 29 | Add(val, root); 30 | else 31 | root = new Node(val); 32 | } 33 | 34 | private void Add(int val, Node tmp) 35 | { 36 | if (val < tmp.val) 37 | { 38 | if (tmp.left != null) 39 | Add(val, tmp.left); 40 | else 41 | tmp.left = new Node(val); 42 | } 43 | else 44 | { 45 | if (tmp.right != null) 46 | Add(val, tmp.right); 47 | else 48 | tmp.right = new Node(val); 49 | } 50 | } 51 | 52 | // Preorder traversal 53 | public void Preorder(Node tmp) 54 | { 55 | if (tmp != null) 56 | { 57 | Console.Write(tmp.val + " "); 58 | Preorder(tmp.left); 59 | Preorder(tmp.right); 60 | } 61 | } 62 | 63 | // Inorder traversal 64 | public void Inorder(Node tmp) 65 | { 66 | if (tmp != null) 67 | { 68 | Inorder(tmp.left); 69 | Console.Write(tmp.val + " "); 70 | Inorder(tmp.right); 71 | } 72 | } 73 | 74 | // Postorder traversal 75 | public void Postorder(Node tmp) 76 | { 77 | if (tmp != null) 78 | { 79 | Postorder(tmp.left); 80 | Postorder(tmp.right); 81 | Console.Write(tmp.val + " "); 82 | } 83 | } 84 | 85 | } 86 | 87 | public class Program 88 | { 89 | static void Main() 90 | { 91 | BSTree t = new BSTree(); 92 | t.Add(25); 93 | t.Add(10); 94 | t.Add(35); 95 | t.Add(5); 96 | t.Add(20); 97 | t.Add(30); 98 | t.Add(40); 99 | 100 | t.Preorder(t.root); 101 | Console.WriteLine(); 102 | 103 | t.Inorder(t.root); 104 | Console.WriteLine(); 105 | 106 | t.Postorder(t.root); 107 | Console.WriteLine(); 108 | 109 | } 110 | } 111 | 112 | -------------------------------------------------------------------------------- /csharp/03-tree-structures/BSTree/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/BSTree/dotnet-run.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/BTree/btree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/BTree/btree.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/BTree/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/BTree/dotnet-run.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/ListBSTree/ListBSTree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/ListBSTree/ListBSTree.png -------------------------------------------------------------------------------- /csharp/03-tree-structures/ListBSTree/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class NodeList 4 | { 5 | public string Value; 6 | public NodeList Next; 7 | 8 | public NodeList(string value) 9 | { 10 | Value = value; 11 | Next = null; 12 | } 13 | } 14 | 15 | class List 16 | { 17 | public NodeList Head; 18 | 19 | public List() 20 | { 21 | Head = null; 22 | } 23 | 24 | public void Add(string value) 25 | { 26 | NodeList tmp = new NodeList(value); 27 | tmp.Next = Head; 28 | Head = tmp; 29 | } 30 | 31 | public void Show() 32 | { 33 | Console.Write("[ "); 34 | NodeList tmp = Head; 35 | while (tmp != null) 36 | { 37 | Console.Write(tmp.Value + " "); 38 | tmp = tmp.Next; 39 | } 40 | Console.WriteLine("]"); 41 | } 42 | } 43 | 44 | class Node 45 | { 46 | public int Value; 47 | public List List; 48 | public Node Left; 49 | public Node Right; 50 | 51 | public Node(int value, List list) 52 | { 53 | Value = value; 54 | List = list; 55 | Left = null; 56 | Right = null; 57 | } 58 | } 59 | 60 | class BSTree 61 | { 62 | public Node Root; 63 | 64 | public BSTree() 65 | { 66 | Root = null; 67 | } 68 | 69 | public void Add(int value, List list) 70 | { 71 | Root = _Add(value, list, Root); 72 | } 73 | 74 | private Node _Add(int value, List list, Node tmp) 75 | { 76 | if (tmp == null) 77 | { 78 | return new Node(value, list); 79 | } 80 | else if (value > tmp.Value) 81 | { 82 | tmp.Right = _Add(value, list, tmp.Right); 83 | } 84 | else 85 | { 86 | tmp.Left = _Add(value, list, tmp.Left); 87 | } 88 | return tmp; 89 | } 90 | 91 | public void Show() 92 | { 93 | _Show(Root); 94 | } 95 | 96 | private void _Show(Node tmp) 97 | { 98 | if (tmp != null) 99 | { 100 | _Show(tmp.Left); 101 | Console.Write(tmp.Value + " "); 102 | tmp.List.Show(); 103 | _Show(tmp.Right); 104 | } 105 | } 106 | } 107 | 108 | class Program 109 | { 110 | static void Main() 111 | { 112 | // Create a new BSTree 113 | BSTree bst = new BSTree(); 114 | 115 | // Add nodes with their associated lists 116 | List tmp = new List(); 117 | tmp.Add("Fernandez"); 118 | tmp.Add("Santiago"); 119 | bst.Add(25, tmp); 120 | 121 | tmp = new List(); 122 | tmp.Add("Lemus"); 123 | tmp.Add("Karla"); 124 | bst.Add(5, tmp); 125 | 126 | tmp = new List(); 127 | tmp.Add("Cabrera"); 128 | tmp.Add("Angel"); 129 | bst.Add(10, tmp); 130 | 131 | tmp = new List(); 132 | tmp.Add("Gonzalez"); 133 | tmp.Add("Carlos"); 134 | bst.Add(41, tmp); 135 | 136 | // Print the tree 137 | bst.Show(); 138 | } 139 | } 140 | 141 | -------------------------------------------------------------------------------- /csharp/03-tree-structures/ListBSTree/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/03-tree-structures/ListBSTree/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Crypto/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Security.Cryptography; 4 | using System.Text; 5 | 6 | class Program 7 | { 8 | static void Main() 9 | { 10 | string plainText = "Hello, World!"; 11 | Console.WriteLine($"Original Text: {plainText}\n"); 12 | 13 | // DES 14 | using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) 15 | { 16 | Console.WriteLine("DES"); 17 | var encrypted = Encrypt(plainText, des.CreateEncryptor(des.Key, des.IV)); 18 | var decrypted = Decrypt(encrypted, des.CreateDecryptor(des.Key, des.IV)); 19 | Show(encrypted, decrypted); 20 | } 21 | 22 | // TripleDES 23 | using (TripleDESCryptoServiceProvider tripleDes = new TripleDESCryptoServiceProvider()) 24 | { 25 | Console.WriteLine("TripleDES"); 26 | var encrypted = Encrypt(plainText, tripleDes.CreateEncryptor(tripleDes.Key, tripleDes.IV)); 27 | var decrypted = Decrypt(encrypted, tripleDes.CreateDecryptor(tripleDes.Key, tripleDes.IV)); 28 | Show(encrypted, decrypted); 29 | } 30 | 31 | // AES 32 | using (Aes aes = Aes.Create()) 33 | { 34 | Console.WriteLine("AES"); 35 | var encrypted = Encrypt(plainText, aes.CreateEncryptor(aes.Key, aes.IV)); 36 | var decrypted = Decrypt(encrypted, aes.CreateDecryptor(aes.Key, aes.IV)); 37 | Show(encrypted, decrypted); 38 | } 39 | 40 | // RSA 41 | using (RSA rsa = RSA.Create()) 42 | { 43 | Console.WriteLine("RSA"); 44 | byte[] encrypted = rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), RSAEncryptionPadding.Pkcs1); 45 | byte[] decrypted = rsa.Decrypt(encrypted, RSAEncryptionPadding.Pkcs1); 46 | Console.WriteLine($"Encrypted (Base64): {Convert.ToBase64String(encrypted)}"); 47 | Console.WriteLine($"Decrypted: {Encoding.UTF8.GetString(decrypted)}\n"); 48 | } 49 | 50 | // SHA256 51 | using (SHA256 sha256 = SHA256.Create()) 52 | { 53 | Console.WriteLine("SHA256 (Hash)"); 54 | byte[] hash = sha256.ComputeHash(Encoding.UTF8.GetBytes(plainText)); 55 | Console.WriteLine($"Hashed (Base64): {Convert.ToBase64String(hash)}\n"); 56 | } 57 | } 58 | 59 | static byte[] Encrypt(string plainText, ICryptoTransform encryptor) 60 | { 61 | using MemoryStream ms = new(); 62 | using CryptoStream cs = new(ms, encryptor, CryptoStreamMode.Write); 63 | using StreamWriter sw = new(cs); 64 | sw.Write(plainText); 65 | sw.Close(); 66 | return ms.ToArray(); 67 | } 68 | 69 | static string Decrypt(byte[] cipherText, ICryptoTransform decryptor) 70 | { 71 | using MemoryStream ms = new(cipherText); 72 | using CryptoStream cs = new(ms, decryptor, CryptoStreamMode.Read); 73 | using StreamReader sr = new(cs); 74 | return sr.ReadToEnd(); 75 | } 76 | 77 | static void Show(byte[] encrypted, string decrypted) 78 | { 79 | Console.WriteLine($"Encrypted (Base64): {Convert.ToBase64String(encrypted)}"); 80 | Console.WriteLine($"Decrypted: {decrypted}\n"); 81 | } 82 | } 83 | 84 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Crypto/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Crypto/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Dijkstra/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Program 4 | { 5 | static int inf = 9999; 6 | 7 | static int[,] adj = { 8 | {0, 5, 6, 0, 0, 0}, 9 | {5, 0, 6, 3, 5, 0}, 10 | {6, 6, 0, 0, 2, 0}, 11 | {0, 3, 0, 0, 3, 4}, 12 | {0, 5, 2, 3, 0, 1}, 13 | {0, 0, 0, 4, 1, 0} 14 | }; 15 | 16 | static void Dijkstra(int start) 17 | { 18 | int n = adj.GetLength(0); 19 | int[] distance = new int[n]; 20 | bool[] explored = new bool[n]; 21 | int[] path = new int[n]; 22 | 23 | for (int i = 0; i < n; i++) 24 | { 25 | distance[i] = inf; 26 | explored[i] = false; 27 | } 28 | 29 | distance[start] = 0; 30 | path[start] = -1; 31 | 32 | for (int c = 0; c < n; c++) 33 | { 34 | int min = inf; 35 | int i = 0; 36 | 37 | for (int j = 0; j < n; j++) 38 | { 39 | if (!explored[j] && distance[j] < min) 40 | { 41 | min = distance[j]; 42 | i = j; 43 | } 44 | } 45 | 46 | explored[i] = true; 47 | 48 | for (int j = 0; j < n; j++) 49 | { 50 | if (!explored[j] && adj[i, j] != 0 && distance[i] != inf && 51 | distance[i] + adj[i, j] < distance[j]) 52 | { 53 | distance[j] = distance[i] + adj[i, j]; 54 | path[j] = i; 55 | } 56 | } 57 | } 58 | 59 | Console.WriteLine("Node\tDistance\tPath"); 60 | for (int i = 0; i < n; i++) 61 | { 62 | Console.WriteLine($"{i}\t{distance[i]}\t\t{path[i]}"); 63 | } 64 | 65 | } 66 | 67 | static void Main(string[] args) 68 | { 69 | Dijkstra(0); 70 | } 71 | } 72 | 73 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Dijkstra/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Dijkstra/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Dijkstra/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Dijkstra/graph.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Search/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Search/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Search/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Search/graph.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Traversal/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Graph 4 | { 5 | int n; 6 | int[,] adj; 7 | 8 | public Graph(int n_) 9 | { 10 | n = n_; 11 | adj = new int[n_, n_]; 12 | } 13 | 14 | 15 | public void Add(int source, int destination) 16 | { 17 | adj[source, destination] = 1; 18 | } 19 | 20 | public void BreadthTraversal(int start) 21 | { 22 | bool[] visited = new bool[n]; 23 | for (int i = 0; i < n; i++) 24 | visited[i] = false; 25 | 26 | int[] queue = new int[n]; 27 | int front = 0, end = 0; 28 | 29 | visited[start] = true; 30 | queue[end++] = start; 31 | 32 | while (front < end) 33 | { 34 | int node = queue[front++]; 35 | Console.Write(node + " "); 36 | 37 | for (int i = 0; i < n; i++) 38 | { 39 | if (adj[node, i] == 1 && !visited[i]) 40 | { 41 | visited[i] = true; 42 | queue[end++] = i; 43 | } 44 | } 45 | } 46 | } 47 | 48 | public void DepthTraversal(int start) 49 | { 50 | bool[] visited = new bool[n]; 51 | for (int i = 0; i < n; i++) 52 | visited[i] = false; 53 | 54 | int[] stack = new int[n]; 55 | int head = -1; 56 | 57 | visited[start] = true; 58 | stack[++head] = start; 59 | 60 | while (head >= 0) 61 | { 62 | int node = stack[head--]; 63 | Console.Write(node + " "); 64 | 65 | for (int i = 0; i < n; i++) 66 | { 67 | if (adj[node, i] == 1 && !visited[i]) 68 | { 69 | visited[i] = true; 70 | stack[++head] = i; 71 | } 72 | } 73 | } 74 | } 75 | } 76 | 77 | class Program 78 | { 79 | static void Main() 80 | { 81 | Graph g = new Graph(8); 82 | g.Add(0, 1); g.Add(0, 4); 83 | g.Add(1, 0); g.Add(1, 2); g.Add(1, 4); 84 | g.Add(2, 3); g.Add(2, 5); 85 | g.Add(3, 6); g.Add(3, 7); 86 | g.Add(4, 2); g.Add(4, 5); 87 | g.Add(5, 2); g.Add(5, 6); 88 | g.Add(6, 2); g.Add(6, 7); 89 | 90 | Console.WriteLine("Breadth Traversal:"); 91 | g.BreadthTraversal(0); 92 | Console.WriteLine(); 93 | 94 | Console.WriteLine("Depth Traversal:"); 95 | g.DepthTraversal(0); 96 | Console.WriteLine(); 97 | } 98 | } 99 | 100 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Traversal/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Traversal/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/Traversal/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/Traversal/graph.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/UniformCost/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | class Program 6 | { 7 | static List<(char, int)> Successors((char, int) n) 8 | { 9 | switch (n.Item1) 10 | { 11 | case 'A': 12 | return new List<(char, int)> { ('B', n.Item2 + 5), ('C', n.Item2 + 6) }; 13 | case 'B': 14 | return new List<(char, int)> { ('A', n.Item2 + 5), ('C', n.Item2 + 6), ('D', n.Item2 + 3), ('E', n.Item2 + 5) }; 15 | case 'C': 16 | return new List<(char, int)> { ('A', n.Item2 + 6), ('B', n.Item2 + 6), ('E', n.Item2 + 2) }; 17 | case 'D': 18 | return new List<(char, int)> { ('B', n.Item2 + 3), ('E', n.Item2 + 3), ('F', n.Item2 + 4) }; 19 | case 'E': 20 | return new List<(char, int)> { ('B', n.Item2 + 5), ('C', n.Item2 + 2), ('D', n.Item2 + 3), ('F', n.Item2 + 1) }; 21 | case 'F': 22 | return new List<(char, int)> { ('D', n.Item2 + 4), ('E', n.Item2 + 1) }; 23 | default: 24 | return new List<(char, int)>(); 25 | } 26 | } 27 | 28 | static void UniformCost(char start, char end) 29 | { 30 | var frontier = new List<(char, int)> { (start, 0) }; 31 | 32 | while (frontier.Any()) 33 | { 34 | var current = frontier.First(); 35 | frontier.RemoveAt(0); 36 | 37 | Console.WriteLine($"Current: ({current.Item1}, {current.Item2})"); 38 | 39 | if (current.Item1 == end) 40 | { 41 | Console.WriteLine("SOLUTION"); 42 | return; 43 | } 44 | 45 | var temp = Successors(current); 46 | 47 | string tempString = string.Join(", ", temp.Select(t => $"({t.Item1}, {t.Item2})")); 48 | Console.WriteLine($"Succesors of {current.Item1}: {tempString}"); 49 | 50 | frontier.AddRange(temp); 51 | frontier = frontier.OrderBy(x => x.Item2).ToList(); 52 | 53 | string frontierStringAfterSort = string.Join(", ", frontier.Select(f => $"({f.Item1}, {f.Item2})")); 54 | Console.WriteLine($"Frontier: {frontierStringAfterSort}"); 55 | } 56 | 57 | Console.WriteLine("NO-SOLUTION"); 58 | } 59 | 60 | static void Main() 61 | { 62 | UniformCost('A', 'F'); 63 | } 64 | } 65 | 66 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/UniformCost/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/UniformCost/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Graph/UniformCost/graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Graph/UniformCost/graph.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Hash/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | class Hash 4 | { 5 | public int n, m; 6 | public int[] h; 7 | public int min, max; 8 | 9 | public Hash(int m, int min, int max) 10 | { 11 | this.m = m; 12 | this.min = min; 13 | this.max = max; 14 | Init(); 15 | } 16 | 17 | public int Division(int k) 18 | { 19 | return k % m; 20 | } 21 | 22 | public int Linear(int k) 23 | { 24 | return (k + 1) % m; 25 | } 26 | 27 | public void Init() 28 | { 29 | n = 0; 30 | h = new int[m]; 31 | for (int i = 0; i < m; i++) 32 | h[i] = -1; 33 | } 34 | 35 | public void Insert(int k) 36 | { 37 | int i = Division(k); 38 | while (h[i] != -1) 39 | i = Linear(i); 40 | h[i] = k; 41 | n++; 42 | Rehashing(); 43 | } 44 | 45 | public void Rehashing() 46 | { 47 | if ((n * 100 / m) >= max) 48 | { 49 | // Array copy 50 | int[] temp = new int[m]; 51 | Array.Copy(h, temp, m); 52 | Print(); 53 | 54 | // Rehashing 55 | int mprev = m; 56 | m = n * 100 / min; 57 | Init(); 58 | for (int i = 0; i < mprev; i++) 59 | if (temp[i] != -1) 60 | Insert(temp[i]); 61 | } 62 | else 63 | { 64 | Print(); 65 | } 66 | } 67 | 68 | public void Print() 69 | { 70 | Console.Write("["); 71 | for (int i = 0; i < m; i++) 72 | Console.Write(" " + h[i]); 73 | Console.WriteLine(" ] " + (n * 100 / m) + "%"); 74 | } 75 | } 76 | 77 | class Program 78 | { 79 | static void Main() 80 | { 81 | Hash hash = new Hash(5, 20, 80); // m, min, max 82 | hash.Insert(5); 83 | hash.Insert(10); 84 | hash.Insert(15); 85 | hash.Insert(20); 86 | hash.Insert(25); 87 | hash.Insert(30); 88 | } 89 | } 90 | 91 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Hash/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Hash/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Huffman/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Huffman/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Huffman/huffman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Huffman/huffman.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Merkle/Hash.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text; 3 | using System.Security.Cryptography; 4 | 5 | class Program 6 | { 7 | static void Main() 8 | { 9 | string input = "Hola mundo"; 10 | string hash = ComputeSha256Hash(input); 11 | Console.WriteLine($"SHA-256: {hash}"); 12 | } 13 | 14 | static string ComputeSha256Hash(string rawData) 15 | { 16 | using (SHA256 sha256Hash = SHA256.Create()) 17 | { 18 | // Convertir a bytes y calcular hash 19 | byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData)); 20 | 21 | // Convertir a string hexadecimal 22 | StringBuilder builder = new StringBuilder(); 23 | foreach (var b in bytes) 24 | builder.Append(b.ToString("x2")); 25 | 26 | return builder.ToString(); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Merkle/dotnet-run.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Merkle/dotnet-run.png -------------------------------------------------------------------------------- /csharp/04-nonlinear-structures/Merkle/merkle.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/04-nonlinear-structures/Merkle/merkle.png -------------------------------------------------------------------------------- /csharp/README.md: -------------------------------------------------------------------------------- 1 | # Install .NET in Arch Linux 2 | 3 | 4 | Upgrade the system: 5 | 6 | ``` 7 | sudo pacman -Syu 8 | ``` 9 | 10 | Install dependencies: 11 | 12 | ``` 13 | sudo pacman -Sy \ 14 | glibc \ 15 | gcc \ 16 | krb5 \ 17 | icu \ 18 | openssl \ 19 | libc++ \ 20 | zlib 21 | ``` 22 | 23 | Download the installation script: 24 | 25 | ``` 26 | wget https://dot.net/v1/dotnet-install.sh -O dotnet-install.sh 27 | ``` 28 | 29 | Grant execute permission to the script: 30 | 31 | ``` 32 | chmod +x ./dotnet-install.sh 33 | ``` 34 | 35 | Run the script to install the latest version: 36 | ``` 37 | ./dotnet-install.sh --version latest 38 | ``` 39 | 40 | Install the .NET Runtime: 41 | ``` 42 | ./dotnet-install.sh --version latest --runtime aspnetcore 43 | ``` 44 | 45 | You can install a specific major version: 46 | ``` 47 | ./dotnet-install.sh --channel 9.0 48 | ``` 49 | 50 | Set DOTNET_ROOT environment variable: 51 | ``` 52 | export DOTNET_ROOT=$HOME/.dotnet 53 | ``` 54 | 55 | Update the PATH environment variable: 56 | ``` 57 | export PATH=$PATH:$DOTNET_ROOT:$DOTNET_ROOT/tools 58 | ``` 59 | 60 | When running export, the changes will only be reflected for the current session. If you want the environment variables to always be loaded, you can use the bashrc file: 61 | 62 | ``` 63 | nano ~/.bashrc 64 | ``` 65 | 66 | The two exports must be added to the end of the file, then saved and exited. If you want to apply the changes immediately: 67 | 68 | ``` 69 | source ~/.bashrc 70 | ``` 71 | 72 | 73 | Check the installed version: 74 | ``` 75 | dotnet --version 76 | ``` 77 | 78 | ![Alt text](https://github.com/luisespino/data-structures/blob/master/csharp/img/screenshot1.png?raw=true "version") 79 | 80 | Install the GTK library (for GUI applications): 81 | ``` 82 | sudo pacman -S gtk3 83 | ``` 84 |   85 | 86 | # Create a graphical 'Hello, World!' program 87 | 88 | Create a new project: 89 | ``` 90 | dotnet new console -n HelloWorld 91 | cd HelloWorld 92 | ``` 93 | 94 | Add the GtkSharp package to the project: 95 | ``` 96 | dotnet add package GtkSharp 97 | ``` 98 | 99 | Edit Program.cs with your favorite text editor: 100 | ``` 101 | using Gtk; 102 | 103 | class Program 104 | { 105 | public static void Main() 106 | { 107 | // Init GTK 108 | Application.Init(); 109 | 110 | // Create main window 111 | Window window = new Window("HelloWorld"); 112 | window.Resize(250, 150); 113 | 114 | // Create label 115 | Label label = new Label("Hello, World!"); 116 | 117 | // Add label to window 118 | window.Add(label); 119 | 120 | // Show components 121 | window.ShowAll(); 122 | 123 | // Connect close event 124 | window.DeleteEvent += (o, e) => { Application.Quit(); }; 125 | 126 | // Run application 127 | Application.Run(); 128 | } 129 | } 130 | ``` 131 | 132 | Compile and run the application: 133 | ``` 134 | dotnet build 135 | dotnet run 136 | ``` 137 | 138 | ![Alt text](https://github.com/luisespino/data-structures/blob/master/csharp/img/screenshot2.png?raw=true "version") 139 | 140 | You can create a self-contained bundle of the application (for .NET 9.0 and Linux x64): 141 | ``` 142 | dotnet publish -c Release -r linux-x64 --self-contained 143 | cd bin/Release/net9.0/linux-x64/publish 144 | chmod +x HelloWorld 145 | ./HelloWorld 146 | ``` 147 | 148 | You can also use Visual Studio Code with the C# Dev Kit: 149 | 150 | ![Alt text](https://github.com/luisespino/data-structures/blob/master/csharp/img/screenshot3.png?raw=true "version") 151 | -------------------------------------------------------------------------------- /csharp/img/screenshot1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/img/screenshot1.png -------------------------------------------------------------------------------- /csharp/img/screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/img/screenshot2.png -------------------------------------------------------------------------------- /csharp/img/screenshot3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/csharp/img/screenshot3.png -------------------------------------------------------------------------------- /fortran/README.md: -------------------------------------------------------------------------------- 1 | # Código de Estructuras de Datos en Fortran 2 | -------------------------------------------------------------------------------- /fortran/linear-structures/linkedlist/README.md: -------------------------------------------------------------------------------- 1 | ### Para compilar el código ejecutar: 2 | 3 | - gfortran -c listdef.f90 4 | - gfortran -c list.f90 5 | - gfortran listdef.o list.o -o list 6 | -------------------------------------------------------------------------------- /fortran/linear-structures/linkedlist/list.f90: -------------------------------------------------------------------------------- 1 | program list 2 | use listdef 3 | implicit none 4 | type(linkedlist) :: l 5 | integer :: i 6 | do i = 1, 5, 1 7 | call l%add(i) 8 | end do 9 | call l%show() 10 | end program list 11 | -------------------------------------------------------------------------------- /fortran/linear-structures/linkedlist/listdef.f90: -------------------------------------------------------------------------------- 1 | module listdef 2 | implicit none 3 | type :: node 4 | integer :: value 5 | type(node), pointer :: next 6 | end type 7 | 8 | type :: linkedlist 9 | type(node), pointer :: head => null() 10 | contains 11 | procedure :: add 12 | procedure :: show 13 | end type 14 | 15 | contains 16 | subroutine add(this, value) 17 | class(linkedlist), intent(inout) :: this 18 | integer, intent(in) :: value 19 | type(node), pointer :: tmp 20 | allocate(tmp) 21 | tmp%value = value 22 | tmp%next => this%head 23 | this%head => tmp 24 | end subroutine add 25 | 26 | subroutine show(this) 27 | class(linkedlist), intent(inout) :: this 28 | type(node), pointer :: tmp 29 | tmp => this%head 30 | do 31 | if( .not. associated(tmp)) then 32 | exit 33 | end if 34 | print *, tmp%value 35 | tmp => tmp%next 36 | end do 37 | end subroutine show 38 | 39 | end module listdef 40 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/avl/README.md: -------------------------------------------------------------------------------- 1 | ### Para compilar el código ejecutar: 2 | 3 | - gfortran -c avldef.f90 4 | - gfortran -c avl.f90 5 | - gfortran avldef.o avl.o -o avl 6 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/avl/avl.f90: -------------------------------------------------------------------------------- 1 | program avltree 2 | use avldef 3 | implicit none 4 | type(avl) :: t 5 | integer :: unit 6 | character(len=100) :: filename 7 | filename = 'output.dot' 8 | open(unit, file=filename, status='replace') 9 | call t%add(5) 10 | call t%add(10) 11 | call t%add(15) 12 | call t%add(20) 13 | call t%add(25) 14 | call t%add(30) 15 | call t%add(35) 16 | 17 | print *, 'Preorder:' 18 | call t%preorder(t%root) 19 | print *, '' 20 | print *, 'Inorder:' 21 | call t%inorder(t%root) 22 | print *, '' 23 | print *, 'Postorder:' 24 | call t%postorder(t%root) 25 | print *, '' 26 | 27 | print *, 'Generating Dot file...' 28 | call t%dotgen(t%root, unit) 29 | close(unit) 30 | print *, 'Dot file generated:', trim(filename) 31 | call execute_command_line('dot -Tsvg output.dot > output.svg') 32 | call execute_command_line('eog output.svg') 33 | end program avltree 34 | 35 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/avl/output.dot: -------------------------------------------------------------------------------- 1 | graph{ 2 | 4 [label=" 20"]; 3 | 4 -- 2; 4 | 4 -- 6; 5 | 2 [label=" 10"]; 6 | 2 -- 1; 7 | 2 -- 3; 8 | 1 [label=" 5"]; 9 | 3 [label=" 15"]; 10 | 6 [label=" 30"]; 11 | 6 -- 5; 12 | 6 -- 7; 13 | 5 [label=" 25"]; 14 | 7 [label=" 35"]; 15 | } 16 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/avl/output.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | %3 11 | 12 | 13 | 14 | 4 15 | 16 |   20 17 | 18 | 19 | 20 | 2 21 | 22 |   10 23 | 24 | 25 | 26 | 4--2 27 | 28 | 29 | 30 | 31 | 6 32 | 33 |   30 34 | 35 | 36 | 37 | 4--6 38 | 39 | 40 | 41 | 42 | 1 43 | 44 |    5 45 | 46 | 47 | 48 | 2--1 49 | 50 | 51 | 52 | 53 | 3 54 | 55 |   15 56 | 57 | 58 | 59 | 2--3 60 | 61 | 62 | 63 | 64 | 5 65 | 66 |   25 67 | 68 | 69 | 70 | 6--5 71 | 72 | 73 | 74 | 75 | 7 76 | 77 |   35 78 | 79 | 80 | 81 | 6--7 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/bst/README.md: -------------------------------------------------------------------------------- 1 | ### Para compilar el código ejecutar: 2 | 3 | - gfortran -c bstdef.f90 4 | - gfortran -c bst.f90 5 | - gfortran bstdef.o bst.o -o bst 6 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/bst/bst.f90: -------------------------------------------------------------------------------- 1 | program bstree 2 | use bstdef 3 | implicit none 4 | type(bst) :: t 5 | integer :: unit 6 | character(len=100) :: filename 7 | filename = 'output.dot' 8 | open(unit, file=filename, status='replace') 9 | call t%add(25) 10 | call t%add(10) 11 | call t%add(35) 12 | call t%add(5) 13 | call t%add(20) 14 | call t%add(30) 15 | call t%add(40) 16 | 17 | print *, 'Preorder:' 18 | call t%preorder(t%root) 19 | print *, '' 20 | print *, 'Inorder:' 21 | call t%inorder(t%root) 22 | print *, '' 23 | print *, 'Postorder:' 24 | call t%postorder(t%root) 25 | print *, '' 26 | 27 | print *, 'Generating Dot file...' 28 | call t%dotgen(t%root, unit) 29 | close(unit) 30 | print *, 'Dot file generated:', trim(filename) 31 | call execute_command_line('dot -Tsvg output.dot > output.svg') 32 | call execute_command_line('eog output.svg') 33 | end program bstree 34 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/bst/bstdef.90: -------------------------------------------------------------------------------- 1 | module bstdef 2 | implicit none 3 | integer :: id = 1 4 | type :: node 5 | integer :: value 6 | integer :: uid 7 | type(node), pointer :: left => null() 8 | type(node), pointer :: right => null() 9 | end type 10 | 11 | type :: bst 12 | type(node), pointer :: root => null() 13 | contains 14 | procedure :: add 15 | procedure :: add_rec 16 | procedure :: preorder 17 | procedure :: inorder 18 | procedure :: postorder 19 | procedure :: dotgen 20 | procedure :: dotgen_rec 21 | end type 22 | 23 | contains 24 | subroutine add(this, value) 25 | class(bst), intent(inout) :: this 26 | integer, intent(in) :: value 27 | type(node), pointer :: tmp 28 | if(associated(this%root)) then 29 | call this%add_rec(value, this%root) 30 | else 31 | allocate(tmp) 32 | tmp%value = value 33 | tmp%uid = id 34 | id = id + 1 35 | this%root => tmp 36 | end if 37 | end subroutine add 38 | 39 | subroutine add_rec(this, value, tmp) 40 | class(bst), intent(inout) :: this 41 | integer, intent(in) :: value 42 | class(node), intent(inout) :: tmp 43 | if (value < tmp%value) then 44 | if (associated(tmp%left)) then 45 | call this%add_rec(value, tmp%left) 46 | else 47 | allocate(tmp%left) 48 | tmp%left%value = value 49 | tmp%left%uid = id 50 | id = id + 1 51 | end if 52 | else 53 | if (associated(tmp%right)) then 54 | call this%add_rec(value, tmp%right) 55 | else 56 | allocate(tmp%right) 57 | tmp%right%value = value 58 | tmp%right%uid = id 59 | id = id + 1 60 | end if 61 | end if 62 | end subroutine add_rec 63 | 64 | subroutine preorder(this, tmp) 65 | class(bst), intent(in) :: this 66 | class(node), intent(in), pointer :: tmp 67 | if( .not. associated(tmp)) then 68 | return 69 | end if 70 | write (*, '(1I3)', advance='no') (tmp%value) 71 | call this%preorder(tmp%left) 72 | call this%preorder(tmp%right) 73 | end subroutine preorder 74 | 75 | subroutine inorder(this, tmp) 76 | class(bst), intent(in) :: this 77 | class(node), intent(in), pointer :: tmp 78 | if( .not. associated(tmp)) then 79 | return 80 | end if 81 | call this%inorder(tmp%left) 82 | write (*, '(1I3)', advance='no') (tmp%value) 83 | call this%inorder(tmp%right) 84 | end subroutine inorder 85 | 86 | subroutine postorder(this, tmp) 87 | class(bst), intent(in) :: this 88 | class(node), intent(in), pointer :: tmp 89 | if( .not. associated(tmp)) then 90 | return 91 | end if 92 | call this%postorder(tmp%left) 93 | call this%postorder(tmp%right) 94 | write (*, '(1I3)', advance='no') (tmp%value) 95 | end subroutine postorder 96 | 97 | subroutine dotgen(this, tmp, unit) 98 | class(bst), intent(in) :: this 99 | class(node), intent(in), pointer :: tmp 100 | integer, intent(in) :: unit 101 | write(unit, '(A)') 'graph{' 102 | call this%dotgen_rec(tmp, unit) 103 | write(unit, '(A)') '}' 104 | end subroutine dotgen 105 | 106 | subroutine dotgen_rec(this, tmp, unit) 107 | class(bst), intent(in) :: this 108 | class(node), intent(in), pointer :: tmp 109 | integer, intent(in) :: unit 110 | if (.not. associated(tmp)) then 111 | return 112 | end if 113 | write (unit, '(A,I5,A,I5,A)') ' ', tmp%uid, ' [label="', tmp%value, '"];' 114 | if (associated(tmp%left)) then 115 | write (unit, '(A,I5,A,I5,A)') ' ', tmp%uid, ' -- ', tmp%left%uid, ';' 116 | end if 117 | if (associated(tmp%right)) then 118 | write (unit, '(A,I5,A,I5,A)') ' ', tmp%uid, ' -- ', tmp%right%uid, ';' 119 | end if 120 | call this%dotgen_rec(tmp%left, unit) 121 | call this%dotgen_rec(tmp%right, unit) 122 | end subroutine dotgen_rec 123 | 124 | end module bstdef 125 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/bst/output.dot: -------------------------------------------------------------------------------- 1 | graph{ 2 | 1 [label=" 25"]; 3 | 1 -- 2; 4 | 1 -- 3; 5 | 2 [label=" 10"]; 6 | 2 -- 4; 7 | 2 -- 5; 8 | 4 [label=" 5"]; 9 | 5 [label=" 20"]; 10 | 3 [label=" 35"]; 11 | 3 -- 6; 12 | 3 -- 7; 13 | 6 [label=" 30"]; 14 | 7 [label=" 40"]; 15 | } 16 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/bst/output.svg: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 9 | 10 | %3 11 | 12 | 13 | 14 | 1 15 | 16 |   25 17 | 18 | 19 | 20 | 2 21 | 22 |   10 23 | 24 | 25 | 26 | 1--2 27 | 28 | 29 | 30 | 31 | 3 32 | 33 |   35 34 | 35 | 36 | 37 | 1--3 38 | 39 | 40 | 41 | 42 | 4 43 | 44 |    5 45 | 46 | 47 | 48 | 2--4 49 | 50 | 51 | 52 | 53 | 5 54 | 55 |   20 56 | 57 | 58 | 59 | 2--5 60 | 61 | 62 | 63 | 64 | 6 65 | 66 |   30 67 | 68 | 69 | 70 | 3--6 71 | 72 | 73 | 74 | 75 | 7 76 | 77 |   40 78 | 79 | 80 | 81 | 3--7 82 | 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/btree/btree.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/btree/btree.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/btree/execution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/btree/execution.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/graph/longest_path_execution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/graph/longest_path_execution.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/graph/longest_path_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/graph/longest_path_graph.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/graph/traversal_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/graph/traversal_output.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/graph/uniform_cost_input_graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/graph/uniform_cost_input_graph.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/graph/uniform_cost_output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/graph/uniform_cost_output.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/hash/execution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/hash/execution.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/hash/hash.f90: -------------------------------------------------------------------------------- 1 | module hash_module 2 | implicit none 3 | 4 | type hash 5 | integer :: n ! elements 6 | integer :: m ! size table 7 | integer :: mini, maxi ! percentages 8 | integer, dimension(:), allocatable :: h ! table 9 | contains 10 | procedure :: init 11 | procedure :: division 12 | procedure :: linear 13 | procedure :: insert 14 | procedure :: rehashing 15 | procedure :: show 16 | end type hash 17 | 18 | contains 19 | 20 | subroutine init(this, m, mini, maxi) 21 | class(hash), intent(inout) :: this 22 | integer, intent(in) :: m, mini, maxi 23 | this%n = 0 24 | this%m = m 25 | this%mini = mini 26 | this%maxi = maxi 27 | if (allocated(this%h)) then 28 | deallocate(this%h) 29 | end if 30 | allocate(this%h(m)) 31 | this%h = -1 32 | end subroutine init 33 | 34 | integer function division(this, k) 35 | class(hash), intent(inout) :: this 36 | integer, intent(in) :: k 37 | division = mod(k, this%m) 38 | end function division 39 | 40 | integer function linear(this, k) 41 | class(hash), intent(inout) :: this 42 | integer, intent(in) :: k 43 | linear = mod(k + 1, this%m) 44 | end function linear 45 | 46 | subroutine insert(this, k) 47 | class(hash), intent(inout) :: this 48 | integer, intent(in) :: k 49 | integer :: i 50 | i = this%division(k) 51 | do while (this%h(i) /= -1) 52 | i = this%linear(i) 53 | end do 54 | this%h(i) = k 55 | this%n = this%n + 1 56 | call this%rehashing() 57 | end subroutine insert 58 | 59 | subroutine rehashing(this) 60 | class(hash), intent(inout) :: this 61 | integer :: i, mprev 62 | integer, dimension(:), allocatable :: temp 63 | if (this%n * 100 / this%m >= this%maxi) then 64 | allocate(temp(this%m)) 65 | temp = this%h 66 | call this%show() 67 | mprev = this%m 68 | this%m = this%n * 100 / this%mini 69 | call this%init(this%m, this%mini, this%maxi) 70 | do i = 1, mprev 71 | if (temp(i) /= -1) then 72 | call this%insert(temp(i)) 73 | end if 74 | end do 75 | else 76 | call this%show() 77 | end if 78 | end subroutine rehashing 79 | 80 | subroutine show(this) 81 | class(hash), intent(inout) :: this 82 | integer :: i 83 | write (*, '(a)', advance='no') '[' 84 | do i = 1, this%m 85 | write (*, '(1I3)', advance='no') this%h(i) 86 | end do 87 | write(*, '(A, I0, A)') '] ', (this%n * 100 / this%m), '%' 88 | end subroutine show 89 | 90 | end module hash_module 91 | 92 | program hash_main 93 | use hash_module 94 | implicit none 95 | integer :: m = 5 !size table 96 | integer :: mini = 20, maxi = 80 ! min and max percentage 97 | type(hash) :: h 98 | call h%init(m, mini, maxi) 99 | print *, 'Hash table with division method and linear probing' 100 | print *, 'Initial size table: ', m 101 | print *, 'Minimum percentage: ', mini 102 | print *, 'Maximum percentage: ', maxi 103 | call h%insert(5) 104 | call h%insert(10) 105 | call h%insert(15) 106 | call h%insert(20) 107 | call h%insert(25) 108 | call h%insert(30) 109 | 110 | end program hash_main 111 | -------------------------------------------------------------------------------- /fortran/non-linear-structures/merkle/execution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/non-linear-structures/merkle/execution.png -------------------------------------------------------------------------------- /fortran/non-linear-structures/merkle/output.dot: -------------------------------------------------------------------------------- 1 | graph{ 2 | 5 [label="4E3B4A3C"]; 3 | 5 -- 6; 4 | 5 -- 9; 5 | 6 [label="313E493B"]; 6 | 6 -- 7; 7 | 6 -- 8; 8 | 7 [label="39697C69"]; 9 | 1 [label="data1" shape=rect]; 10 | 7 -- 1; 11 | 8 [label="3A697C69"]; 12 | 2 [label="data2" shape=rect]; 13 | 8 -- 2; 14 | 9 [label="4B3F393F"]; 15 | 9 -- 10; 16 | 9 -- 11; 17 | 10 [label="3B697C69"]; 18 | 3 [label="data3" shape=rect]; 19 | 10 -- 3; 20 | 11 [label="717C7865"]; 21 | 4 [label="empty" shape=rect]; 22 | 11 -- 4; 23 | } 24 | -------------------------------------------------------------------------------- /fortran/parser/parser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/parser/parser -------------------------------------------------------------------------------- /fortran/parser/parser.f90: -------------------------------------------------------------------------------- 1 | module parser 2 | implicit none 3 | 4 | type :: attr 5 | character(len=:), allocatable :: in 6 | character(len=:), allocatable :: out 7 | end type attr 8 | 9 | contains 10 | 11 | function parse(str) result(res) 12 | character(len=:), intent(in), allocatable :: str 13 | type(attr) :: tmp 14 | character(len=:), allocatable :: res 15 | tmp%in = str 16 | tmp%out = "" 17 | tmp = s(tmp) 18 | res = tmp%out 19 | end function parse 20 | 21 | ! s = e 22 | recursive function s(tmp) result(res) 23 | type(attr), intent(in) :: tmp 24 | type(attr) :: res 25 | res = e(tmp) 26 | end function s 27 | 28 | ! e = t '+' e 29 | ! / t 30 | recursive function e(tmp) result(res) 31 | type(attr), intent(in) :: tmp 32 | type(attr) :: t1, e1, res 33 | integer :: tn, en, i, len 34 | character(10) :: str 35 | t1 = t(tmp) 36 | if (t1%in(1:1) == '+') then 37 | t1%in = t1%in(2:) 38 | e1 = e(t1) 39 | res%in = e1%in 40 | tn = iachar(t1%out(1:1)) - ichar('0') 41 | en = iachar(e1%out(1:1)) - ichar('0') 42 | i = tn + en 43 | len = 0 44 | do while (i /= 0) 45 | len = len + 1 46 | str(len:len) = achar(modulo(i, 10) + ichar('0')) 47 | i = i / 10 48 | end do 49 | str = adjustl(str(1:len)) 50 | res%out = str 51 | return 52 | end if 53 | res = t(tmp) 54 | end function e 55 | 56 | ! t = f '*' t 57 | ! / f 58 | recursive function t(tmp) result(res) 59 | type(attr), intent(in) :: tmp 60 | type(attr) :: f1, t1, res 61 | integer :: fn, tn, i, len 62 | character(10) :: str 63 | f1 = f(tmp) 64 | if (f1%in(1:1) == '*') then 65 | f1%in = f1%in(2:) 66 | t1 = t(f1) 67 | res%in = t1%in 68 | fn = iachar(f1%out(1:1)) - ichar('0') 69 | tn = iachar(t1%out(1:1)) - ichar('0') 70 | i = fn * tn 71 | len = 0 72 | do while (i /= 0) 73 | len = len + 1 74 | str(len:len) = achar(modulo(i, 10) + ichar('0')) 75 | i = i / 10 76 | end do 77 | str = adjustl(str(1:len)) 78 | res%out = str 79 | return 80 | end if 81 | res = f(tmp) 82 | end function t 83 | 84 | ! f = [0..9] 85 | recursive function f(tmp) result(res) 86 | type(attr), intent(in) :: tmp 87 | type(attr) :: f1, res 88 | integer :: num 89 | f1%in = tmp%in 90 | f1%out = tmp%out 91 | if (f1%in(1:1) >= '0' .and. f1%in(1:1) <= '9') then 92 | res%out = f1%in(1:1) 93 | f1%in = f1%in(2:) 94 | res%in = f1%in 95 | return 96 | end if 97 | end function f 98 | 99 | end module parser 100 | 101 | program main 102 | use parser 103 | implicit none 104 | character(len=:), allocatable :: in, out 105 | 106 | 107 | ! Ciclo para ingresar varias expresiones 108 | do 109 | allocate(character(len=100) :: in) 110 | ! Solicitar al usuario que ingrese una expresión 111 | print *, "Ingrese una expresión (o 'exit' para terminar):" 112 | 113 | read (*,*) in 114 | 115 | ! Si el usuario ingresa 'exit', terminar el ciclo 116 | if (in == 'exit') then 117 | print *, "Saliendo del programa..." 118 | exit 119 | end if 120 | 121 | ! Llamar a la función parse con la expresión ingresada 122 | out = parse(in) 123 | 124 | ! Imprimir el resultado 125 | print *, "Resultado: ", out 126 | deallocate(in, out) 127 | end do 128 | 129 | end program main 130 | 131 | -------------------------------------------------------------------------------- /fortran/parser/parser.mod: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/parser/parser.mod -------------------------------------------------------------------------------- /fortran/sha256/output.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/sha256/output.png -------------------------------------------------------------------------------- /fortran/test/test01_execution.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/test/test01_execution.png -------------------------------------------------------------------------------- /fortran/xdialog/xreadnwrite/console.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/xdialog/xreadnwrite/console.png -------------------------------------------------------------------------------- /fortran/xdialog/xreadnwrite/output.txt: -------------------------------------------------------------------------------- 1 | Your name is: Fortran 2 | -------------------------------------------------------------------------------- /fortran/xdialog/xreadnwrite/xreadnwrite: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/xdialog/xreadnwrite/xreadnwrite -------------------------------------------------------------------------------- /fortran/xdialog/xreadnwrite/xreadnwrite.f90: -------------------------------------------------------------------------------- 1 | program xreadnwrite 2 | implicit none 3 | character(len=100) :: user_input 4 | integer :: iunit, ios 5 | logical :: exists 6 | 7 | call system("Xdialog --inputbox 'Enter your name' 8 40 2>/tmp/user_input.txt") 8 | 9 | open(unit=iunit, file='/tmp/user_input.txt', status='old', action='read') 10 | 11 | read(iunit, '(A)', iostat=ios) user_input 12 | if (ios == -1) then 13 | print *, "Canceled" 14 | close(iunit) 15 | stop 16 | end if 17 | close(iunit) 18 | 19 | open(unit=iunit, file="output.txt", status='replace', action='write') 20 | write(iunit, '(A)') "Your name is: " // trim(adjustl(user_input)) 21 | close(iunit) 22 | 23 | print *, "Your name is: ", user_input 24 | 25 | end program xreadnwrite -------------------------------------------------------------------------------- /fortran/xdialog/xreadnwrite/xreadnwrite.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/fortran/xdialog/xreadnwrite/xreadnwrite.png -------------------------------------------------------------------------------- /go/class-diagram/mitologico/MundoMitologico.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Caballo struct { 6 | color string 7 | } 8 | 9 | func NewCaballo() *Caballo { 10 | return &Caballo{"blanco"} 11 | } 12 | 13 | func (c Caballo) caminar() {} 14 | 15 | func (c Caballo) correr() {} 16 | 17 | func (c Caballo) parar() {} 18 | 19 | type Alas interface { 20 | volar() 21 | } 22 | 23 | type Cuerno interface { 24 | cornear() 25 | } 26 | 27 | type Torso interface { 28 | hablar() 29 | } 30 | 31 | type Pegaso struct { 32 | Caballo 33 | } 34 | 35 | func (p Pegaso) volar() {} 36 | 37 | func (p Pegaso) show() { 38 | fmt.Println(p.color) 39 | } 40 | 41 | type Unicornio struct { 42 | Caballo 43 | } 44 | 45 | func (u Unicornio) cornear() {} 46 | 47 | func (u Unicornio) show() { 48 | fmt.Println(u.color) 49 | } 50 | 51 | type Centauro struct { 52 | Caballo 53 | } 54 | 55 | func (c Centauro) hablar() {} 56 | 57 | func (c Centauro) show() { 58 | fmt.Println(c.color) 59 | } 60 | 61 | type MundoMitologico struct { 62 | p Pegaso 63 | u Unicornio 64 | c Centauro 65 | } 66 | 67 | func main() { 68 | m := MundoMitologico{ 69 | Pegaso{Caballo{color: "negro"}}, 70 | Unicornio{Caballo{color: "blanco"}}, 71 | Centauro{Caballo{color: "cafe"}}, 72 | } 73 | m.p.show() 74 | m.u.show() 75 | m.c.show() 76 | } 77 | -------------------------------------------------------------------------------- /go/class-diagram/mitologico/MundoMitologico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/go/class-diagram/mitologico/MundoMitologico.jpg -------------------------------------------------------------------------------- /go/class-diagram/mitologico/README.md: -------------------------------------------------------------------------------- 1 | # Mundo mitológico: enunciado 2 | Se desean crear tres seres en un mundo mitológico: pegaso, unicornio y centauro. 3 | Los tres comparten tener la apariencia de un caballo, pegaso adicionalmente tiene alas, el unicornio tiene un cuerno y el centauro tiene torso humano. 4 | Normalmente pegaso es color blanco pero también hay representaciones en color negro, mientras que el unicornio normalmente es de color blanco y el centauro de color café. 5 | El caballo puede caminar, correr o parar. Pero la operación parar no está implementada inicialmente, ya que puede parar por su voluntad o por fuerza externa, o por otras condiciones. 6 | Además, si tiene alas puede volar si ya alcanzo cierta velocidad mediante la operación correr. Los seres con alas puede volar y tienen dos alas, los seres con cuernos pueden cornear y tiene un solo cuerno, y los seres con torso humano pueden hablar y tienen la característica de tener género masculino o femenino, las operaciones de los seres son abstractas y que cada uno debe implementar en su existencia. 7 | En el mundo mitológico existen simultáneamente los tres seres y no pueden existir en otros mundos. 8 | -------------------------------------------------------------------------------- /go/linear-structures/arraylistmod.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var a [5]List 7 | a[1%5].add(1) 8 | a[3%5].add(3) 9 | a[6%5].add(6) 10 | a[8%5].add(8) 11 | a[14%5].add(14) 12 | var i int 13 | for i = 0; i < 5; i++ { 14 | fmt.Print(i, " -> ") 15 | a[i].show() 16 | fmt.Println() 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /go/linear-structures/list.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Node struct { 6 | data int 7 | next *Node 8 | } 9 | 10 | type List struct { 11 | head *Node 12 | } 13 | 14 | func (l *List) add(data int) { 15 | tmp := &Node{data: data, next: l.head} 16 | l.head = tmp 17 | } 18 | 19 | func (l *List) show() { 20 | tmp := l.head 21 | for tmp != nil { 22 | fmt.Print(tmp.data, " ") 23 | tmp = tmp.next 24 | } 25 | } 26 | 27 | func main2() { //Go does not allow multiple main 28 | l := List{} 29 | l.add(3) 30 | l.add(9) 31 | l.add(6) 32 | l.show() 33 | } 34 | -------------------------------------------------------------------------------- /go/nonlinear-structures/bstree-of-lists.go: -------------------------------------------------------------------------------- 1 | // Luis Espino 2021 2 | // Example of Binary Search Tree of Lists 3 | 4 | package main 5 | 6 | import "fmt" 7 | 8 | // NodeList is a integer with next pointer 9 | type NodeList struct { 10 | data string 11 | next *NodeList 12 | } 13 | 14 | // List with stack insert 15 | type List struct { 16 | head *NodeList 17 | } 18 | 19 | func (l *List) add(data string) { 20 | l.head = &NodeList{data: data, next: l.head} 21 | } 22 | 23 | func (l *List) show() { 24 | tmp := l.head 25 | fmt.Print("[ ") 26 | for tmp != nil { 27 | fmt.Print(tmp.data, " ") 28 | tmp = tmp.next 29 | } 30 | fmt.Println("]") 31 | } 32 | 33 | // Node is a key with two pointers 34 | type Node struct { 35 | key int 36 | value *List 37 | left *Node 38 | right *Node 39 | } 40 | 41 | // BST is a set of sorted Nodes 42 | type BST struct { 43 | root *Node 44 | } 45 | 46 | func (bst *BST) add(key int, value *List) { 47 | bst.root = bst._add(key, value, bst.root) 48 | } 49 | 50 | func (bst BST) _add(key int, value *List, tmp *Node) *Node { 51 | if tmp == nil { 52 | return &Node{key: key, value: value} 53 | } else if key > tmp.key { 54 | tmp.right = bst._add(key, value, tmp.right) 55 | } else { 56 | tmp.left = bst._add(key, value, tmp.left) 57 | } 58 | return tmp 59 | } 60 | 61 | func (bst BST) show(tmp *Node) { 62 | if tmp != nil { 63 | bst.show(tmp.left) 64 | fmt.Print(tmp.key, " ") 65 | tmp.value.show() 66 | bst.show(tmp.right) 67 | } 68 | } 69 | 70 | func main() { 71 | bst := &BST{} 72 | tmp := &List{} 73 | tmp.add("FERNANDEZ") 74 | tmp.add("SANTIAGO") 75 | bst.add(25, tmp) 76 | tmp = &List{} 77 | tmp.add("LEMUS") 78 | tmp.add("KARLA") 79 | bst.add(5, tmp) 80 | tmp = &List{} 81 | tmp.add("CABRERA") 82 | tmp.add("ANGEL") 83 | bst.add(10, tmp) 84 | tmp = &List{} 85 | tmp.add("GONZALEZ") 86 | tmp.add("CARLOS") 87 | bst.add(41, tmp) 88 | bst.show(bst.root) 89 | } 90 | -------------------------------------------------------------------------------- /go/nonlinear-structures/bstree-of-lists.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/go/nonlinear-structures/bstree-of-lists.png -------------------------------------------------------------------------------- /go/nonlinear-structures/bstree.go: -------------------------------------------------------------------------------- 1 | // Luis Espino 2021 2 | 3 | package main 4 | 5 | import "fmt" 6 | 7 | // Node is a value with two pointers 8 | type Node struct { 9 | value int 10 | left *Node 11 | right *Node 12 | } 13 | 14 | // BST is a set of sorted Nodes 15 | type BST struct { 16 | root *Node 17 | } 18 | 19 | func (bst *BST) add(value int) { 20 | bst.root = bst._add(value, bst.root) 21 | } 22 | 23 | func (bst BST) _add(value int, tmp *Node) *Node { 24 | if tmp == nil { 25 | return &Node{value: value} 26 | } else if value > tmp.value { 27 | tmp.right = bst._add(value, tmp.right) 28 | } else { 29 | tmp.left = bst._add(value, tmp.left) 30 | } 31 | return tmp 32 | } 33 | 34 | func (bst BST) preorder(tmp *Node) { 35 | if tmp != nil { 36 | fmt.Print(tmp.value, " ") 37 | bst.preorder(tmp.left) 38 | bst.preorder(tmp.right) 39 | } 40 | } 41 | 42 | func (bst BST) inorder(tmp *Node) { 43 | if tmp != nil { 44 | bst.inorder(tmp.left) 45 | fmt.Print(tmp.value, " ") 46 | bst.inorder(tmp.right) 47 | } 48 | } 49 | 50 | func (bst BST) postorder(tmp *Node) { 51 | if tmp != nil { 52 | bst.postorder(tmp.left) 53 | bst.postorder(tmp.right) 54 | fmt.Print(tmp.value, " ") 55 | } 56 | } 57 | 58 | func main() { 59 | t := BST{} 60 | t.add(25) 61 | t.add(10) 62 | t.add(5) 63 | t.add(20) 64 | t.add(35) 65 | t.add(30) 66 | t.add(40) 67 | fmt.Print("Preorder : ") 68 | t.preorder(t.root) 69 | fmt.Println() 70 | fmt.Print("Inorder : ") 71 | t.inorder(t.root) 72 | fmt.Println() 73 | fmt.Print("Postorder : ") 74 | t.postorder(t.root) 75 | } 76 | -------------------------------------------------------------------------------- /go/order/rowmajor2d.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // row major 2d 7 | var m = [4][4]int{{0, 1, 2, 3}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}} 8 | var a [16]int 9 | var i, j int 10 | for i = 0; i < 4; i++ { 11 | for j = 0; j < 4; j++ { 12 | a[i*4+j] = m[i][j] 13 | } 14 | } 15 | for i = 0; i < 16; i++ { 16 | fmt.Print(a[i], " ") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /go/order/rowmajor3d.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | // row major 3d 7 | var m = [2][2][2]int{{{0, 4}, {1, 5}}, {{2, 6}, {3, 7}}} 8 | var a [8]int 9 | var i, j, k int 10 | for i = 0; i < 2; i++ { 11 | for j = 0; j < 2; j++ { 12 | for k = 0; k < 2; k++ { 13 | a[k+2*(j+2*i)] = m[i][j][k] 14 | } 15 | } 16 | } 17 | for i = 0; i < 8; i++ { 18 | fmt.Print(a[i], " ") 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /go/pointers/pointers.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | var a *int 7 | b := 10 8 | a = &b 9 | c := &a 10 | d := a 11 | fmt.Println("b ", b) 12 | fmt.Println("&b ", &b) 13 | fmt.Println("a ", a) 14 | fmt.Println("&a ", &a) 15 | fmt.Println("*a ", *a) 16 | fmt.Println("c ", c) 17 | fmt.Println("c ", c) 18 | fmt.Println("&c ", &c) 19 | fmt.Println("*c ", *c) 20 | fmt.Println("**c ", **c) 21 | fmt.Println("*&c ", *&c) 22 | fmt.Println("&*c ", &*c) 23 | fmt.Println("d ", d) 24 | fmt.Println("&d ", &d) 25 | fmt.Println("*d ", *d) 26 | } 27 | -------------------------------------------------------------------------------- /go/sparse-matrix/sparse.go: -------------------------------------------------------------------------------- 1 | // Sparse Matrix with ERROR in addRow() (Fix it!) 2 | 3 | package main 4 | 5 | import "fmt" 6 | 7 | //Node is a value with pointers 8 | type Node struct { 9 | value int 10 | up *Node 11 | down *Node 12 | right *Node 13 | left *Node 14 | } 15 | 16 | //Matrix is a set of Nodes 17 | type Matrix struct { 18 | head *Node 19 | } 20 | 21 | func (m *Matrix) init() { 22 | m.head = &Node{value: 0} 23 | } 24 | 25 | func (m *Matrix) addRow(row int) { 26 | tmp := m.head 27 | if tmp.down == nil { 28 | newNode := &Node{value: row} 29 | tmp.down = newNode 30 | newNode.up = tmp 31 | } else { 32 | for tmp.down != nil && tmp.down.value < row { 33 | tmp = tmp.down 34 | } 35 | if tmp.down == nil && tmp.value != row { 36 | newNode := &Node{value: row} 37 | tmp.down = newNode 38 | newNode.up = tmp 39 | } else if tmp.down != nil && tmp.down.value != row { 40 | aux := tmp.down 41 | newNode := &Node{value: row} 42 | tmp.down = newNode 43 | newNode.up = tmp 44 | newNode.down = aux 45 | aux.up = newNode 46 | } 47 | } 48 | } 49 | 50 | func (m *Matrix) addCol(col int) { 51 | tmp := m.head 52 | if tmp.right == nil { 53 | newNode := &Node{value: col} 54 | tmp.right = newNode 55 | newNode.left = tmp 56 | } else { 57 | for tmp.right != nil && tmp.right.value < col { 58 | tmp = tmp.right 59 | } 60 | if tmp.right == nil && tmp.value != col { 61 | newNode := &Node{value: col} 62 | tmp.right = newNode 63 | newNode.left = tmp 64 | } else if tmp.right != nil && tmp.right.value != col { 65 | aux := tmp.right 66 | newNode := &Node{value: col} 67 | tmp.right = newNode 68 | newNode.left = tmp 69 | newNode.right = aux 70 | aux.left = newNode 71 | } 72 | } 73 | } 74 | 75 | // fix the error 76 | func (m *Matrix) addNode(row, col, value int) { 77 | newNode := &Node{value: value} 78 | tmprow := m.head 79 | tmpcol := m.head 80 | for tmprow.down != nil { 81 | tmprow = tmprow.down 82 | if tmprow.value == row { 83 | tmprow.right = newNode 84 | newNode.left = tmprow 85 | } 86 | } 87 | for tmpcol.right != nil { 88 | tmpcol = tmpcol.right 89 | if tmpcol.value == col { 90 | tmpcol.down = newNode 91 | newNode.up = tmpcol 92 | } 93 | } 94 | } 95 | 96 | func (m *Matrix) add(row, col, value int) { 97 | m.addRow(row) 98 | m.addCol(col) 99 | m.addNode(row, col, value) 100 | } 101 | 102 | func getCol(tmp *Node) int { 103 | var col int 104 | for tmp.up != nil { 105 | tmp = tmp.up 106 | col = tmp.value 107 | } 108 | return col 109 | } 110 | 111 | func (m *Matrix) show() { 112 | tmprow := m.head 113 | for tmprow != nil { 114 | tmpcol := m.head 115 | fmt.Print(tmprow.value, ",", tmpcol.value, " ") 116 | tmpcol = tmprow.right 117 | if tmprow.value == 0 { 118 | for tmpcol != nil { 119 | fmt.Print(tmprow.value, ",", tmpcol.value, " ") 120 | tmpcol = tmpcol.right 121 | } 122 | } else { 123 | for tmpcol != nil { 124 | fmt.Print(tmprow.value, ",", getCol(tmpcol), "(", tmpcol.value, ") ") 125 | tmpcol = tmpcol.right 126 | } 127 | } 128 | tmprow = tmprow.down 129 | fmt.Println() 130 | } 131 | } 132 | 133 | func main() { 134 | matrix := Matrix{} 135 | matrix.init() 136 | matrix.add(1, 1, 9) 137 | matrix.add(2, 3, 99) 138 | matrix.add(4, 6, 999) 139 | matrix.show() 140 | } 141 | -------------------------------------------------------------------------------- /java/algorithms/Recursion.java: -------------------------------------------------------------------------------- 1 | 2 | public class Recursion { 3 | 4 | int multi(int a, int b) { 5 | if (b > 0) 6 | return a + multi(a, b-1); 7 | return 0; 8 | } 9 | 10 | int divi(int a, int b) { 11 | if (a > 1) 12 | return 1 + divi(a-b, b); 13 | return 0; 14 | } 15 | 16 | int factorial(int n) { 17 | if (n > 1) 18 | return n * factorial(n-1); 19 | return 1; 20 | } 21 | 22 | int fibonacci(int n) { 23 | if (n > 1) 24 | return fibonacci(n-1) + fibonacci(n-2); 25 | return n; 26 | } 27 | 28 | int ackermann(int m, int n) { 29 | if (m == 0) 30 | return n + 1; 31 | else if (n == 0) 32 | return ackermann(m-1, 1); 33 | else 34 | return ackermann(m-1, ackermann(m, n-1)); 35 | } 36 | 37 | boolean par(int n) { 38 | if (n == 0) 39 | return true; 40 | return impar(n - 1); 41 | } 42 | 43 | boolean impar(int n) { 44 | if (n == 0) 45 | return false; 46 | return par(n - 1); 47 | } 48 | 49 | public static void main(String [] args) { 50 | Recursion r = new Recursion(); 51 | System.out.println("3*2 : "+r.multi(3, 2)); 52 | System.out.println("6/3 : "+r.divi(6, 3)); 53 | System.out.println("fact(5) : "+r.factorial(5)); 54 | System.out.println("fibo(5) : "+r.fibonacci(5)); 55 | System.out.println("ack(2,2): "+r.ackermann(2,2)); 56 | System.out.println("impar(3): "+r.impar(3)); 57 | System.out.println("par(3) : "+r.par(3)); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /java/algorithms/Sort.java: -------------------------------------------------------------------------------- 1 | public class Sort { 2 | 3 | int a [] = {17, 10, 12, 7, 11}; 4 | 5 | void show() 6 | { 7 | System.out.print("["); 8 | for (int i=0; ia[j]) 20 | { 21 | aux = a[i]; 22 | a[i]=a[j]; 23 | a[j]=aux; 24 | show(); 25 | itera++; 26 | } 27 | System.out.println("Iteraciones: "+itera); 28 | } 29 | 30 | void selectionSort() 31 | { 32 | int itera = 0; 33 | int menor, aux; 34 | for(int i=0; ia[j]) 42 | menor = j; 43 | } 44 | aux = a[i]; 45 | a[i] = a[menor]; 46 | a[menor] = aux; 47 | } 48 | System.out.println("Iteraciones: "+itera); 49 | } 50 | 51 | void insertionSort() 52 | { 53 | int itera = 0; 54 | int aux,j; 55 | for(int i=0; i0 && a[j-1]>aux) 60 | { 61 | a[j]=a[j-1]; 62 | j--; 63 | show(); 64 | itera++; 65 | } 66 | a[j] = aux; 67 | } 68 | System.out.println("Iteraciones: "+itera); 69 | } 70 | 71 | void quickSort(int primero, int ultimo) 72 | { 73 | int central = (primero+ultimo)/2; 74 | int pivote = a[central]; 75 | int aux; 76 | int i = primero, j = ultimo; 77 | do{ 78 | while (a[i]pivote) j--; 80 | if (i<=j) 81 | { 82 | aux = a[i]; 83 | a[i] = a[j]; 84 | a[j] = aux; 85 | i++; 86 | j--; 87 | show(); 88 | } 89 | } 90 | while(i<=j); 91 | if (primero "); 15 | a[i].show(); 16 | System.out.println(); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /java/linear-structures/List.java: -------------------------------------------------------------------------------- 1 | class ListNode { 2 | Object data; 3 | ListNode next; 4 | public ListNode(Object data) { 5 | this.data = data; 6 | this.next = null; 7 | } 8 | } 9 | 10 | public class List { 11 | ListNode head; 12 | 13 | void add(Object val) { 14 | ListNode tmp = new ListNode(val); 15 | tmp.next = head; 16 | head = tmp; 17 | } 18 | 19 | void show() { 20 | ListNode tmp = head; 21 | while(tmp!=null) { 22 | System.out.print(tmp.data+" "); 23 | tmp = tmp.next; 24 | } 25 | System.out.println(); 26 | } 27 | 28 | public static void main(String [] args){ 29 | List list = new List(); 30 | list.add(5); 31 | list.add(10); 32 | list.show(); 33 | } 34 | 35 | } 36 | -------------------------------------------------------------------------------- /java/linear-structures/Queue.java: -------------------------------------------------------------------------------- 1 | class QueueNode { 2 | Object data; 3 | QueueNode prev; 4 | QueueNode next; 5 | 6 | public QueueNode(Object data) { 7 | this.data = data; 8 | this.prev = null; 9 | this.next = null; 10 | } 11 | } 12 | 13 | public class Queue { 14 | QueueNode head, tail; 15 | 16 | void push (Object data) { // push tail 17 | if (head==null) { 18 | head = tail = new QueueNode(data); 19 | } else { 20 | tail.next = new QueueNode(data); 21 | tail.next.prev = tail; 22 | tail = tail.next; 23 | } 24 | } 25 | 26 | QueueNode pop () { //pop head 27 | QueueNode tmp = null; 28 | if (head!=null) { 29 | tmp = new QueueNode(head.data); 30 | head = head.next; 31 | if (head!=null) { 32 | head.prev = null; 33 | } 34 | } 35 | return tmp; 36 | } 37 | 38 | public static void main(String [] args) { 39 | Queue queue = new Queue(); 40 | queue.push(5); 41 | queue.push(7); 42 | queue.push(10); 43 | System.out.println(queue.pop().data); 44 | System.out.println(queue.pop().data); 45 | System.out.println(queue.pop().data); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /java/linear-structures/Stack.java: -------------------------------------------------------------------------------- 1 | class StackNode { 2 | Object data; 3 | StackNode next; 4 | 5 | public StackNode(Object data) { 6 | this.data = data; 7 | this.next = null; 8 | } 9 | } 10 | 11 | public class Stack { 12 | StackNode head; 13 | 14 | void push (Object data) { 15 | StackNode tmp = new StackNode(data); 16 | tmp.next = head; 17 | head = tmp; 18 | } 19 | 20 | StackNode pop () { 21 | StackNode tmp = null; 22 | if (head!=null) { 23 | tmp = new StackNode(head.data); 24 | head = head.next; 25 | } 26 | return tmp; 27 | } 28 | 29 | public static void main(String [] args) { 30 | Stack stack = new Stack(); 31 | stack.push(5); 32 | stack.push(7); 33 | stack.push(10); 34 | System.out.println(stack.pop().data); 35 | System.out.println(stack.pop().data); 36 | System.out.println(stack.pop().data); 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /java/linear-structures/StackList.java: -------------------------------------------------------------------------------- 1 | 2 | public class StackList { 3 | 4 | public static void main(String [] args) { 5 | Stack stacklist = new Stack(); 6 | List list1 = new List(); 7 | list1.add(3); 8 | list1.add(2); 9 | list1.add(1); 10 | stacklist.push(list1); 11 | List list2 = new List(); 12 | list2.add("c"); 13 | list2.add("b"); 14 | list2.add("a"); 15 | stacklist.push(list2); 16 | ((List) stacklist.pop().data).show(); 17 | ((List) stacklist.pop().data).show(); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /java/non-linear-structures/hash/Hash.java: -------------------------------------------------------------------------------- 1 | // Luis Espino 2020 2 | 3 | public class Hash { 4 | int n, m; 5 | int [] h; 6 | int min, max; 7 | 8 | public Hash(int m, int min, int max) { 9 | this.m = m; 10 | this.min = min; 11 | this.max = max; 12 | init(); 13 | } 14 | 15 | int division(int k) { 16 | return (k % m); 17 | } 18 | 19 | int linear(int k) { 20 | return ((k+1) % m); 21 | } 22 | 23 | void init() { 24 | n = 0; 25 | h = new int[m]; 26 | for(int i=0; i=max) { 41 | //array copy 42 | int [] temp = h; 43 | print(); 44 | //rehashing 45 | int mprev = m; 46 | m = n*100/min; 47 | init(); 48 | for (int i=0; itmp.right.value) tmp = srr(tmp); 38 | else tmp = drr(tmp); 39 | } 40 | 41 | } 42 | int d, i, m; 43 | d = altura(tmp.right); 44 | i = altura(tmp.left); 45 | m = maxi(d,i); 46 | tmp.alt = m + 1; 47 | return tmp; 48 | } 49 | 50 | int altura(NodeAVL tmp) { 51 | if (tmp == null) return -1; 52 | else return tmp.alt; 53 | } 54 | 55 | int maxi(int val1, int val2) { 56 | return ((val1 > val2) ? val1 : val2); 57 | } 58 | 59 | NodeAVL srl(NodeAVL t1) { 60 | NodeAVL t2; 61 | t2 = t1.left; 62 | t1.left = t2.right; 63 | t2.right = t1; 64 | t1.alt = maxi(altura(t1.left), altura(t1.right))+1; 65 | t2.alt = maxi(altura(t2.left),t1.alt)+1; 66 | return t2; 67 | } 68 | 69 | NodeAVL srr(NodeAVL t1) { 70 | NodeAVL t2; 71 | t2 = t1.right; 72 | t1.right = t2.left; 73 | t2.left = t1; 74 | t1.alt = maxi(altura(t1.left), altura(t1.right))+1; 75 | t2.alt = maxi(altura(t2.right),t1.alt)+1; 76 | return t2; 77 | } 78 | 79 | NodeAVL drl(NodeAVL tmp) { 80 | tmp.left = srr(tmp.left); 81 | return srl(tmp); 82 | } 83 | 84 | NodeAVL drr(NodeAVL tmp) { 85 | tmp.right = srl(tmp.right); 86 | return srr(tmp); 87 | } 88 | 89 | void preorder(NodeAVL tmp) { 90 | if (tmp != null) { 91 | System.out.print(tmp.value+" "); 92 | preorder(tmp.left); 93 | preorder(tmp.right); 94 | } 95 | } 96 | 97 | void enorder(NodeAVL tmp) { 98 | if (tmp != null) { 99 | enorder(tmp.left); 100 | System.out.print(tmp.value+" "); 101 | enorder(tmp.right); 102 | } 103 | } 104 | 105 | void postorder(NodeAVL tmp) { 106 | if (tmp != null) { 107 | postorder(tmp.left); 108 | postorder(tmp.right); 109 | System.out.print(tmp.value+" "); 110 | } 111 | } 112 | 113 | public static void main(String[] args) { 114 | AVL a = new AVL(); 115 | a.add(5);a.add(10); 116 | a.add(20);a.add(25); 117 | a.add(30);a.add(35); 118 | a.add(40); 119 | a.preorder(a.root); 120 | System.out.println(); 121 | a.enorder(a.root); 122 | System.out.println(); 123 | a.postorder(a.root); 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /java/tree-structures/BST.java: -------------------------------------------------------------------------------- 1 | // Luis Espino 2022 2 | 3 | class Node { 4 | int value; 5 | Node left; 6 | Node right; 7 | 8 | Node(int value){ 9 | this.value = value; 10 | left = null; 11 | right = null; 12 | } 13 | } 14 | 15 | 16 | public class BST { 17 | Node root = null; 18 | 19 | void add(int value) { 20 | if (root != null) 21 | add(value, root); 22 | else 23 | root = new Node(value); 24 | } 25 | 26 | void add(int value, Node tmp) { 27 | if (value < tmp.value) { 28 | if (tmp.left != null) 29 | add(value, tmp.left); 30 | else 31 | tmp.left = new Node(value); 32 | } 33 | else { 34 | if (tmp.right != null) 35 | add(value, tmp.right); 36 | else 37 | tmp.right = new Node(value); 38 | } 39 | } 40 | 41 | void preorder(Node tmp) { 42 | if (tmp != null) { 43 | System.out.print(tmp.value+" "); 44 | preorder(tmp.left); 45 | preorder(tmp.right); 46 | } 47 | } 48 | 49 | void enorder(Node tmp) { 50 | if (tmp != null) { 51 | enorder(tmp.left); 52 | System.out.print(tmp.value+" "); 53 | enorder(tmp.right); 54 | } 55 | } 56 | 57 | void postorder(Node tmp) { 58 | if (tmp != null) { 59 | postorder(tmp.left); 60 | postorder(tmp.right); 61 | System.out.print(tmp.value+" "); 62 | } 63 | } 64 | 65 | public static void main(String[] args) { 66 | BST bst = new BST(); 67 | bst.add(25);bst.add(10); 68 | bst.add(35);bst.add(5); 69 | bst.add(20);bst.add(30); 70 | bst.add(40); 71 | bst.preorder(bst.root); 72 | System.out.println(); 73 | bst.enorder(bst.root); 74 | System.out.println(); 75 | bst.postorder(bst.root); 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /java/tree-structures/BST.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/java/tree-structures/BST.png -------------------------------------------------------------------------------- /javascript/README.md: -------------------------------------------------------------------------------- 1 | Javascript examples: 2 | 3 | [Class diagram implementation](https://luisespino.github.io/data-structures/javascript/class-diagram/mitologico/mitologico.html) 4 | 5 | [Linked List](https://luisespino.github.io/data-structures/javascript/lineal-structures/list.html) 6 | 7 | [Array List Mod](https://luisespino.github.io/data-structures/javascript/lineal-structures/array-list-mod.html) 8 | 9 | [BST Tree](https://luisespino.github.io/data-structures/javascript/nonlineal-structures/bst.html) 10 | 11 | [AVL Tree](https://luisespino.github.io/data-structures/javascript/nonlineal-structures/avl.html) 12 | 13 | [Merkle Tree](https://luisespino.github.io/data-structures/javascript/nonlineal-structures/merkle.html) 14 | 15 | [Breadth Depth Search](https://luisespino.github.io/data-structures/javascript/graph/breadth_depth_search.html) 16 | 17 | [Uniform Cost](https://luisespino.github.io/data-structures/javascript/graph/uniform_cost.html) 18 | -------------------------------------------------------------------------------- /javascript/class-diagram/mitologico/README.md: -------------------------------------------------------------------------------- 1 | # Mundo mitológico: enunciado 2 | Se desean crear tres seres en un mundo mitológico: pegaso, unicornio y centauro. 3 | Los tres comparten tener la apariencia de un caballo, pegaso adicionalmente tiene alas, el unicornio tiene un cuerno y el centauro tiene torso humano. 4 | Normalmente pegaso es color blanco pero también hay representaciones en color negro, mientras que el unicornio normalmente es de color blanco y el centauro de color café. 5 | El caballo puede caminar, correr o parar. Pero la operación parar no está implementada inicialmente, ya que puede parar por su voluntad o por fuerza externa, o por otras condiciones. 6 | Además, si tiene alas puede volar si ya alcanzo cierta velocidad mediante la operación correr. Los seres con alas puede volar y tienen dos alas, los seres con cuernos pueden cornear y tiene un solo cuerno, y los seres con torso humano pueden hablar y tienen la característica de tener género masculino o femenino, las operaciones de los seres son abstractas y que cada uno debe implementar en su existencia. 7 | En el mundo mitológico existen simultáneamente los tres seres y no pueden existir en otros mundos. 8 | -------------------------------------------------------------------------------- /javascript/class-diagram/mitologico/mitologico.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Class diagram

4 |

5 | 6 | 7 | -------------------------------------------------------------------------------- /javascript/class-diagram/mitologico/mitologico.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/javascript/class-diagram/mitologico/mitologico.jpg -------------------------------------------------------------------------------- /javascript/class-diagram/mitologico/mitologico.js: -------------------------------------------------------------------------------- 1 | // Autor: Luis Espino 2 | // 2022 3 | 4 | // abstract class and interfaces are not supported, 5 | // but abstract class can use throw new Error 6 | // and interfaces can use prototype "Duck typing" 7 | 8 | // abstract 9 | class Caballo{ 10 | constructor() { 11 | this.color = "blanco" 12 | } 13 | caminar() { 14 | // ... 15 | } 16 | correr() { 17 | // ... 18 | } 19 | parar() { 20 | throw new Error("Method 'parar()' must be implemented.") 21 | } 22 | } 23 | 24 | // multiple inheritance is not supported, 25 | // but double inheritance is possible 26 | 27 | // interface 28 | class Alas extends Caballo { 29 | constructor() { 30 | super() 31 | } 32 | volar() { 33 | throw new Error("Method 'volar()' must be implemented.") 34 | } 35 | } 36 | 37 | // interface 38 | class Cuerno extends Caballo { 39 | constructor() { 40 | super() 41 | } 42 | cornear() { 43 | throw new Error("Method 'cornear()' must be implemented.") 44 | } 45 | } 46 | 47 | // interface 48 | class Torso extends Caballo { 49 | constructor() { 50 | super() 51 | } 52 | hablar() { 53 | throw new Error("Method 'hablar()' must be implemented.") 54 | } 55 | } 56 | 57 | class Pegaso extends Alas { 58 | constructor(color) { 59 | super() 60 | this.color = color 61 | } 62 | show() { 63 | document.getElementById("log").innerHTML+=this.color+"
" 64 | } 65 | parar() { 66 | // ... 67 | } 68 | volar() { 69 | // ... 70 | } 71 | } 72 | 73 | class Unicornio extends Cuerno{ 74 | constructor () { 75 | super() 76 | } 77 | parar() { 78 | // ... 79 | } 80 | cornear() { 81 | // ... 82 | } 83 | show() { 84 | document.getElementById("log").innerHTML+=this.color+"
" 85 | } 86 | } 87 | 88 | class Centauro extends Torso{ 89 | constructor (color) { 90 | super(); 91 | this.color = color 92 | } 93 | parar() { 94 | // ... 95 | } 96 | hablar() { 97 | // ... 98 | } 99 | show() { 100 | document.getElementById("log").innerHTML+=this.color+"
" 101 | } 102 | } 103 | 104 | class MundoMitologico { 105 | constructor() { 106 | this.p = new Pegaso("negro") 107 | this.u = new Unicornio() 108 | this.c = new Centauro("cafe") 109 | } 110 | } 111 | 112 | document.getElementById("log").innerHTML+="constructors result:
" 113 | var m = new MundoMitologico() 114 | m.p.show() 115 | m.u.show() 116 | m.c.show() 117 | 118 | -------------------------------------------------------------------------------- /javascript/graph/breadth_depth_search.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Breadht and Depth Search

4 |

5 | 6 | 7 | -------------------------------------------------------------------------------- /javascript/graph/breadth_depth_search.js: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2020 Luis Espino 3 | 4 | function successors(n){ 5 | if (n == 1) return [2, 4, 5]; 6 | if (n == 2) return [1, 3, 4, 5, 6]; 7 | if (n == 3) return [2, 5, 6]; 8 | if (n == 4) return [1, 2, 5, 7, 8]; 9 | if (n == 5) return [1, 2, 3, 4, 6, 7 ,8, 9]; 10 | if (n == 6) return [2, 3, 5, 8 ,9]; 11 | if (n == 7) return [4, 5, 8]; 12 | if (n == 8) return [4, 5, 6 ,7, 9]; 13 | if (n == 9) return [5, 6 , 8]; 14 | } 15 | 16 | function breadth(start, end){ 17 | document.getElementById("log").innerHTML+="
".concat("

Breadth First Search

"); 18 | var list = [start]; 19 | while (list.length > 0){ 20 | var current = list.shift(); 21 | document.getElementById("log").innerHTML+="
".concat(current); 22 | if (current == end) { 23 | document.getElementById("log").innerHTML+="
".concat("GOAL FOUND"); 24 | return; 25 | } 26 | var temp = successors(current); 27 | document.getElementById("log").innerHTML+="
".concat(temp); 28 | list = list.concat(temp); 29 | document.getElementById("log").innerHTML+="
".concat(list); 30 | } 31 | document.getElementById("log").innerHTML+="
".concat("GOAL NOT FOUND"); 32 | } 33 | 34 | function depth(start, end){ 35 | document.getElementById("log").innerHTML+="

".concat("

Depth First Search (reverse)

"); 36 | var list = [start]; 37 | while (list.length > 0){ 38 | var current = list.shift(); 39 | document.getElementById("log").innerHTML+="
".concat(current); 40 | if (current == end) { 41 | document.getElementById("log").innerHTML+="
".concat("GOAL FOUND"); 42 | return; 43 | } 44 | var temp = successors(current); 45 | temp.reverse(); 46 | document.getElementById("log").innerHTML+="
".concat(temp); 47 | list = temp.concat(list); 48 | document.getElementById("log").innerHTML+="
".concat(list); 49 | } 50 | document.getElementById("log").innerHTML+="
".concat("GOAL NOT FOUND"); 51 | } 52 | 53 | breadth(1,9); 54 | depth(1,9); -------------------------------------------------------------------------------- /javascript/graph/breadth_depth_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/javascript/graph/breadth_depth_search.png -------------------------------------------------------------------------------- /javascript/graph/uniform_cost.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Uniform Cost 4 | 5 | 6 | 7 | 8 | 9 |
10 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /javascript/graph/uniform_cost.js: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2021 Luis Espino 3 | 4 | function sucesores(n){ 5 | if (n[0]=='A') 6 | return [['B', n[1]+5,inc()], ['C', n[1]+6,inc()]] 7 | if (n[0]=='B') 8 | return [['A', n[1]+5,inc()], ['C', n[1]+6,inc()], ['D', n[1]+3,inc()], ['E', n[1]+5,inc()]] 9 | if (n[0]=='C') 10 | return [['A', n[1]+6,inc()], ['B', n[1]+6,inc()], ['E', n[1]+2,inc()]] 11 | if (n[0]=='D') 12 | return [['B', n[1]+3,inc()], ['E', n[1]+3,inc()], ['F', n[1]+4,inc()]] 13 | if (n[0]=='E') 14 | return [['B', n[1]+5,inc()], ['C', n[1]+2,inc()], ['D', n[1]+3,inc()], ['F', n[1]+1,inc()]] 15 | if (n[0]=='F') 16 | return [['D', n[1]+4,inc()], ['E', n[1]+1,inc()]] 17 | } 18 | 19 | function costo(start, end){ 20 | var dot = '{' 21 | var list = [[start,0,inc()]]; 22 | dot+=list[0][2]+' [label="'+list[0][0]+'"];' 23 | while (list.length > 0){ 24 | var current = list.shift(); 25 | if (current[0] == end) { 26 | dot += '}' 27 | return dot 28 | } 29 | var temp = sucesores(current); 30 | //temp.reverse(); 31 | temp.forEach(val => dot+=val[2]+' [label="'+val[0]+'"];'+current[2]+'--'+val[2]+' [label="'+val[1]+'"] ;') 32 | list = temp.concat(list); 33 | list = list.sort( function(a,b) { return a[1] - b[1] }); 34 | } 35 | dot += '}' 36 | return dot 37 | } 38 | 39 | var id = 1 40 | function inc() { 41 | return id++ 42 | } 43 | -------------------------------------------------------------------------------- /javascript/graph/uniform_cost.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/javascript/graph/uniform_cost.png -------------------------------------------------------------------------------- /javascript/lineal-structures/array-list-mod.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Linked List

4 |

5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /javascript/lineal-structures/array-list-mod.js: -------------------------------------------------------------------------------- 1 | class Nodo { 2 | constructor(valor) { 3 | this.valor = valor 4 | this.siguiente = null 5 | } 6 | } 7 | 8 | class Lista{ 9 | constructor() { 10 | this.cabeza = null 11 | } 12 | 13 | agregar(valor) { 14 | var temp = new Nodo(valor) 15 | temp.siguiente = this.cabeza 16 | this.cabeza = temp 17 | } 18 | 19 | mostrar() { 20 | var temp = this.cabeza 21 | document.getElementById("log").innerHTML+="[ " 22 | while(temp) { 23 | document.getElementById("log").innerHTML+=temp.valor+" "; 24 | temp = temp.siguiente 25 | } 26 | document.getElementById("log").innerHTML+="]" 27 | } 28 | } 29 | 30 | var a = [] 31 | 32 | for (var i = 0; i < 5; i++) { 33 | a[i] = new Lista() 34 | } 35 | 36 | a[1%5].agregar(1) 37 | a[3%5].agregar(3) 38 | a[6%5].agregar(6) 39 | a[8%5].agregar(8) 40 | a[14%5].agregar(14) 41 | 42 | for (var i = 0; i < 5; i++) { 43 | document.getElementById("log").innerHTML+="
"+i+" -> " 44 | a[i].mostrar() 45 | } 46 | -------------------------------------------------------------------------------- /javascript/lineal-structures/list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Linked List

4 |

5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /javascript/lineal-structures/list.js: -------------------------------------------------------------------------------- 1 | class Nodo { 2 | constructor(valor) { 3 | this.valor = valor 4 | this.siguiente = null 5 | } 6 | } 7 | 8 | class Lista{ 9 | constructor() { 10 | this.cabeza = null 11 | } 12 | 13 | agregar(valor) { 14 | var temp = new Nodo(valor) 15 | temp.siguiente = this.cabeza 16 | this.cabeza = temp 17 | } 18 | 19 | mostrar() { 20 | var temp = this.cabeza 21 | document.getElementById("log").innerHTML+="
[ " 22 | while(temp) { 23 | document.getElementById("log").innerHTML+=temp.valor+" "; 24 | temp = temp.siguiente 25 | } 26 | document.getElementById("log").innerHTML+="]" 27 | } 28 | } 29 | 30 | var lista = new Lista() 31 | lista.agregar(20) 32 | lista.agregar(10) 33 | lista.agregar(15) 34 | lista.agregar(5) 35 | lista.mostrar() 36 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/avl.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | AVL Tree 4 | 5 | 6 | 7 | 8 | 9 |

10 |
11 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/avlinmuta.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | AVL Tree 4 | 5 | 6 | 7 | 8 | 9 |

10 |
11 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/avlinmuta.js: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2021 Luis Espino 3 | 4 | class Node { 5 | constructor(value) { 6 | this.value = value 7 | this.left = null 8 | this.right = null 9 | this.height = 0 10 | } 11 | } 12 | 13 | class AVL { 14 | constructor() { 15 | this.root = null 16 | this.dot = '' 17 | } 18 | 19 | add(value) { 20 | if (this.root != null) this._add(value, this.root) 21 | else this.root = new Node(value) 22 | } 23 | 24 | _add(value, tmp) { 25 | if (tmp == null) { 26 | tmp = new Node(value) 27 | alert(tmp.value) 28 | } 29 | else if (value < tmp.value) { 30 | this._add(value,tmp.left) 31 | /* 32 | if ((this.height(tmp.left)-this.height(tmp.right))==2) { 33 | if (value < tmp.left.value) tmp = this.srl(tmp) 34 | else tmp = this.drl(tmp) 35 | } 36 | */ 37 | } else if (value > tmp.value) { 38 | alert('mayor') 39 | this._add(value,tmp.right) 40 | alert(tmp.right) 41 | /* 42 | if ((this.height(tmp.right)-this.height(tmp.left))==2) { 43 | if (value < tmp.right.value) tmp = this.srr(tmp) 44 | else tmp = this.drr(tmp) 45 | } 46 | */ 47 | } 48 | 49 | 50 | var r = this.height(tmp.right) 51 | var l = this.height(tmp.left) 52 | var m = this.max(r, l) 53 | tmp.height = m + 1 54 | 55 | } 56 | 57 | height(tmp) { 58 | if (tmp == null) return -1 59 | return tmp.height 60 | } 61 | 62 | max(val1, val2) { 63 | if (val1 > val2) return val1 64 | return val2 65 | } 66 | 67 | srl(t1) { 68 | var t2 69 | t2 = t1.left 70 | t1.left = t2.right 71 | t2.right = t1 72 | t1.height = max(this.height(t1.left), this.height(t1.right)) + 1 73 | t2.height = max(this.height(t2.left), t1.height) + 1 74 | return t2 75 | } 76 | 77 | drl(tmp) { 78 | tmp.left = this.srr(tmp.left) 79 | return this.srl(tmp) 80 | } 81 | 82 | srr(t1) { 83 | var t2 84 | t2 = t1.right 85 | t1.right = t2.left 86 | t2.left = t1 87 | t1.height = this.max(this.height(t1.left), this.height(t1.right)) + 1 88 | t2.height = this.max(this.height(t2.right), t1.height) + 1 89 | return t2 90 | } 91 | 92 | drr(tmp) { 93 | tmp.right = this.srl(tmp.right) 94 | return this.srr(tmp) 95 | } 96 | 97 | 98 | preorder(tmp) { 99 | if (tmp != null) { 100 | document.getElementById("log").innerHTML+=tmp.value+' ' 101 | this.preorder(tmp.left) 102 | this.preorder(tmp.right) 103 | } 104 | } 105 | 106 | inorder(tmp) { 107 | if (tmp != null) { 108 | this.inorder(tmp.left) 109 | document.getElementById("log").innerHTML+=tmp.value+' ' 110 | this.inorder(tmp.right) 111 | } 112 | } 113 | 114 | postorder(tmp) { 115 | if (tmp != null) { 116 | this.postorder(tmp.left) 117 | this.postorder(tmp.right) 118 | document.getElementById("log").innerHTML+=tmp.value+' ' 119 | } 120 | } 121 | 122 | dotgen(tmp) { 123 | if (tmp != null) { 124 | if (tmp.left != null) this.dot += tmp.value+'--'+tmp.left.value+';' 125 | if (tmp.right != null) this.dot += tmp.value+'--'+tmp.right.value+';' 126 | this.dotgen(tmp.left) 127 | this.dotgen(tmp.right) 128 | } 129 | } 130 | } 131 | 132 | function avl() { 133 | var avl = new AVL() 134 | avl.add(5) 135 | avl.add(10) 136 | /* 137 | 138 | avl.add(20) 139 | avl.add(25) 140 | avl.add(30) 141 | avl.add(35) 142 | avl.add(40) 143 | */ 144 | document.getElementById("log").innerHTML+='Preorder: ' 145 | avl.preorder(avl.root) 146 | document.getElementById("log").innerHTML+='
Inorder: ' 147 | avl.inorder(avl.root) 148 | document.getElementById("log").innerHTML+='
Postorder: ' 149 | avl.postorder(avl.root) 150 | avl.dot = '{' 151 | avl.dotgen(avl.root) 152 | avl.dot += '}' 153 | return avl.dot 154 | } 155 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/bst.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | BST Tree 4 | 5 | 6 | 7 | 8 | 9 |

10 |
11 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/bst.js: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2021 Luis Espino 3 | 4 | class Node { 5 | constructor(value) { 6 | this.value = value 7 | this.left = null 8 | this.right = null 9 | } 10 | } 11 | 12 | class BST { 13 | constructor() { 14 | this.root = null 15 | this.dot = '' 16 | } 17 | 18 | add(value) { 19 | if (this.root != null) this._add(value, this.root) 20 | else this.root = new Node(value) 21 | } 22 | 23 | _add(value, tmp) { 24 | if (value < tmp.value) { 25 | if (tmp.left != null) this._add(value,tmp.left) 26 | else tmp.left = new Node(value) 27 | } else { 28 | if (tmp.right != null) this._add(value,tmp.right) 29 | else tmp.right = new Node(value) 30 | } 31 | } 32 | 33 | preorder(tmp) { 34 | if (tmp != null) { 35 | document.getElementById("log").innerHTML+=tmp.value+' ' 36 | this.preorder(tmp.left) 37 | this.preorder(tmp.right) 38 | } 39 | } 40 | 41 | inorder(tmp) { 42 | if (tmp != null) { 43 | this.inorder(tmp.left) 44 | document.getElementById("log").innerHTML+=tmp.value+' ' 45 | this.inorder(tmp.right) 46 | } 47 | 48 | } 49 | 50 | postorder(tmp) { 51 | if (tmp != null) { 52 | this.postorder(tmp.left) 53 | this.postorder(tmp.right) 54 | document.getElementById("log").innerHTML+=tmp.value+' ' 55 | } 56 | } 57 | 58 | dotgen(tmp) { 59 | if (tmp != null) { 60 | if (tmp.left != null) this.dot += tmp.value+'--'+tmp.left.value+';' 61 | if (tmp.right != null) this.dot += tmp.value+'--'+tmp.right.value+';' 62 | this.dotgen(tmp.left) 63 | this.dotgen(tmp.right) 64 | } 65 | } 66 | } 67 | 68 | function bst() { 69 | var bst = new BST() 70 | bst.add(25) 71 | bst.add(10) 72 | bst.add(5) 73 | bst.add(20) 74 | bst.add(35) 75 | bst.add(30) 76 | bst.add(40) 77 | document.getElementById("log").innerHTML+='Preorder: ' 78 | bst.preorder(bst.root) 79 | document.getElementById("log").innerHTML+='
Inorder: ' 80 | bst.inorder(bst.root) 81 | document.getElementById("log").innerHTML+='
Postorder: ' 82 | bst.postorder(bst.root) 83 | bst.dot = '{' 84 | bst.dotgen(bst.root) 85 | bst.dot += '}' 86 | return bst.dot 87 | } 88 | 89 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/merkle.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | Merkle Tree 4 | 5 | 6 | 7 | 8 | 9 |

10 |
11 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /javascript/nonlineal-structures/merkle.js: -------------------------------------------------------------------------------- 1 | // MIT License 2 | // Copyright (c) 2021 Luis Espino 3 | 4 | class DataNode { 5 | constructor(value) { 6 | this.value = value 7 | } 8 | } 9 | 10 | class HashNode { 11 | constructor(hash) { 12 | this.hash = hash 13 | this.left = null 14 | this.right = null 15 | } 16 | } 17 | 18 | class Merkle { 19 | constructor() { 20 | this.tophash = null 21 | this.datablock = [] 22 | this.dot = '' 23 | } 24 | 25 | add(value) { 26 | this.datablock.push(new DataNode(value)) 27 | } 28 | 29 | createTree(exp) { 30 | this.tophash = new HashNode(0) 31 | this._createTree(this.tophash, exp ) 32 | } 33 | 34 | _createTree(tmp, exp) { 35 | if (exp > 0) { 36 | tmp.left = new HashNode(0) 37 | tmp.right = new HashNode(0) 38 | this._createTree(tmp.left, exp - 1) 39 | this._createTree(tmp.right, exp - 1) 40 | } 41 | } 42 | 43 | genHash(tmp, n) { // postorder 44 | if (tmp != null) { 45 | this.genHash(tmp.left, n) 46 | this.genHash(tmp.right, n) 47 | 48 | if (tmp.left == null && tmp.right == null) { 49 | tmp.left = this.datablock[n-index--] 50 | tmp.hash = (tmp.left.value*1000).toString(16) 51 | } else { 52 | tmp.hash = (parseInt(tmp.left.hash, 16)+parseInt(tmp.right.hash, 16)).toString(16) 53 | } 54 | } 55 | } 56 | 57 | preorder(tmp) { 58 | if (tmp != null) { 59 | if (tmp instanceof DataNode) { 60 | document.getElementById("log").innerHTML+='DB:'+tmp.value+' ' 61 | } else { 62 | document.getElementById("log").innerHTML+=tmp.hash+' ' 63 | } 64 | this.preorder(tmp.left) 65 | this.preorder(tmp.right) 66 | } 67 | } 68 | 69 | auth() { 70 | var exp = 1 71 | while (Math.pow(2, exp) < this.datablock.length) { 72 | exp += 1 73 | } 74 | for (var i = this.datablock.length; i < Math.pow(2, exp); i++) { 75 | this.datablock.push(new DataNode(i*100)) 76 | } 77 | index = Math.pow(2, exp) 78 | this.createTree(exp) 79 | this.genHash(this.tophash, Math.pow(2, exp)) 80 | this.preorder(this.tophash) 81 | } 82 | 83 | show() { 84 | this.datablock.forEach(element => document.getElementById("log").innerHTML+=element.value+' '); 85 | } 86 | 87 | dotgen(tmp) { 88 | if (tmp != null) { 89 | if (tmp.left != null){ 90 | if (tmp.left instanceof DataNode) { 91 | this.dot += '"'+tmp.left.value+'" [color=gray] -> "0x'+tmp.hash+'";' 92 | } 93 | } 94 | if (tmp.left instanceof HashNode) { 95 | if (tmp.left != null) this.dot += '"0x'+tmp.left.hash+'" -> "0x'+tmp.hash+'";' 96 | if (tmp.right != null) this.dot += '"0x'+tmp.right.hash+'" -> "0x'+tmp.hash+'";' 97 | } 98 | this.dotgen(tmp.left) 99 | this.dotgen(tmp.right) 100 | } 101 | } 102 | } 103 | 104 | var index = 0 105 | 106 | function merkle() { 107 | var m = new Merkle() 108 | m.add(4) 109 | m.add(5) 110 | m.add(6) 111 | m.add(7) 112 | m.add(8) 113 | m.auth() 114 | m.dot = '{node [shape=box];' 115 | m.dotgen(m.tophash) 116 | m.dot += '}' 117 | return m.dot 118 | } 119 | -------------------------------------------------------------------------------- /lisp/README.md: -------------------------------------------------------------------------------- 1 | ### LISP Examples 2 | 3 | Compiler: [SBCL](https://www.sbcl.org/) 4 | 5 | Installation on Arch Linux: 6 | ``` 7 | sudo pacman -Syu 8 | sudo pacman -S sbcl 9 | ``` 10 | 11 | Installation on Ubuntu: 12 | ``` 13 | sudo apt update 14 | sudo apt install sbcl 15 | ``` 16 | 17 | 18 | Execution example: 19 | 20 | ``` 21 | $ sbcl --load filename.lisp 22 | ``` -------------------------------------------------------------------------------- /lisp/breadth-search.lisp: -------------------------------------------------------------------------------- 1 | ; Breadth First Search Algorithm 2 | ; Copyrigth (c) 2015 Luis Espino 3 | 4 | ; Problem: Points matrix 5 | ; Sucesors rule: every possible node around 6 | ; 1 2 3 7 | ; 4 5 6 8 | ; 7 8 9 9 | 10 | (defun successors (node) 11 | (cond 12 | ((= node 1) (list 2 4 5)) 13 | ((= node 2) (list 1 3 4 5 6)) 14 | ((= node 3) (list 2 5 6)) 15 | ((= node 4) (list 1 2 5 7 8)) 16 | ((= node 5) (list 1 2 3 4 6 7 8 9)) 17 | ((= node 6) (list 2 3 5 8 9)) 18 | ((= node 7) (list 4 5 8)) 19 | ((= node 8) (list 4 5 6 7 9)) 20 | ((= node 9) (list 5 6 8)) 21 | (t nil))) 22 | 23 | (defun breadth-search (start-node end-node) 24 | (format t "BREADTH FIRST SEARCH:~%~%") 25 | (setq l (list start-node)) 26 | (loop until (null l) do 27 | (setq current-node (car l)) 28 | (format t "Current node: ~a~%" current-node) 29 | (setq l (cdr l)) 30 | (if (= current-node end-node) 31 | (return-from breadth-search (print "SOLUTION FOUND"))) 32 | (setq temp (successors current-node)) 33 | ;(setq temp (reverse temp)) 34 | (format t "Current node successors: ~a~%" temp) 35 | (if (not (null temp)) (setq l (append l temp))) 36 | (format t "List content: ~a~%~%" l)) 37 | (print "SOLUTION NOT FOUND")) 38 | 39 | (breadth-search 1 9) 40 | (format t "~%") 41 | (quit) 42 | -------------------------------------------------------------------------------- /lisp/breadth-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/lisp/breadth-search.png -------------------------------------------------------------------------------- /lisp/depth-search.lisp: -------------------------------------------------------------------------------- 1 | ; Depth First Search Algorithm 2 | ; Copyrigth (c) 2015 Luis Espino 3 | 4 | ; Problem: Points matrix 5 | ; Sucesors rule: every possible node around 6 | ; 1 2 3 7 | ; 4 5 6 8 | ; 7 8 9 9 | 10 | (defun successors (node) 11 | (cond 12 | ((= node 1) (list 2 4 5)) 13 | ((= node 2) (list 1 3 4 5 6)) 14 | ((= node 3) (list 2 5 6)) 15 | ((= node 4) (list 1 2 5 7 8)) 16 | ((= node 5) (list 1 2 3 4 6 7 8 9)) 17 | ((= node 6) (list 2 3 5 8 9)) 18 | ((= node 7) (list 4 5 8)) 19 | ((= node 8) (list 4 5 6 7 9)) 20 | ((= node 9) (list 5 6 8)) 21 | (t nil))) 22 | 23 | (defun depth-search (start-node end-node) 24 | (format t "DEPTH FIRST SEARCH:~%~%") 25 | (setq l (list start-node)) 26 | (loop until (null l) do 27 | (setq current-node (car l)) 28 | (format t "Current node: ~a~%" current-node) 29 | (setq l (cdr l)) 30 | (if (= current-node end-node) 31 | (return-from depth-search (print "SOLUTION FOUND"))) 32 | (setq temp (successors current-node)) 33 | (setq temp (reverse temp)) ; to avoid cycling 34 | (format t "Current node successors: ~a~%" temp) 35 | (if (not (null temp)) (setq l (append temp l))) 36 | (format t "List content: ~a~%~%" l)) 37 | (print "SOLUTION NOT FOUND")) 38 | 39 | (depth-search 1 9) 40 | (format t "~%") 41 | (quit) 42 | -------------------------------------------------------------------------------- /lisp/depth-search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/lisp/depth-search.png -------------------------------------------------------------------------------- /python/README.md: -------------------------------------------------------------------------------- 1 | ## Código de Estructuras de Datos en Python 2 | -------------------------------------------------------------------------------- /python/linear-structures/list.py: -------------------------------------------------------------------------------- 1 | class Node: 2 | def __init__(self, data=None): 3 | self.data = data 4 | self.next = None 5 | 6 | class LinkedList: 7 | def __init__(self): 8 | self.head = None 9 | 10 | def addStart(self, data): 11 | tmp = Node(data) 12 | tmp.next = self.head 13 | self.head = tmp 14 | 15 | def show(self): 16 | tmp = self.head 17 | while tmp is not None: 18 | print (tmp.data) 19 | tmp = tmp.next 20 | 21 | list1 = LinkedList() 22 | list1.addStart(5) 23 | list1.addStart(10) 24 | list1.addStart(7) 25 | list1.show() 26 | -------------------------------------------------------------------------------- /python/nonlinear-structures/graph/anchura_profundidad.py: -------------------------------------------------------------------------------- 1 | # Luis Espino 2017 2 | # Búsqueda por anchura y profundidad 3 | # Problema matriz de 3x3 4 | 5 | def sucesores(n): 6 | if n == 1: return [2, 4, 5] 7 | elif n == 2: return [1, 3, 4, 5, 6] 8 | elif n == 3: return [2, 5, 6] 9 | elif n == 4: return [1, 2, 5, 7, 8] 10 | elif n == 5: return [1, 2, 3, 4, 6, 7 ,8, 9] 11 | elif n == 6: return [2, 3, 5, 8 ,9] 12 | elif n == 7: return [4, 5, 8] 13 | elif n == 8: return [4, 5, 6 ,7, 9] 14 | elif n == 9: return [5, 6 , 8] 15 | else: return None 16 | 17 | 18 | def anchura(nodo_inicio, nodo_fin): 19 | lista = [nodo_inicio] 20 | while lista: 21 | nodo_actual = lista.pop(0) 22 | print (nodo_actual) 23 | if nodo_actual == nodo_fin: 24 | return print("SOLUCIÓN") 25 | temp = sucesores(nodo_actual) 26 | #temp.reverse() 27 | print (temp) 28 | if temp: 29 | lista.extend(temp) 30 | print(lista) 31 | print ("NO-SOLUCIÓN") 32 | 33 | def profundidad (nodo_inicio, nodo_fin): 34 | lista = [nodo_inicio] 35 | while lista: 36 | nodo_actual = lista.pop(0) 37 | print (nodo_actual) 38 | if nodo_actual == nodo_fin: 39 | print(len(lista)) 40 | return print("SOLUCIÓN") 41 | temp = sucesores(nodo_actual) 42 | temp.reverse() 43 | print (temp) 44 | if temp: 45 | temp.extend(lista) 46 | lista = temp 47 | print(lista) 48 | print ("NO-SOLUCIÓN") 49 | 50 | #anchura (1,9) 51 | profundidad (1,9) 52 | -------------------------------------------------------------------------------- /python/nonlinear-structures/graph/costo_uniforme.py: -------------------------------------------------------------------------------- 1 | def sucesores(n): 2 | if n[0]=='A': 3 | return [['B', n[1]+5,], ['C', n[1]+6]] 4 | if n[0]=='B': 5 | return [['A', n[1]+5], ['C', n[1]+6], ['D', n[1]+3], ['E', n[1]+5]] 6 | if n[0]=='C': 7 | return [['A', n[1]+6], ['B', n[1]+6], ['E', n[1]+2]] 8 | if n[0]=='D': 9 | return [['B', n[1]+3], ['E', n[1]+3], ['F', n[1]+4]] 10 | if n[0]=='E': 11 | return [['B', n[1]+5], ['C', n[1]+2], ['D', n[1]+3], ['F', n[1]+1]] 12 | if n[0]=='F': 13 | return [['D', n[1]+4], ['E', n[1]+1]] 14 | 15 | 16 | def costo_uniforme (nodo_inicio, nodo_fin): 17 | lista = [[nodo_inicio, 0]] 18 | while lista: 19 | nodo_actual = lista.pop(0) 20 | print (nodo_actual) 21 | if nodo_actual[0] == nodo_fin[0]: 22 | return print ("SOLUCION") 23 | temp = sucesores (nodo_actual) 24 | print (temp) 25 | if temp: 26 | lista.extend(temp) 27 | lista.sort(key=lambda x: int(x[1])) 28 | print (lista) 29 | print ("NO-SOLUCION") 30 | 31 | costo_uniforme ('A', 'F') 32 | -------------------------------------------------------------------------------- /python/nonlinear-structures/graph/dijkstra.py: -------------------------------------------------------------------------------- 1 | 2 | def dijkstra (origen): 3 | distancia = [None]*len(ady) 4 | visto = [None]*len(ady) 5 | camino = [None]*len(ady) 6 | for i in range(len(ady)): 7 | distancia[i] = inf 8 | visto[i] = False 9 | distancia[origen] = 0 10 | camino[origen] = -1 11 | for c in range(len(ady)): 12 | min = inf 13 | i = 0 14 | for j in range(len(ady)): 15 | if not visto[j] and distancia[j] < min: 16 | min = distancia[j] 17 | i = j 18 | visto[i] = True 19 | for j in range(len(ady)): 20 | if not visto[j] and ady[i][j] and distancia[i]!=inf \ 21 | and distancia[i]+ady[i][j] < distancia[j]: 22 | distancia[j] = distancia[i] + ady[i][j] 23 | camino[j] = i 24 | print("Nodo\tDistancia\tCamino") 25 | for i in range(len(ady)): 26 | print(str(i)+'\t'+str(distancia[i])+ 27 | '\t'+'\t'+str(camino[i])) 28 | 29 | inf = 9999 30 | 31 | 32 | ady = [[0, 5, 6, 0, 0, 0], 33 | [5, 0, 6, 3, 5, 0], 34 | [6, 6, 0, 0, 2, 0], 35 | [0, 3, 0, 0, 3, 4], 36 | [0, 5, 2, 3, 0, 1], 37 | [0, 0, 0, 4, 1, 0]] 38 | 39 | 40 | 41 | dijkstra(0) 42 | 43 | ady = [[0, 6, 0, 4, 7, 0, 0, 0], 44 | [6, 0, 0, 7, 0, 3, 9, 0], 45 | [0, 0, 0, 0, 4,4, 0, 0], 46 | [4, 7, 0, 0, 0, 5, 0, 5], 47 | [7, 0, 4, 0, 0, 0, 0, 6], 48 | [0, 3, 4, 5, 0, 0, 0, 0], 49 | [0, 9, 0, 0, 0, 0, 0, 8], 50 | [0, 0, 0, 5, 6, 0, 8, 0]] 51 | 52 | 53 | 54 | 55 | ady = [[0, 7, 0, 0, 0, 0, 0, 0], 56 | [7, 0, 2, 0, 0, 0, 0, 0], 57 | [0, 2, 0, 0, 4, 0, 5, 0], 58 | [0, 0, 0, 0, 0, 0, 0, 7], 59 | [0, 0, 4, 0, 0, 0, 0, 8], 60 | [0, 0, 0, 0, 0, 0, 1, 0], 61 | [0, 0, 5, 0, 0, 1, 0, 0], 62 | [0, 0, 0, 7, 8, 0, 0, 0]] 63 | -------------------------------------------------------------------------------- /python/nonlinear-structures/tree/avltree.py: -------------------------------------------------------------------------------- 1 | # Luis Espino 2020 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.left = None 7 | self.right = None 8 | self.height = 0 9 | 10 | class AVLTree: 11 | def __init__(self): 12 | self.root = None 13 | 14 | #add 15 | 16 | def add(self, value): 17 | self.root = self._add(value, self.root) 18 | 19 | def _add(self, value, tmp): 20 | if tmp is None: 21 | return Node(value) 22 | elif value>tmp.value: 23 | tmp.right=self._add(value, tmp.right) 24 | if (self.height(tmp.right)-self.height(tmp.left))==2: 25 | if value>tmp.right.value: 26 | tmp = self.srr(tmp) 27 | else: 28 | tmp = self.drr(tmp) 29 | else: 30 | tmp.left=self._add(value, tmp.left) 31 | if (self.height(tmp.left)-self.height(tmp.right))==2: 32 | if valuel] 50 | 51 | #rotations 52 | 53 | def srl(self, t1): 54 | t2 = t1.left 55 | t1.left = t2.right 56 | t2.right = t1 57 | t1.height = self.maxi(self.heigh(t1.left), self.height(t1.right))+1 58 | t2.height = self.maxi(self.heigh(t2.left), t1.height)+1 59 | return t2 60 | 61 | def srr(self, t1): 62 | t2 = t1.right 63 | t1.right = t2.left 64 | t2.left = t1 65 | t1.height = self.maxi(self.height(t1.left), self.height(t1.right))+1 66 | t2.height = self.maxi(self.height(t2.left), t1.height)+1 67 | return t2 68 | 69 | def drl(self, tmp): 70 | tmp.left = srr(tmp.left) 71 | return srl(tmp) 72 | 73 | def drr(self, tmp): 74 | tmp.right = srl(tmp.right) 75 | return srr(tmp) 76 | 77 | #traversals 78 | 79 | def preorder(self): 80 | self._preorder(self.root) 81 | 82 | def _preorder(self, tmp): 83 | if tmp: 84 | print(tmp.value,end = ' ') 85 | self._preorder(tmp.left) 86 | self._preorder(tmp.right) 87 | 88 | def inorder(self): 89 | self._inorder(self.root) 90 | 91 | def _inorder(self, tmp): 92 | if tmp: 93 | self._inorder(tmp.left) 94 | print(tmp.value,end = ' ') 95 | self._inorder(tmp.right) 96 | 97 | def postorder(self): 98 | self._postorder(self.root) 99 | 100 | def _postorder(self, tmp): 101 | if tmp: 102 | self._postorder(tmp.left) 103 | self._postorder(tmp.right) 104 | print(tmp.value,end = ' ') 105 | 106 | 107 | #init 108 | t = AVLTree() 109 | 110 | #add 111 | t.add(5) 112 | t.add(10) 113 | t.add(20) 114 | t.add(25) 115 | t.add(30) 116 | t.add(35) 117 | t.add(50) 118 | 119 | #print traversals 120 | t.preorder() 121 | print() 122 | t.inorder() 123 | print() 124 | t.postorder() 125 | -------------------------------------------------------------------------------- /python/nonlinear-structures/tree/bstree.py: -------------------------------------------------------------------------------- 1 | # Luis Espino 2020 2 | 3 | class Node: 4 | def __init__(self, value): 5 | self.value = value 6 | self.left = None 7 | self.right = None 8 | 9 | class BSTree: 10 | def __init__(self): 11 | self.root = None 12 | 13 | #add 14 | 15 | def add(self, value): 16 | self.root = self._add(value, self.root) 17 | 18 | def _add(self, value, tmp): 19 | if tmp is None: 20 | return Node(value) 21 | elif (value>tmp.value): 22 | tmp.right=self._add(value, tmp.right) 23 | else: 24 | tmp.left=self._add(value, tmp.left) 25 | return tmp 26 | 27 | #traversals 28 | 29 | def preorder(self): 30 | self._preorder(self.root) 31 | 32 | def _preorder(self, tmp): 33 | if tmp: 34 | print(tmp.value,end = ' ') 35 | self._preorder(tmp.left) 36 | self._preorder(tmp.right) 37 | 38 | def inorder(self): 39 | self._inorder(self.root) 40 | 41 | def _inorder(self, tmp): 42 | if tmp: 43 | self._inorder(tmp.left) 44 | print(tmp.value,end = ' ') 45 | self._inorder(tmp.right) 46 | 47 | def postorder(self): 48 | self._postorder(self.root) 49 | 50 | def _postorder(self, tmp): 51 | if tmp: 52 | self._postorder(tmp.left) 53 | self._postorder(tmp.right) 54 | print(tmp.value,end = ' ') 55 | 56 | 57 | #init 58 | t = BSTree() 59 | 60 | #add 61 | t.add(25) 62 | t.add(10) 63 | t.add(5) 64 | t.add(20) 65 | t.add(35) 66 | t.add(30) 67 | t.add(40) 68 | 69 | #print traversals 70 | t.preorder() 71 | print() 72 | t.inorder() 73 | print() 74 | t.postorder() 75 | -------------------------------------------------------------------------------- /python/nonlinear-structures/tree/bstree_gviz.py: -------------------------------------------------------------------------------- 1 | # Luis Espino 2021 2 | 3 | from graphviz import Graph 4 | 5 | def inc(): 6 | global i 7 | i += 1 8 | return i 9 | 10 | class Node: 11 | def __init__(self, value): 12 | self.value = value 13 | self.left = None 14 | self.right = None 15 | 16 | class BSTree: 17 | def __init__(self): 18 | self.root = None 19 | 20 | #add 21 | def add(self, value): 22 | self.root = self._add(value, self.root) 23 | 24 | def _add(self, value, tmp): 25 | if tmp is None: 26 | return Node(value) 27 | elif (value>tmp.value): 28 | tmp.right=self._add(value, tmp.right) 29 | else: 30 | tmp.left=self._add(value, tmp.left) 31 | return tmp 32 | 33 | #traversals 34 | def gviz(self, parent): 35 | self._gviz(self.root, parent) 36 | 37 | def _gviz(self, tmp, parent): 38 | if tmp: 39 | id = inc() 40 | dot.node(str(id),str(tmp.value)) 41 | if parent: 42 | dot.edge(str(parent), str(id)) 43 | self._gviz(tmp.left, id) 44 | self._gviz(tmp.right, id) 45 | 46 | #init 47 | t = BSTree() 48 | i = 0 49 | dot = Graph() 50 | dot.attr(splines='false') 51 | dot.node_attr.update(shape='oval', fontname='arial', color='blue4', fontcolor='blue4') 52 | dot.edge_attr.update(color='blue4') 53 | 54 | #add 55 | t.add(25) 56 | t.add(10) 57 | t.add(5) 58 | t.add(20) 59 | t.add(35) 60 | t.add(30) 61 | t.add(40) 62 | 63 | #print tree 64 | t.gviz(None) 65 | dot.view() -------------------------------------------------------------------------------- /python/nonlinear-structures/tree/bstree_of_lists.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/python/nonlinear-structures/tree/bstree_of_lists.png -------------------------------------------------------------------------------- /python/nonlinear-structures/tree/bstree_of_lists.py: -------------------------------------------------------------------------------- 1 | # Luis Espino 2021 2 | # Examples of Binary Search Tree of Lists 3 | 4 | class NodeList: 5 | def __init__(self, value): 6 | self.value = value 7 | self.next = None 8 | 9 | class List: 10 | def __init__(self): 11 | self.head = None 12 | 13 | def add(self, value): 14 | tmp = NodeList(value) 15 | tmp.next = self.head 16 | self.head = tmp 17 | 18 | def show(self): 19 | print("[", end = " ") 20 | tmp = self.head 21 | while tmp: 22 | print(tmp.value, end=" ") 23 | tmp = tmp.next 24 | print("]") 25 | 26 | class Node: 27 | def __init__(self, value, list): 28 | self.value = value 29 | self.list = list 30 | self.left = None 31 | self.right = None 32 | 33 | class BSTree: 34 | def __init__(self): 35 | self.root = None 36 | 37 | #add 38 | 39 | def add(self, value, list): 40 | self.root = self._add(value, list, self.root) 41 | 42 | def _add(self, value, list, tmp): 43 | if tmp is None: 44 | return Node(value, list) 45 | elif (value>tmp.value): 46 | tmp.right=self._add(value, list, tmp.right) 47 | else: 48 | tmp.left=self._add(value, list, tmp.left) 49 | return tmp 50 | 51 | #traversal 52 | 53 | def show(self): 54 | self._show(self.root) 55 | 56 | def _show(self, tmp): 57 | if tmp: 58 | self._show(tmp.left) 59 | print(str(tmp.value), end = " ") 60 | tmp.list.show() 61 | self._show(tmp.right) 62 | 63 | #init 64 | t = BSTree() 65 | 66 | #add 67 | tmp = List() 68 | tmp.add("Fernandez") 69 | tmp.add("Santiago") 70 | bst = BSTree() 71 | bst.add(25, tmp) 72 | 73 | tmp = List() 74 | tmp.add("Lemus") 75 | tmp.add("Karla") 76 | bst.add(5, tmp) 77 | 78 | tmp = List() 79 | tmp.add("Cabrera") 80 | tmp.add("Angel") 81 | bst.add(10, tmp) 82 | 83 | tmp = List() 84 | tmp.add("Gonzalez") 85 | tmp.add("Carlos") 86 | bst.add(41, tmp) 87 | 88 | #print tree 89 | bst.show() -------------------------------------------------------------------------------- /rust/01-stack-heap: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/rust/01-stack-heap -------------------------------------------------------------------------------- /rust/01-stack-heap.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/rust/01-stack-heap.png -------------------------------------------------------------------------------- /rust/01-stack-heap.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | let stack_var = 10; 3 | println!("Stack value: {}", stack_var); 4 | 5 | let heap_var = Box::new(20); 6 | println!("Heap value: {}", heap_var); 7 | 8 | } -------------------------------------------------------------------------------- /rust/11-structs: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/rust/11-structs -------------------------------------------------------------------------------- /rust/11-structs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/luisespino/data-structures/88dccfc8c47d65a70ab15ccd430454be2cfd4576/rust/11-structs.png -------------------------------------------------------------------------------- /rust/11-structs.rs: -------------------------------------------------------------------------------- 1 | #![allow( 2 | dead_code, 3 | unused_imports 4 | )] 5 | 6 | trait Alas { 7 | fn volar(&self); 8 | } 9 | 10 | trait Cuerno { 11 | fn cornear(&self); 12 | } 13 | 14 | trait Torso { 15 | fn hablar(&self); 16 | } 17 | 18 | trait Caballo { 19 | fn caminar(&self); 20 | fn correr(&self); 21 | fn parar(&self); 22 | } 23 | 24 | struct Pegaso { 25 | color: String, 26 | } 27 | 28 | impl Pegaso { 29 | fn new(color: &str) -> Self { 30 | Pegaso { 31 | color: String::from(color), 32 | } 33 | } 34 | 35 | fn show(&self) { 36 | println!("{}", self.color); 37 | } 38 | } 39 | 40 | impl Caballo for Pegaso { 41 | fn caminar(&self) { 42 | println!("El Pegaso está caminando"); 43 | } 44 | 45 | fn correr(&self) { 46 | println!("El Pegaso está corriendo"); 47 | } 48 | 49 | fn parar(&self) { 50 | println!("El Pegaso ha parado"); 51 | } 52 | } 53 | 54 | impl Alas for Pegaso { 55 | fn volar(&self) { 56 | println!("El Pegaso está volando"); 57 | } 58 | } 59 | 60 | struct Unicornio { 61 | color: String, 62 | } 63 | 64 | impl Unicornio { 65 | fn new() -> Self { 66 | Unicornio { 67 | color: String::from("blanco"), 68 | } 69 | } 70 | 71 | fn show(&self) { 72 | println!("{}", self.color); 73 | } 74 | } 75 | 76 | impl Caballo for Unicornio { 77 | fn caminar(&self) { 78 | println!("El Unicornio está caminando"); 79 | } 80 | 81 | fn correr(&self) { 82 | println!("El Unicornio está corriendo"); 83 | } 84 | 85 | fn parar(&self) { 86 | println!("El Unicornio ha parado"); 87 | } 88 | } 89 | 90 | impl Cuerno for Unicornio { 91 | fn cornear(&self) { 92 | println!("El Unicornio está corneando"); 93 | } 94 | } 95 | 96 | struct Centauro { 97 | color: String, 98 | } 99 | 100 | impl Centauro { 101 | fn new(color: &str) -> Self { 102 | Centauro { 103 | color: String::from(color), 104 | } 105 | } 106 | 107 | fn show(&self) { 108 | println!("{}", self.color); 109 | } 110 | } 111 | 112 | impl Caballo for Centauro { 113 | fn caminar(&self) { 114 | println!("El Centauro está caminando"); 115 | } 116 | 117 | fn correr(&self) { 118 | println!("El Centauro está corriendo"); 119 | } 120 | 121 | fn parar(&self) { 122 | println!("El Centauro ha parado"); 123 | } 124 | } 125 | 126 | impl Torso for Centauro { 127 | fn hablar(&self) { 128 | println!("El Centauro está hablando"); 129 | } 130 | } 131 | 132 | fn main() { 133 | let p = Pegaso::new("negro"); 134 | let u = Unicornio::new(); 135 | let c = Centauro::new("café"); 136 | 137 | p.show(); 138 | u.show(); 139 | c.show(); 140 | 141 | } 142 | -------------------------------------------------------------------------------- /rust/README.md: -------------------------------------------------------------------------------- 1 | ### Rust Examples 2 | 3 | Official compiler with rustup: [Rust](https://www.rust-lang.org/) 4 | 5 | Rustup Installation on Arch Linux: 6 | ``` 7 | sudo pacman -Syu 8 | sudo pacman -S rustup 9 | ``` 10 | 11 | Installation of the most recent and stable version of Rust: 12 | ``` 13 | rustup install stable 14 | ``` 15 | 16 | Setting environment variable: 17 | ``` 18 | export PATH="$HOME/.cargo/bin:$PATH" 19 | ``` 20 | 21 | Add the export to the end of the .bashrc file and save it: 22 | ``` 23 | nano ~/.bashrc 24 | ``` 25 | 26 | Update configuration file: 27 | ``` 28 | source ~/.bashrc 29 | ``` 30 | 31 | Check Rust version: 32 | ``` 33 | rustc --version 34 | ``` 35 | 36 | 37 | Compilation and execution: 38 | ``` 39 | $ rustc filename.rs 40 | ./filename 41 | ``` 42 | --------------------------------------------------------------------------------