├── README.md ├── abb ├── CMakeLists.txt ├── include │ └── trees │ │ ├── abb.hpp │ │ └── abbNode.hpp ├── src │ ├── abb.cpp │ └── abbNode.cpp └── tests │ └── test.cpp ├── avl ├── CMakeLists.txt ├── include │ └── trees │ │ ├── avl.hpp │ │ └── avlNode.hpp ├── src │ ├── avl.cpp │ └── avlNode.cpp └── tests │ └── test.cpp ├── eda.png ├── game ├── CMakeLists.txt ├── game │ └── game.cpp ├── include │ ├── adts │ │ ├── cell2D.hpp │ │ ├── cellStack.hpp │ │ └── node.hpp │ └── game │ │ └── utils.hpp └── src │ ├── cell2D.cpp │ ├── cellStack.cpp │ ├── node.cpp │ └── utils.cpp ├── game_v2 ├── CMakeLists.txt ├── game │ └── game.cpp ├── include │ ├── adts │ │ ├── cell2D.hpp │ │ ├── cellLinkedList.hpp │ │ ├── cellStack.hpp │ │ └── node.hpp │ └── game │ │ └── utils.hpp └── src │ ├── cell2D.cpp │ ├── cellLinkedList.cpp │ ├── cellStack.cpp │ ├── node.cpp │ └── utils.cpp ├── imagepro ├── CMakeLists.txt ├── imagepro.cpp ├── images │ ├── image_1.bmp │ ├── image_2.bmp │ ├── image_3.bmp │ ├── image_4.bmp │ └── image_5.bmp ├── include │ └── image │ │ ├── image.hpp │ │ ├── listofpoint2d.hpp │ │ ├── listofregion.hpp │ │ ├── nodepoint2d.hpp │ │ ├── noderegion.hpp │ │ ├── point2D.hpp │ │ └── region.hpp └── src │ ├── image.cpp │ ├── listofpoint2d.cpp │ ├── listofregion.cpp │ ├── nodepoint2d.cpp │ ├── noderegion.cpp │ ├── point2D.cpp │ └── region.cpp ├── io ├── CMakeLists.txt ├── data │ └── ej1.html ├── include │ └── io │ │ └── io.hpp ├── src │ └── io.cpp └── tests │ └── test.cpp ├── lLists ├── CMakeLists.txt ├── include │ └── lLists │ │ ├── linkedList.hpp │ │ ├── node.hpp │ │ ├── queue.hpp │ │ └── stack.hpp ├── src │ ├── linkedList.cpp │ ├── node.cpp │ ├── queue.cpp │ └── stack.cpp └── tests │ ├── test.cpp │ └── test_stack_queue.cpp ├── laberinto ├── CMakeLists.txt ├── include │ └── maze │ │ └── maze.hpp ├── src │ └── maze.cpp └── test │ └── test.cpp ├── misc ├── CMakeLists.txt ├── include │ └── misc │ │ └── misc.hpp ├── src │ └── misc.cpp └── tests │ └── main.cpp ├── parenthesis ├── CMakeLists.txt ├── include │ └── adts │ │ ├── node.hpp │ │ ├── queue.hpp │ │ └── stack.hpp ├── src │ ├── node.cpp │ ├── queue.cpp │ └── stack.cpp └── test │ └── test_parenthesis.cpp ├── pointers ├── test.cpp ├── test1.cpp ├── test2.cpp └── test_pointers.cpp ├── rb_tree ├── CMakeLists.txt ├── include │ └── trees │ │ ├── rb.hpp │ │ └── rbNode.hpp ├── keys.bin ├── keys_sorted.bin ├── src │ ├── rb.cpp │ └── rbNode.cpp └── test.cpp ├── sort ├── CMakeLists.txt ├── include │ └── sort │ │ ├── sort.hpp │ │ └── utils.hpp ├── src │ ├── sort.cpp │ └── utils.cpp └── tests │ └── test.cpp └── trees ├── CMakeLists.txt ├── include └── trees │ ├── tree.hpp │ ├── treeList.hpp │ ├── treeListNode.hpp │ └── treeNode.hpp ├── src ├── tree.cpp ├── treeList.cpp ├── treeListNode.cpp └── treeNode.cpp └── tests └── test.cpp /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 |

4 | 5 |

6 | 7 | # Data Structures and Algorithms in C++ 8 | 9 | ## Dependencies 10 | - g++ 11 | - cmake 12 | 13 | To make environment setting easy, we have relesed a Linux virtual machine with the dependencies already installed. You can download the VM from [here](https://www.dropbox.com/scl/fi/suhnm0ci3pj0xo80a71gm/EDAUAndes.ova?rlkey=9j3wswii81fdtnet3h2zmwpko&dl=0). 14 | 15 | ## Installing VirtualBox 16 | If you want to work with the VBox environment please folow these steps: 17 | - Go to https://www.virtualbox.org/wiki/Downloads 18 | - Select the option [Windows Hosts] 19 | - Run the installer answering yes to all questions. 20 | - Use File/Import Applience to select the VM EDAUAndes downloaded previously. 21 | - If a password is required, use "edauandes" 22 | 23 | Please, to install the dependencies consult the Chapter 1 of the guide [book](https://www.dropbox.com/s/v3jeokz580z0amq/EDA_book.pdf). 24 | ## Compiling with CMake 25 | - cd [sort | misc] (_go to the project folder_) 26 | - mkdir build (_create a build folder_) 27 | - cd build 28 | - cmake .. (_create makefiles. Remember, to use cmake you should have the CMakeLists.txt file_ ) 29 | - make (_run make_) 30 | 31 | ## Running 32 | - ./test (_run executable_) 33 | 34 | -------------------------------------------------------------------------------- /abb/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 3 | # set the project name 4 | 5 | project(misc 6 | VERSION 1.0 7 | LANGUAGES CXX) 8 | 9 | set(CMAKE_CXX_STANDARD 11) 10 | set(CMAKE_CXX_STANDARD_REQUIRED True) 11 | 12 | add_executable(test_abb tests/test.cpp src/abbNode.cpp src/abb.cpp) 13 | target_include_directories(test_abb PUBLIC ${PROJECT_SOURCE_DIR}/include) 14 | 15 | 16 | 17 | # add the executable -------------------------------------------------------------------------------- /abb/include/trees/abb.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abb.hpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef ABB_HPP_ 9 | #define ABB_HPP_ 10 | 11 | #include "trees/abbNode.hpp" 12 | 13 | namespace trees { 14 | 15 | class ABB { 16 | private: 17 | ABBNode* root; 18 | public: 19 | ABB(); 20 | void insert_rec(int val, ABBNode* node); 21 | void insert(int val); 22 | ABBNode* find_rec(int val, ABBNode* node); 23 | ABBNode* find(int val); 24 | void traverse_rec(ABBNode* node, int level); 25 | void traverse(); 26 | void showASC_rec(ABBNode* node); 27 | void showASC(); 28 | void updateSize_rec(ABBNode* node); 29 | void updateSize(); 30 | ABBNode* k_element_rec(int k, ABBNode* node); 31 | ABBNode* k_element(int k); 32 | virtual ~ABB(); 33 | }; 34 | 35 | } /* namespace trees */ 36 | 37 | #endif /* ABB_HPP_ */ 38 | -------------------------------------------------------------------------------- /abb/include/trees/abbNode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abbNode.h 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef ABBNODE_HPP_ 9 | #define ABBNODE_HPP_ 10 | 11 | namespace trees { 12 | 13 | class ABBNode { 14 | private: 15 | ABBNode* ptrLeft; 16 | int data; 17 | ABBNode* ptrRight; 18 | int size; 19 | public: 20 | ABBNode(); 21 | ABBNode(int val); 22 | void setLeft(ABBNode* node); 23 | void setRight(ABBNode* node); 24 | void setData(int val); 25 | void setSize(int s); 26 | ABBNode* getLeft(); 27 | ABBNode* getRight(); 28 | int getData(); 29 | int getSize(); 30 | virtual ~ABBNode(); 31 | }; 32 | 33 | } /* namespace trees */ 34 | 35 | #endif /* ABBNODE_H_ */ 36 | -------------------------------------------------------------------------------- /abb/src/abb.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abb.cpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/abb.hpp" 9 | #include 10 | 11 | namespace trees { 12 | 13 | ABB::ABB():root(nullptr) { 14 | // TODO Auto-generated constructor stub 15 | } 16 | 17 | void ABB::insert_rec(int val, ABBNode* node){ 18 | if (val < node->getData()){ 19 | //LEFT 20 | if (node->getLeft() == nullptr){ 21 | node->setLeft(new ABBNode(val)); 22 | //std::cout<getLeft()); 26 | } 27 | } 28 | else{ 29 | //RIGHT 30 | if (node->getRight() == nullptr){ 31 | node->setRight(new ABBNode(val)); 32 | //std::cout<getRight()); 36 | } 37 | } 38 | } 39 | 40 | void ABB::insert(int val){ 41 | if (root == nullptr){ 42 | root = new ABBNode(val); 43 | } 44 | else{ 45 | insert_rec(val, root); 46 | } 47 | } 48 | 49 | ABBNode* ABB::find_rec(int val, ABBNode* node){ 50 | ABBNode* ans = nullptr; 51 | 52 | if (node->getData() == val){ 53 | ans = node; 54 | } 55 | else{ 56 | if (val < node->getData()){ 57 | ans = find_rec(val, node->getLeft()); 58 | } 59 | else{ 60 | ans = find_rec(val, node->getRight()); 61 | } 62 | } 63 | 64 | return ans; 65 | } 66 | 67 | ABBNode* ABB::find(int val){ 68 | ABBNode* ans = nullptr; 69 | ans = find_rec(val, root); 70 | return ans; 71 | } 72 | 73 | void ABB::traverse_rec(ABBNode* node, int level){ 74 | if (node != nullptr){ 75 | std::cout << std::string(level*2, '-'); 76 | std::cout << node->getData() << " | s = " << node->getSize() << std::endl; 77 | traverse_rec(node->getLeft(), level + 1); 78 | traverse_rec(node->getRight(), level + 1); 79 | } 80 | } 81 | 82 | void ABB::traverse(){ 83 | traverse_rec(root, 1); 84 | } 85 | 86 | /*extras*/ 87 | void ABB::showASC_rec(ABBNode* node){ 88 | if (node != nullptr){ 89 | showASC_rec(node->getLeft()); 90 | std::cout << node->getData() << " " << std::flush; 91 | showASC_rec(node->getRight()); 92 | } 93 | } 94 | void ABB::showASC(){ 95 | showASC_rec(root); 96 | std::cout << std::endl; 97 | } 98 | 99 | void ABB::updateSize_rec(ABBNode* node){ 100 | if (node != nullptr){ 101 | updateSize_rec(node->getLeft()); 102 | updateSize_rec(node->getRight()); 103 | int lSize = 0; 104 | int rSize = 0; 105 | if (node->getLeft() != nullptr){ 106 | lSize = node->getLeft()->getSize(); 107 | } 108 | if (node->getRight() != nullptr){ 109 | rSize = node->getRight()->getSize(); 110 | } 111 | node->setSize(lSize + rSize + 1); 112 | } 113 | } 114 | 115 | void ABB::updateSize(){ 116 | updateSize_rec(root); 117 | } 118 | 119 | 120 | ABBNode* ABB::k_element_rec(int k, ABBNode* node){ 121 | ABBNode* ans = nullptr; 122 | if (node != nullptr){ 123 | int lSize = 0; 124 | int posNode = 0; 125 | if (node->getLeft() != nullptr){ 126 | lSize = node->getLeft()->getSize(); 127 | } 128 | posNode = lSize + 1; 129 | 130 | if (k == posNode){ 131 | ans = node; 132 | } 133 | else if (k > posNode ){ 134 | ans = k_element_rec( k - posNode, node->getRight()); 135 | } 136 | else{ 137 | ans = k_element_rec( k, node->getLeft()); 138 | } 139 | } 140 | return ans; 141 | 142 | } 143 | 144 | ABBNode* ABB::k_element(int k){ 145 | return k_element_rec(k, root); 146 | } 147 | 148 | ABB::~ABB() { 149 | delete root; 150 | } 151 | 152 | } /* namespace trees */ 153 | -------------------------------------------------------------------------------- /abb/src/abbNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abbNode.cpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/abbNode.hpp" 9 | 10 | namespace trees { 11 | 12 | ABBNode::ABBNode(): 13 | ptrLeft(nullptr), data(-1), ptrRight(nullptr) { 14 | // TODO Auto-generated constructor stub 15 | } 16 | 17 | ABBNode::ABBNode(int val): 18 | ptrLeft(nullptr), data(val), ptrRight(nullptr){ 19 | 20 | } 21 | 22 | void ABBNode::setLeft(ABBNode* node){ 23 | ptrLeft = node; 24 | } 25 | 26 | void ABBNode::setRight(ABBNode* node){ 27 | ptrRight = node; 28 | } 29 | 30 | void ABBNode::setData(int val){ 31 | data = val; 32 | } 33 | 34 | void ABBNode::setSize(int s){ 35 | size = s; 36 | } 37 | 38 | ABBNode* ABBNode::getLeft(){ 39 | return ptrLeft; 40 | } 41 | 42 | ABBNode* ABBNode::getRight(){ 43 | return ptrRight; 44 | } 45 | 46 | int ABBNode::getData(){ 47 | return data; 48 | } 49 | 50 | int ABBNode::getSize(){ 51 | return size; 52 | } 53 | 54 | ABBNode::~ABBNode() { 55 | if (ptrLeft != nullptr){ 56 | delete ptrLeft; 57 | } 58 | if (ptrRight != nullptr){ 59 | delete ptrRight; 60 | } 61 | } 62 | 63 | } /* namespace trees */ 64 | -------------------------------------------------------------------------------- /abb/tests/test.cpp: -------------------------------------------------------------------------------- 1 | #include "trees/abb.hpp" 2 | #include 3 | 4 | int main(int nargas, char** vargs){ 5 | trees::ABB abb; 6 | abb.insert(16); 7 | abb.insert(4); 8 | abb.insert(2); 9 | abb.insert(20); 10 | abb.insert(15); 11 | abb.insert(18); 12 | abb.insert(35); 13 | abb.insert(50); 14 | //abb.showASC(); 15 | abb.updateSize(); 16 | abb.traverse(); 17 | 18 | trees::ABBNode* node = nullptr; 19 | for (int k = 1; k<= 8; k++ ){ 20 | node = abb.k_element(k); 21 | if (node != nullptr){ 22 | std::cout << "k = " < "<< node->getData() << std::endl; 23 | } 24 | } 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /avl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 3 | # set the project name 4 | 5 | project(misc 6 | VERSION 1.0 7 | LANGUAGES CXX) 8 | 9 | set(CMAKE_CXX_STANDARD 11) 10 | set(CMAKE_CXX_STANDARD_REQUIRED True) 11 | 12 | add_executable(test_avl tests/test.cpp src/avlNode.cpp src/avl.cpp) 13 | target_include_directories(test_avl PUBLIC ${PROJECT_SOURCE_DIR}/include) 14 | 15 | 16 | 17 | # add the executable -------------------------------------------------------------------------------- /avl/include/trees/avl.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abb.hpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef ABB_HPP_ 9 | #define ABB_HPP_ 10 | 11 | #include "trees/avlNode.hpp" 12 | 13 | namespace trees { 14 | 15 | enum RotationType{ 16 | LeftRotation = 10, 17 | RightRotation = 20, 18 | LeftRightRotation = 30, 19 | RightLeftRotation = 40 20 | }; 21 | 22 | class AVL { 23 | private: 24 | AVLNode* root; 25 | /*private methods, rotations*/ 26 | void balance(AVLNode* node); 27 | void leftRotation(AVLNode* node); 28 | void rightRotation(AVLNode* node); 29 | void leftRightRotation(AVLNode* node); 30 | void rightLeftRotation(AVLNode* node); 31 | RotationType getRotationType(AVLNode* node); 32 | public: 33 | AVL(); 34 | void insert_rec(int val, AVLNode* node); 35 | void insert(int val); 36 | AVLNode* find_rec(int val, AVLNode* node); 37 | AVLNode* find(int val); 38 | void traverse_rec(AVLNode* node, int level); 39 | void traverse(); 40 | virtual ~AVL(); 41 | }; 42 | 43 | } /* namespace trees */ 44 | 45 | #endif /* ABB_HPP_ */ 46 | -------------------------------------------------------------------------------- /avl/include/trees/avlNode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abbNode.h 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef ABBNODE_HPP_ 9 | #define ABBNODE_HPP_ 10 | 11 | namespace trees { 12 | 13 | enum NodeType{ 14 | LEFT = 10, 15 | RIGHT = 20 16 | }; 17 | 18 | 19 | class AVLNode { 20 | private: 21 | int data; 22 | AVLNode* ptrLeft; 23 | AVLNode* ptrRight; 24 | AVLNode* parent; 25 | int hLeft; 26 | int hRight; 27 | NodeType type; 28 | 29 | 30 | public: 31 | AVLNode(); 32 | AVLNode(int val, AVLNode* _parent = nullptr); 33 | void setLeft(AVLNode* node); 34 | void setRight(AVLNode* node); 35 | void setParent(AVLNode* node); 36 | void setData(int val); 37 | void setLeftHeight(int h); 38 | void setRightHeight(int h); 39 | void setType(NodeType t); 40 | bool isLeft(); 41 | bool isRight(); 42 | AVLNode* getLeft(); 43 | AVLNode* getRight(); 44 | AVLNode* getParent(); 45 | int getData(); 46 | int getLeftHeight(); 47 | int getRightHeight(); 48 | int getHeight(); 49 | int getBalanceScore(); 50 | char getType(); 51 | void updateChildrenHeights(); 52 | virtual ~AVLNode(); 53 | }; 54 | 55 | } /* namespace trees */ 56 | 57 | #endif /* ABBNODE_H_ */ 58 | -------------------------------------------------------------------------------- /avl/src/avl.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abb.cpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/avl.hpp" 9 | #include 10 | 11 | 12 | namespace trees { 13 | 14 | AVL::AVL():root(nullptr) { 15 | // TODO Auto-generated constructor stub 16 | } 17 | 18 | void AVL::balance(AVLNode* node){ 19 | RotationType rType = getRotationType(node); 20 | std::cout << "Rotation Type : " << rType << std::endl; 21 | if (rType == RotationType::LeftRotation){ 22 | leftRotation(node); 23 | } 24 | else if (rType == RotationType::RightRotation){ 25 | rightRotation(node); 26 | } 27 | else if (rType == RotationType::LeftRightRotation){ 28 | leftRightRotation(node); 29 | } 30 | else if (rType == RotationType::RightLeftRotation){ 31 | rightLeftRotation(node); 32 | } 33 | 34 | } 35 | 36 | RotationType AVL::getRotationType(AVLNode* node){ 37 | /*this will run if node is unbalanced*/ 38 | RotationType rType = RotationType::LeftRightRotation; 39 | AVLNode* leftChild = node->getLeft(); 40 | AVLNode* rightChild = node->getRight(); 41 | if (node->getLeftHeight() > node->getRightHeight()){ 42 | if (leftChild->getLeftHeight() > leftChild->getRightHeight()){ 43 | //unbalance left-left 44 | rType = RotationType::RightRotation; 45 | } 46 | else{ 47 | //unbalance right-left (from deeper node to) 48 | rType = RotationType::LeftRightRotation; 49 | } 50 | } 51 | else{ //RIGHT 52 | if (rightChild->getLeftHeight() > rightChild->getRightHeight()){ 53 | //unbalance left-right 54 | rType = RotationType::RightLeftRotation; 55 | } 56 | else{ 57 | //unbalance right-right 58 | rType = RotationType::LeftRotation; 59 | } 60 | } 61 | return rType; 62 | } 63 | 64 | void AVL::leftRotation(AVLNode* node){ 65 | std::cout<<"****left-rotation "<getData()<getRight(); 67 | AVLNode* parent = node->getParent(); 68 | bool isLeft = node->isLeft(); 69 | node->setRight(rightChild->getLeft()); 70 | rightChild->setLeft(node); 71 | node->updateChildrenHeights(); 72 | rightChild->updateChildrenHeights(); 73 | if (node == root){ 74 | root = rightChild; 75 | } 76 | else{ 77 | if (isLeft){ 78 | parent->setLeft(rightChild); 79 | } 80 | else{ 81 | parent->setRight(rightChild); 82 | } 83 | parent->updateChildrenHeights(); 84 | } 85 | } 86 | 87 | void AVL::leftRightRotation(AVLNode* node){ 88 | std::cout<<"left-right rotation"<getLeft()); 90 | rightRotation(node); 91 | } 92 | 93 | void AVL::rightRotation(AVLNode* node){ 94 | 95 | std::cout<<"***right rotation "<getData()<getLeft(); 97 | AVLNode* parent = node->getParent(); 98 | bool isLeft = node->isLeft(); 99 | node->setLeft(leftChild->getRight()); 100 | leftChild->setRight(node); 101 | node->updateChildrenHeights(); 102 | leftChild->updateChildrenHeights(); 103 | 104 | if (node == root){ 105 | root = leftChild; 106 | } 107 | else{ 108 | if (isLeft){ 109 | parent->setLeft(leftChild); 110 | } 111 | else{ 112 | parent->setRight(leftChild); 113 | } 114 | parent->updateChildrenHeights(); 115 | } 116 | } 117 | 118 | void AVL::rightLeftRotation(AVLNode* node){ 119 | std::cout<<"right-left rotation"<getRight()); 121 | leftRotation(node); 122 | } 123 | 124 | void AVL::insert_rec(int val, AVLNode* node){ 125 | 126 | if (val < node->getData()){ 127 | if (node->getLeft() == nullptr){ 128 | node->setLeft(new AVLNode(val, node)); 129 | } 130 | else{ 131 | insert_rec(val, node->getLeft()); 132 | } 133 | } 134 | else{ 135 | if (node->getRight() == nullptr){ 136 | node->setRight(new AVLNode(val, node)); 137 | } 138 | else{ 139 | insert_rec(val, node->getRight()); 140 | } 141 | } 142 | 143 | node->updateChildrenHeights(); 144 | if (node->getBalanceScore() > 1){ 145 | balance(node); 146 | } 147 | 148 | //std::cout<getData() == val){ 164 | ans = node; 165 | } 166 | else{ 167 | if (val < node->getData()){ 168 | find_rec(val, node->getLeft()); 169 | } 170 | else{ 171 | find_rec(val, node->getRight()); 172 | } 173 | } 174 | return ans; 175 | } 176 | 177 | AVLNode* AVL::find(int val){ 178 | AVLNode* ans = nullptr; 179 | ans = find_rec(val, root); 180 | return ans; 181 | } 182 | 183 | void AVL::traverse_rec(AVLNode* node, int label){ 184 | if (node != nullptr){ 185 | for (int i = 0; i < label; i++){ 186 | std::cout << "*" << std::flush; 187 | } 188 | char T = node->isLeft()?'L':'R'; 189 | std::cout << node->getData() << " " << T <getLeft(), label + 1); 191 | traverse_rec(node->getRight(), label + 1); 192 | } 193 | } 194 | 195 | void AVL::traverse(){ 196 | traverse_rec(root, 1); 197 | } 198 | 199 | 200 | AVL::~AVL() { 201 | delete root; 202 | } 203 | 204 | } /* namespace trees */ 205 | -------------------------------------------------------------------------------- /avl/src/avlNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * abbNode.cpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/avlNode.hpp" 9 | #include 10 | #include 11 | 12 | namespace trees { 13 | 14 | AVLNode::AVLNode(): 15 | data(-1), ptrLeft(nullptr), ptrRight(nullptr), 16 | parent(nullptr), hLeft(0), hRight(0),type(NodeType::LEFT) { 17 | // TODO Auto-generated constructor stub 18 | } 19 | 20 | AVLNode::AVLNode(int val, AVLNode* _parent): 21 | data(val), ptrLeft(nullptr), ptrRight(nullptr), 22 | parent(_parent), hLeft(0), hRight(0), type(NodeType::LEFT){ 23 | } 24 | 25 | void AVLNode::setLeft(AVLNode* node){ 26 | ptrLeft = node; 27 | if (ptrLeft!= nullptr){ 28 | ptrLeft->setParent(this); 29 | ptrLeft->setType(NodeType::LEFT); 30 | } 31 | 32 | } 33 | 34 | void AVLNode::setRight(AVLNode* node){ 35 | ptrRight = node; 36 | if (ptrRight!= nullptr){ 37 | ptrRight->setParent(this); 38 | ptrRight->setType(NodeType::RIGHT); 39 | } 40 | } 41 | 42 | void AVLNode::setType(NodeType t){ 43 | type = t; 44 | } 45 | 46 | bool AVLNode::isLeft(){ 47 | return (type == NodeType::LEFT); 48 | } 49 | 50 | bool AVLNode::isRight(){ 51 | return (type == NodeType::RIGHT); 52 | } 53 | 54 | void AVLNode::setData(int val){ 55 | data = val; 56 | } 57 | 58 | void AVLNode::setParent(AVLNode* node){ 59 | parent = node; 60 | } 61 | 62 | void AVLNode::setRightHeight(int h){ 63 | hRight = h; 64 | } 65 | 66 | void AVLNode::setLeftHeight(int h){ 67 | hLeft = h; 68 | } 69 | 70 | AVLNode* AVLNode::getLeft(){ 71 | return ptrLeft; 72 | } 73 | 74 | AVLNode* AVLNode::getRight(){ 75 | return ptrRight; 76 | } 77 | 78 | int AVLNode::getData(){ 79 | return data; 80 | } 81 | 82 | int AVLNode::getLeftHeight(){ 83 | return hLeft; 84 | } 85 | 86 | int AVLNode::getRightHeight(){ 87 | return hRight; 88 | } 89 | 90 | int AVLNode::getHeight(){ 91 | return hLeft>hRight?hLeft:hRight; 92 | } 93 | AVLNode* AVLNode::getParent(){ 94 | return parent; 95 | } 96 | 97 | int AVLNode::getBalanceScore(){ 98 | return std::abs(hLeft-hRight); 99 | } 100 | 101 | char AVLNode::getType(){ 102 | return isLeft()?'L':'R'; 103 | } 104 | void AVLNode::updateChildrenHeights(){ 105 | if (ptrLeft != nullptr){ 106 | setLeftHeight(ptrLeft->getHeight() + 1); 107 | } 108 | else{ 109 | setLeftHeight(0); 110 | } 111 | if (ptrRight != nullptr){ 112 | setRightHeight(ptrRight->getHeight() + 1); 113 | } 114 | else{ 115 | setRightHeight(0); 116 | } 117 | } 118 | 119 | 120 | AVLNode::~AVLNode() { 121 | if (ptrLeft != nullptr){ 122 | delete ptrLeft; 123 | } 124 | if (ptrRight != nullptr){ 125 | delete ptrRight; 126 | } 127 | } 128 | 129 | } /* namespace trees */ 130 | -------------------------------------------------------------------------------- /avl/tests/test.cpp: -------------------------------------------------------------------------------- 1 | #include "trees/avl.hpp" 2 | #include 3 | 4 | int main(int nargas, char** vargs){ 5 | trees::AVL avl; 6 | avl.insert(16); 7 | avl.insert(32); 8 | avl.insert(45); 9 | avl.insert(8); 10 | avl.insert(10); 11 | avl.insert(15); 12 | 13 | avl.traverse(); 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /eda.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/eda.png -------------------------------------------------------------------------------- /game/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.10) 3 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 4 | # set the project name 5 | 6 | project(misc 7 | VERSION 1.0 8 | LANGUAGES CXX) 9 | 10 | set(CMAKE_CXX_STANDARD 11) 11 | set(CMAKE_CXX_STANDARD_REQUIRED True) 12 | 13 | 14 | add_executable(game game/game.cpp src/utils.cpp src/cell2D.cpp src/node.cpp src/cellStack.cpp) 15 | target_include_directories(game PUBLIC ${PROJECT_SOURCE_DIR}/include) 16 | 17 | 18 | 19 | # add the executable -------------------------------------------------------------------------------- /game/game/game.cpp: -------------------------------------------------------------------------------- 1 | #include "adts/cellStack.hpp" 2 | #include "adts/cell2D.hpp" 3 | #include "adts/node.hpp" 4 | #include "game/utils.hpp" 5 | #include 6 | 7 | bool isAValidCell(adts::Cell2D cell, int size){ 8 | return (cell.getRow() >=0 && cell.getRow() < size && 9 | cell.getCol()>=0 && cell.getCol() < size); 10 | } 11 | bool pathExists(bool** lab, int size, adts::Cell2D start, adts::Cell2D end){ 12 | bool ans = false; 13 | adts::CellStack stack; 14 | stack.push(new adts::Node(start)); 15 | bool** visit = game::createLab(size, false); 16 | int row = 0, col = 0; 17 | while (!ans && !stack.isEmpty()){ 18 | adts::Node* node = stack.top(); 19 | row = node->getData().getRow(); 20 | col = node->getData().getCol(); 21 | visit[row][col] = true; 22 | if (end.getRow() == row && end.getCol() == col){ 23 | ans = true; 24 | } 25 | else{ 26 | stack.pop(); 27 | adts::Cell2D cellTop(row - 1, col); 28 | adts::Cell2D cellBottom(row + 1, col); 29 | adts::Cell2D cellLeft(row, col - 1); 30 | adts::Cell2D cellRight(row, col + 1); 31 | if (isAValidCell(cellTop, size) && 32 | lab[cellTop.getRow()][cellTop.getCol()] && 33 | !visit[cellTop.getRow()][cellTop.getCol()]){ 34 | stack.push(new adts::Node(cellTop)); 35 | } 36 | if (isAValidCell(cellBottom, size) && 37 | lab[cellBottom.getRow()][cellBottom.getCol()] && 38 | !visit[cellBottom.getRow()][cellBottom.getCol()]){ 39 | stack.push(new adts::Node(cellBottom)); 40 | } 41 | if (isAValidCell(cellLeft, size) && 42 | lab[cellLeft.getRow()][cellLeft.getCol()] && 43 | !visit[cellLeft.getRow()][cellLeft.getCol()]){ 44 | stack.push(new adts::Node(cellLeft)); 45 | } 46 | if (isAValidCell(cellRight, size) && 47 | lab[cellRight.getRow()][cellRight.getCol()] && 48 | !visit[cellRight.getRow()][cellRight.getCol()]){ 49 | stack.push(new adts::Node(cellRight)); 50 | } 51 | 52 | } 53 | } 54 | 55 | game::deleteLab(visit, size); 56 | return ans; 57 | } 58 | 59 | int main(int nargas, char** vargs){ 60 | bool data[8][8] ={{1,1,1,0,1,0,1,1}, 61 | {1,0,1,1,1,1,0,1}, 62 | {0,0,1,1,1,1,0,1}, 63 | {1,0,1,1,0,1,0,1}, 64 | {1,0,0,0,1,1,1,1}, 65 | {1,1,0,1,1,0,0,0}, 66 | {1,1,1,1,0,1,1,1}, 67 | {1,1,1,1,0,1,1,1}, 68 | }; 69 | 70 | int size = 8; 71 | bool** lab = game::createLab(size, false); 72 | game::copyData(lab, size, data); 73 | 74 | adts::Cell2D start(1,2); 75 | adts::Cell2D end(0,0); 76 | bool ans = pathExists(lab, size, start, end); 77 | if (ans){ 78 | std::cout<<"Existe Ruta" < 11 | 12 | namespace adts { 13 | 14 | Cell2D::Cell2D():row(-1), col(-1) { 15 | // TODO Auto-generated constructor stub 16 | } 17 | 18 | Cell2D::Cell2D(int _row, int _col): row(_row), col(_col){ 19 | 20 | } 21 | void Cell2D::setRow(int _row){ 22 | row = _row; 23 | } 24 | 25 | void Cell2D::setCol(int _col){ 26 | col = _col; 27 | } 28 | 29 | int Cell2D::getRow(){ 30 | return row; 31 | } 32 | 33 | int Cell2D::getCol(){ 34 | return col; 35 | } 36 | 37 | void Cell2D::print(){ 38 | std::cout << "(" << row<< "," << col << ")" << std::endl; 39 | } 40 | 41 | Cell2D::~Cell2D() { 42 | // TODO Auto-generated destructor stub 43 | } 44 | 45 | } /* namespace adts */ 46 | -------------------------------------------------------------------------------- /game/src/cellStack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * cellStack.cpp 3 | * 4 | * Created on: Sep 13, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/cellStack.hpp" 9 | 10 | namespace adts { 11 | 12 | CellStack::CellStack():head(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | void CellStack::push(Node* node){ 18 | if (head == nullptr){ 19 | head = node; 20 | } 21 | else{ 22 | node->setNext(head); 23 | head = node; 24 | } 25 | } 26 | 27 | void CellStack::pop(){ 28 | if (!isEmpty()){ 29 | Node* ptr = head; 30 | head = head->getNext(); 31 | delete ptr; 32 | } 33 | } 34 | 35 | Node* CellStack::top(){ 36 | return head; 37 | } 38 | 39 | bool CellStack::isEmpty(){ 40 | return (head == nullptr); 41 | } 42 | 43 | void CellStack::clear(){ 44 | while (!isEmpty()){ 45 | pop(); 46 | } 47 | } 48 | 49 | CellStack::~CellStack() { 50 | clear(); 51 | } 52 | 53 | } /* namespace adts */ 54 | -------------------------------------------------------------------------------- /game/src/node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * node.hpp 3 | * 4 | * Created on: Sep 13, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/node.hpp" 9 | 10 | namespace adts { 11 | 12 | Node::Node(): data(), ptrNext(nullptr) { 13 | } 14 | 15 | Node::Node(Cell2D val, Node* next): data(val), ptrNext(next) { 16 | 17 | } 18 | 19 | void Node::setData(Cell2D _data){ 20 | data = _data; 21 | } 22 | 23 | void Node::setNext(Node* next){ 24 | ptrNext = next; 25 | } 26 | 27 | Cell2D Node::getData(){ 28 | return data; 29 | } 30 | Node* Node::getNext(){ 31 | return ptrNext; 32 | } 33 | 34 | void Node::print(){ 35 | data.print() ; 36 | } 37 | 38 | Node::~Node() { 39 | 40 | } 41 | 42 | 43 | } /* namespace adts */ 44 | 45 | 46 | -------------------------------------------------------------------------------- /game/src/utils.cpp: -------------------------------------------------------------------------------- 1 | namespace game{ 2 | bool** createLab(int size,bool val){ 3 | bool** lab = new bool*[size]; 4 | for(int i=0; i 7 | 8 | bool isAValidCell(adts::Cell2D cell, int size){ 9 | return (cell.getRow() >=0 && cell.getRow() < size && 10 | cell.getCol()>=0 && cell.getCol() < size); 11 | } 12 | 13 | bool pathExists(bool** lab, int size, adts::Cell2D start, adts::Cell2D end, adts::CellLinkedList &path){ 14 | bool ans = false; 15 | adts::CellStack stack; 16 | stack.push(new adts::Node(start)); 17 | bool** visit = game::createLab(size, false); 18 | int row = 0, col = 0; 19 | adts::Node* node = nullptr; 20 | while (!ans && !stack.isEmpty()){ 21 | node = stack.top(); 22 | row = node->getData().getRow(); 23 | col = node->getData().getCol(); 24 | if (visit[row][col]){ 25 | //pop when the cell is visited for the second time 26 | stack.pop(); 27 | } 28 | else{ 29 | visit[row][col] = true; 30 | if (end.getRow() == row && end.getCol() == col){ 31 | ans = true; 32 | } 33 | else{ 34 | //add children to the stack 35 | adts::Cell2D cellTop(row - 1, col); 36 | adts::Cell2D cellBottom(row + 1, col); 37 | adts::Cell2D cellLeft(row, col - 1); 38 | adts::Cell2D cellRight(row, col + 1); 39 | if (isAValidCell(cellTop, size) && 40 | lab[cellTop.getRow()][cellTop.getCol()] && 41 | !visit[cellTop.getRow()][cellTop.getCol()]){ 42 | stack.push(new adts::Node(cellTop, nullptr, node)); 43 | } 44 | if (isAValidCell(cellBottom, size) && 45 | lab[cellBottom.getRow()][cellBottom.getCol()] && 46 | !visit[cellBottom.getRow()][cellBottom.getCol()]){ 47 | stack.push(new adts::Node(cellBottom, nullptr, node)); 48 | } 49 | if (isAValidCell(cellLeft, size) && 50 | lab[cellLeft.getRow()][cellLeft.getCol()] && 51 | !visit[cellLeft.getRow()][cellLeft.getCol()]){ 52 | stack.push(new adts::Node(cellLeft, nullptr, node)); 53 | } 54 | if (isAValidCell(cellRight, size) && 55 | lab[cellRight.getRow()][cellRight.getCol()] && 56 | !visit[cellRight.getRow()][cellRight.getCol()]){ 57 | stack.push(new adts::Node(cellRight, nullptr, node)); 58 | } 59 | } 60 | } 61 | } 62 | //copy path 63 | if (ans) { 64 | path.clear(); 65 | node = stack.top(); 66 | while (node != nullptr){ 67 | path.insertAtFirst(new adts::Node(node->getData())); 68 | node = node->getParent(); 69 | } 70 | } 71 | game::deleteLab(visit, size); 72 | return ans; 73 | } 74 | 75 | int main(int nargas, char** vargs){ 76 | bool data[8][8] ={{1,1,1,0,1,0,1,1}, 77 | {1,0,1,1,1,1,0,1}, 78 | {0,0,1,1,1,1,0,1}, 79 | {1,0,1,1,0,1,0,1}, 80 | {1,0,0,0,1,1,1,1}, 81 | {1,1,0,1,1,0,0,0}, 82 | {1,1,1,1,0,1,1,1}, 83 | {1,1,1,1,0,1,1,1}, 84 | }; 85 | 86 | int size = 8; 87 | bool** lab = game::createLab(size, false); 88 | game::copyData(lab, size, data); 89 | 90 | adts::Cell2D start(1,2); 91 | adts::Cell2D end(5,4); 92 | adts::CellLinkedList path; 93 | bool ans = pathExists(lab, size, start, end, path); 94 | if (ans){ 95 | std::cout<<"Existe Ruta" < 11 | 12 | namespace adts { 13 | 14 | Cell2D::Cell2D():row(-1), col(-1) { 15 | // TODO Auto-generated constructor stub 16 | } 17 | 18 | Cell2D::Cell2D(int _row, int _col): row(_row), col(_col){ 19 | 20 | } 21 | void Cell2D::setRow(int _row){ 22 | row = _row; 23 | } 24 | 25 | void Cell2D::setCol(int _col){ 26 | col = _col; 27 | } 28 | 29 | int Cell2D::getRow(){ 30 | return row; 31 | } 32 | 33 | int Cell2D::getCol(){ 34 | return col; 35 | } 36 | 37 | void Cell2D::print(){ 38 | std::cout << "(" << row<< "," << col << ")"< 10 | 11 | namespace adts{ 12 | 13 | CellLinkedList::CellLinkedList():head(nullptr) { 14 | // TODO Auto-generated constructor stub 15 | 16 | } 17 | 18 | void CellLinkedList::insertAtFirst(Node* node){ 19 | node->setNext(head); 20 | head=node; 21 | } 22 | 23 | void CellLinkedList::print(){ 24 | Node* ptr = head; 25 | while(ptr!=nullptr){ 26 | ptr->print(); 27 | std::cout<<"-"<getNext(); 29 | } 30 | std::cout<getNext(); 37 | delete ptr; 38 | } 39 | } 40 | 41 | void CellLinkedList::clear(){ 42 | 43 | while(head != nullptr){ 44 | removeFirst(); 45 | } 46 | } 47 | 48 | CellLinkedList::~CellLinkedList() { 49 | clear(); 50 | } 51 | 52 | } /* namespace trees */ 53 | -------------------------------------------------------------------------------- /game_v2/src/cellStack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * cellStack.cpp 3 | * 4 | * Created on: Sep 13, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/cellStack.hpp" 9 | 10 | namespace adts { 11 | 12 | CellStack::CellStack():head(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | void CellStack::push(Node* node){ 18 | if (head == nullptr){ 19 | head = node; 20 | } 21 | else{ 22 | node->setNext(head); 23 | head = node; 24 | } 25 | } 26 | 27 | void CellStack::pop(){ 28 | if (!isEmpty()){ 29 | Node* ptr = head; 30 | head = head->getNext(); 31 | delete ptr; 32 | } 33 | } 34 | 35 | Node* CellStack::top(){ 36 | return head; 37 | } 38 | 39 | bool CellStack::isEmpty(){ 40 | return (head == nullptr); 41 | } 42 | 43 | void CellStack::clear(){ 44 | while (!isEmpty()){ 45 | pop(); 46 | } 47 | } 48 | 49 | CellStack::~CellStack() { 50 | clear(); 51 | } 52 | 53 | } /* namespace adts */ 54 | -------------------------------------------------------------------------------- /game_v2/src/node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * node.hpp 3 | * 4 | * Created on: Sep 13, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/node.hpp" 9 | 10 | namespace adts { 11 | 12 | Node::Node(): data(), ptrNext(nullptr), ptrParent(nullptr) { 13 | } 14 | 15 | Node::Node(Cell2D val, Node* next, Node* parent): data(val), ptrNext(next), ptrParent(parent) { 16 | 17 | } 18 | 19 | void Node::setData(Cell2D _data){ 20 | data = _data; 21 | } 22 | 23 | void Node::setNext(Node* next){ 24 | ptrNext = next; 25 | } 26 | 27 | void Node::setParent(Node* parent){ 28 | ptrParent = parent; 29 | } 30 | 31 | Cell2D Node::getData(){ 32 | return data; 33 | } 34 | 35 | Node* Node::getNext(){ 36 | return ptrNext; 37 | } 38 | 39 | Node* Node::getParent(){ 40 | return ptrParent; 41 | } 42 | 43 | void Node::print(){ 44 | data.print() ; 45 | } 46 | 47 | Node::~Node() { 48 | 49 | } 50 | 51 | 52 | } /* namespace adts */ 53 | 54 | 55 | -------------------------------------------------------------------------------- /game_v2/src/utils.cpp: -------------------------------------------------------------------------------- 1 | namespace game{ 2 | bool** createLab(int size,bool val){ 3 | bool** lab = new bool*[size]; 4 | for(int i=0; i 4 | #include 5 | 6 | 7 | int main(int nargs, char** vargs){ 8 | std::string filename("images/image_1.bmp"); 9 | std::cout << filename << std::endl; 10 | image::Image* im = nullptr; 11 | im = image::Image::readImage(filename); 12 | im->show(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /imagepro/images/image_1.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/imagepro/images/image_1.bmp -------------------------------------------------------------------------------- /imagepro/images/image_2.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/imagepro/images/image_2.bmp -------------------------------------------------------------------------------- /imagepro/images/image_3.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/imagepro/images/image_3.bmp -------------------------------------------------------------------------------- /imagepro/images/image_4.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/imagepro/images/image_4.bmp -------------------------------------------------------------------------------- /imagepro/images/image_5.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jmsaavedrar/eda_cpp/db13e70b135030fbb5ae69d8149fa25c86d6e915/imagepro/images/image_5.bmp -------------------------------------------------------------------------------- /imagepro/include/image/image.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Class Image 3 | */ 4 | 5 | #include 6 | 7 | namespace image { 8 | class Image; 9 | typedef unsigned char uchar; 10 | 11 | 12 | class Image{ 13 | private: 14 | int width; 15 | int height; 16 | int th_value; 17 | uchar* data; 18 | boolean* visited; 19 | public: 20 | Image(); 21 | Image(int w, int h); 22 | Image(int w, int h, uchar* _data); 23 | void threshold(); 24 | int getValue(int row, int col); 25 | void show(); 26 | //ListOfRegions *getRegions() 27 | virtual ~Image(); 28 | static Image* readImage(std::string &path); 29 | }; 30 | } 31 | -------------------------------------------------------------------------------- /imagepro/include/image/listofpoint2d.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class ListOfPoint2D 3 | */ 4 | 5 | namespace image { 6 | class ListOfPoint2D{ 7 | private: 8 | public: 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /imagepro/include/image/listofregion.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class ListOfRegion 3 | */ 4 | 5 | namespace image { 6 | class ListOfRegion{ 7 | private: 8 | public: 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /imagepro/include/image/nodepoint2d.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class NodePoint2D 3 | */ 4 | 5 | namespace image { 6 | class NodePoint2D{ 7 | private: 8 | public: 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /imagepro/include/image/noderegion.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class NodeRegion 3 | */ 4 | 5 | namespace image { 6 | class NodeRegion{ 7 | private: 8 | public: 9 | }; 10 | } 11 | -------------------------------------------------------------------------------- /imagepro/include/image/point2D.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class Point2D 3 | */ 4 | 5 | 6 | namespace image { 7 | class Point2D{ 8 | private: 9 | int x; 10 | int y; 11 | public: 12 | Point2D(); 13 | Point2D(int _x, int _y); 14 | void setX(int _x); 15 | void setY(int _y); 16 | int getX(); 17 | int getY(); 18 | virtual ~Point2D(); 19 | }; 20 | } 21 | -------------------------------------------------------------------------------- /imagepro/include/image/region.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | Class Region 3 | */ 4 | 5 | namespace image { 6 | class Region{ 7 | private: 8 | int id; 9 | int size; 10 | ListOfPoint2D points; 11 | public: 12 | Region(); 13 | void showRegion(); 14 | virtual ~Region(); 15 | }; 16 | } 17 | -------------------------------------------------------------------------------- /imagepro/src/image.cpp: -------------------------------------------------------------------------------- 1 | #include "image/image.hpp" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | namespace image{ 8 | 9 | Image::Image(): width(0), height(0), th_value(120), data(nullptr){ 10 | 11 | } 12 | 13 | Image::Image(int w, int h): width(w), height(h), th_value(120), data(nullptr){ 14 | 15 | } 16 | 17 | Image::Image(int w, int h, uchar* _data): width(w), height(h), th_value(120), data(_data){ 18 | threshold(); 19 | } 20 | 21 | void Image::threshold(){ 22 | if (data != nullptr) { 23 | for(int i = 0; i < height*width; i++){ 24 | if (static_cast(data[i]) < th_value){ 25 | data[i] = static_cast(0); 26 | } 27 | else{ 28 | data[i] = static_cast(1); 29 | } 30 | } 31 | } 32 | } 33 | 34 | 35 | 36 | int Image::getValue(int row, int col){ 37 | int pos = row*width + col; 38 | return static_cast(data[pos]); 39 | } 40 | 41 | void Image::show(){ 42 | std::cout << "----------------------" << std::endl; 43 | std::cout << "size [ (w: " << width << ") x ( h:" << height << ")]" << std::endl; 44 | std::cout << "---------------------" << std::endl; 45 | for(int i = 0 ; i < height ; i++ ){ 46 | for(int j = 0; j < width; j++ ){ 47 | //std::cout<< getValue(i,j) << " "; 48 | if (getValue(i,j) == 0) { 49 | std::cout<<" "; 50 | } 51 | else{ 52 | std::cout<<"*"; 53 | } 54 | } 55 | std::cout<(ordered_data)); 102 | } 103 | assert(im != nullptr); 104 | return im; 105 | } 106 | 107 | // ListOfRegions* Image::getRegions(int label){ 108 | // ListOfRegions* list = new ListOfRegions(); 109 | // id = 09 110 | // for cada punto en la image 111 | // if punto es no visitado 112 | // region = getRegion(punto, id +1 ) 113 | // list->insert(region) 114 | // id = id + 1 115 | 116 | 117 | // return list; 118 | // } 119 | 120 | // Region* Image::getRegion(punto=i,j, int label)){ 121 | // Region r(label) 122 | // lista de punto l 123 | // Point2D p(i,j) 124 | // stack s 125 | // s->insert(p(i,j)) 126 | // while s->!empty{ 127 | // punto i,j =s->top 128 | // s->pop 129 | // l->insert(punto(i,j)) 130 | // vistado[i,j] = True 131 | 132 | // if s no visitado { 133 | // para cada vecino que no este visitado 134 | // s-insert(vecino) 135 | // } 136 | // } 137 | // r->setList(l) 138 | // r->setLabel(label) 139 | // return r 140 | 141 | // } 142 | 143 | 144 | //} 145 | 146 | 147 | } 148 | 149 | -------------------------------------------------------------------------------- /imagepro/src/listofpoint2d.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class ListOfPoint2D 2 | */ -------------------------------------------------------------------------------- /imagepro/src/listofregion.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class ListOfRegion 2 | */ -------------------------------------------------------------------------------- /imagepro/src/nodepoint2d.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class NodePoint2D 2 | */ -------------------------------------------------------------------------------- /imagepro/src/noderegion.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class NodeRegion 2 | */ -------------------------------------------------------------------------------- /imagepro/src/point2D.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class Point2D 2 | */ 3 | 4 | namespace image{ 5 | Point2D::Point2D(): x(0), y(0) { 6 | 7 | } 8 | 9 | Point2D(int _x, int _y): x(_x), y(_y){ 10 | 11 | } 12 | void Point2D::setX(int _x){ 13 | x = _x; 14 | } 15 | void Point2D::setY(int _y){ 16 | y = _y; 17 | } 18 | int Point2D::getX(){ 19 | return x; 20 | } 21 | int Point2D::getY(){ 22 | return y; 23 | } 24 | virtual Point_2D::~Point2D(){ 25 | 26 | } 27 | } -------------------------------------------------------------------------------- /imagepro/src/region.cpp: -------------------------------------------------------------------------------- 1 | /* implementation of the class Region 2 | */ -------------------------------------------------------------------------------- /io/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.10) 3 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 4 | # set the project name 5 | 6 | project(io 7 | VERSION 1.0 8 | LANGUAGES CXX) 9 | 10 | set(CMAKE_CXX_STANDARD 11) 11 | set(CMAKE_CXX_STANDARD_REQUIRED True) 12 | 13 | add_executable(test tests/test.cpp src/io.cpp) 14 | target_include_directories(test PUBLIC ${PROJECT_SOURCE_DIR}/include) 15 | 16 | 17 | -------------------------------------------------------------------------------- /io/data/ej1.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |

Estructura de Datos

5 | 6 |

Esto es un ejemplo

7 | 8 | 9 | -------------------------------------------------------------------------------- /io/include/io/io.hpp: -------------------------------------------------------------------------------- 1 | #ifndef IO_IO_HPP 2 | #define IO_IO_HPP 3 | #include 4 | 5 | namespace io { 6 | void readTextFile(const std::string &filename); 7 | } 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /io/src/io.cpp: -------------------------------------------------------------------------------- 1 | #include "io/io.hpp" 2 | #include 3 | #include 4 | 5 | namespace io { 6 | void readTextFile(const std::string &filename){ 7 | std::ifstream f_in(filename); 8 | char symbol = '\0'; 9 | if (f_in.is_open()){ 10 | while (f_in.get(symbol)){ 11 | std::cout< 8 | 9 | namespace eda { 10 | 11 | LinkedList::LinkedList(): head(nullptr) { 12 | 13 | } 14 | 15 | void LinkedList::insertFirst(int val){ 16 | Node* node = new Node(val); 17 | if (head == nullptr) { 18 | head = node; 19 | } 20 | else{ 21 | node->setNext(head); 22 | head = node; 23 | } 24 | } 25 | 26 | void LinkedList::insertLast(int val){ 27 | Node* node = new Node(val); 28 | if (head == nullptr) { 29 | head = node; 30 | } 31 | else{ 32 | Node* ptr = head; 33 | while(ptr->getNext() != nullptr){ 34 | ptr = ptr->getNext(); 35 | } 36 | ptr->setNext(node); 37 | } 38 | } 39 | 40 | void LinkedList::removeFirst(){ 41 | Node* ptr = head; 42 | if (head != nullptr){ 43 | head = head->getNext(); 44 | delete ptr; 45 | } 46 | } 47 | 48 | void LinkedList::remove(int val){ 49 | Node* ptr = head; 50 | Node* ptr_prev = nullptr; 51 | while (ptr != nullptr){ 52 | if (ptr->getData() == val){ 53 | //erase node 54 | if (ptr_prev == nullptr) { 55 | head = ptr->getNext(); 56 | delete ptr; 57 | ptr = head; 58 | } 59 | else{ 60 | ptr_prev->setNext(ptr->getNext()); 61 | delete ptr; 62 | ptr = ptr_prev->getNext(); 63 | } 64 | } 65 | else{ 66 | ptr_prev = ptr; 67 | ptr = ptr->getNext(); 68 | } 69 | } 70 | } 71 | 72 | void LinkedList::removeAll(){ 73 | 74 | while (head != nullptr){ 75 | removeFirst(); 76 | } 77 | } 78 | 79 | Node* LinkedList::find(int val){ 80 | Node* ptr = head; 81 | while ((ptr != nullptr) && (ptr->getData() != val)){ 82 | ptr = ptr->getNext(); 83 | } 84 | return ptr; 85 | } 86 | 87 | void LinkedList::print(){ 88 | Node* ptr = head; 89 | while (ptr != nullptr) { 90 | ptr->print(); 91 | std::cout << " -> "; 92 | ptr = ptr->getNext(); 93 | } 94 | std::cout << std::endl; 95 | } 96 | 97 | LinkedList::~LinkedList() { 98 | removeAll(); 99 | } 100 | 101 | } /* namespace eda */ 102 | -------------------------------------------------------------------------------- /lLists/src/node.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * nodo.cpp 3 | */ 4 | 5 | #include "lLists/node.hpp" 6 | #include 7 | 8 | namespace eda { 9 | 10 | 11 | Node::Node(): data(-1), ptrNext(nullptr) { 12 | } 13 | 14 | Node::Node(int val, Node* next): data(val), ptrNext(next) { 15 | 16 | } 17 | 18 | void Node::setData(int _data){ 19 | data = _data; 20 | } 21 | 22 | void Node::setNext(Node* next){ 23 | ptrNext = next; 24 | } 25 | 26 | int Node::getData(){ 27 | return data; 28 | } 29 | Node* Node::getNext(){ 30 | return ptrNext; 31 | } 32 | 33 | void Node::print(){ 34 | std::cout << data ; 35 | } 36 | 37 | Node::~Node() { 38 | 39 | } 40 | 41 | } /* namespace eda */ 42 | -------------------------------------------------------------------------------- /lLists/src/queue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.cpp 3 | * 4 | * Created on: Aug 18, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "lLists/queue.hpp" 9 | 10 | namespace eda { 11 | 12 | Queue::Queue():head(nullptr), tail(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | void Queue::push(int val){ 18 | Node* node = new Node(val); 19 | push(node); 20 | } 21 | 22 | void Queue::push(Node* node){ 23 | if (tail == nullptr){ 24 | head = node; 25 | tail = node; 26 | } 27 | else{ 28 | tail->setNext(node); 29 | tail = node; 30 | } 31 | } 32 | 33 | void Queue::pop(){ 34 | if (!isEmpty()){ 35 | Node* ptr = head; 36 | head = head->getNext(); 37 | delete ptr; 38 | if (head == nullptr){ 39 | tail = nullptr; 40 | } 41 | } 42 | } 43 | 44 | Node* Queue::top(){ 45 | return head; 46 | } 47 | 48 | bool Queue::isEmpty(){ 49 | return (head == nullptr); 50 | } 51 | 52 | void Queue::clear(){ 53 | while (!isEmpty()){ 54 | pop(); 55 | } 56 | } 57 | 58 | Queue::~Queue() { 59 | clear(); 60 | } 61 | 62 | } /* namespace eda */ 63 | -------------------------------------------------------------------------------- /lLists/src/stack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.cpp 3 | * 4 | * Created on: Aug 18, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "lLists/stack.hpp" 9 | #include 10 | 11 | namespace eda { 12 | 13 | Stack::Stack():head(nullptr) { 14 | // TODO Auto-generated constructor stub 15 | 16 | } 17 | 18 | void Stack::push(int val){ 19 | Node* node = new Node(val); 20 | push(node); 21 | } 22 | 23 | void Stack::push(Node* node){ 24 | node->setNext(head); 25 | head = node; 26 | } 27 | 28 | void Stack::pop(){ 29 | if (!isEmpty()){ 30 | Node* ptr = head; 31 | head = head->getNext(); 32 | delete ptr; 33 | } 34 | } 35 | 36 | Node* Stack::top(){ 37 | return head; 38 | } 39 | 40 | bool Stack::isEmpty(){ 41 | return (head == nullptr); 42 | } 43 | 44 | void Stack::clear(){ 45 | while (!isEmpty()){ 46 | pop(); 47 | } 48 | } 49 | 50 | Stack::~Stack() { 51 | std::cout<<"destructor Stack"< 4 | using namespace eda; 5 | 6 | int main(int nargs, char** vargs){ 7 | // Node nodo1(5); 8 | // Node* nodo2 = new Node(7); 9 | // nodo1.setNext(nodo2); 10 | LinkedList list; 11 | list.insertFirst(1); 12 | list.insertFirst(3); 13 | list.insertFirst(5); 14 | list.insertFirst(15); 15 | list.insertFirst(5); 16 | list.insertFirst(17); 17 | list.print(); 18 | list.remove(5); 19 | list.remove(17); 20 | list.print(); 21 | list.removeAll(); 22 | list.insertLast(2); 23 | list.insertLast(12); 24 | list.insertLast(2); 25 | list.print(); 26 | Node* p = list.find(12); 27 | p->setData(18); 28 | std::cout << " after clean " << std::endl; 29 | list.print(); 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /lLists/tests/test_stack_queue.cpp: -------------------------------------------------------------------------------- 1 | #include "lLists/queue.hpp" 2 | #include "lLists/stack.hpp" 3 | #include 4 | using namespace eda; 5 | 6 | int main(int nargs, char** vargs){ 7 | Stack stack; 8 | stack.push(0); 9 | stack.push(10); 10 | stack.push(20); 11 | stack.push(30); 12 | 13 | // while(!stack->isEmpty()){ 14 | // std::cout<top()->getData()<<" "<pop(); 16 | // } 17 | 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /laberinto/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 3 | # set the project name 4 | 5 | project(laberinto 6 | VERSION 1.0 7 | LANGUAGES CXX) 8 | 9 | set(CMAKE_CXX_STANDARD 11) 10 | set(CMAKE_CXX_STANDARD_REQUIRED True) 11 | 12 | #add_subdirectory(src) 13 | #add_subdirectory(tests) 14 | 15 | add_executable(test test/test.cpp src/maze.cpp) 16 | target_include_directories(test PUBLIC ${PROJECT_SOURCE_DIR}/include) 17 | 18 | 19 | # add the executable -------------------------------------------------------------------------------- /laberinto/include/maze/maze.hpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Classe Maze 3 | */ 4 | 5 | namespace maze { 6 | typedef unsigned char uchar; 7 | 8 | 9 | class Maze{ 10 | private: 11 | uchar** grid; 12 | int width; 13 | int height; 14 | int dir[4]; 15 | void shuffle_dir(); 16 | void delete_maze(); 17 | void visit(int i, int j); 18 | public: 19 | static const unsigned char WALL; 20 | static const unsigned char EMPTY; 21 | static const int NORTH; 22 | static const int SOUTH; 23 | static const int EAST; 24 | static const int WEST; 25 | Maze(int h, int w); 26 | void generate_maze(int h, int w); 27 | void reset_maze(int h, int w); 28 | bool inRange(int i, int j); 29 | void print(); 30 | 31 | }; 32 | } 33 | -------------------------------------------------------------------------------- /laberinto/src/maze.cpp: -------------------------------------------------------------------------------- 1 | #include "maze/maze.hpp" 2 | #include 3 | #include 4 | #include 5 | 6 | namespace maze{ 7 | 8 | const unsigned char Maze::WALL = '@'; 9 | const unsigned char Maze::EMPTY = '-'; 10 | const int Maze::NORTH= 0; 11 | const int Maze::SOUTH= 1; 12 | const int Maze::EAST= 2; 13 | const int Maze::WEST= 3; 14 | Maze::Maze(int h, int w): 15 | height(h), 16 | width(w), 17 | grid(nullptr){ 18 | dir[0] = NORTH; 19 | dir[1] = SOUTH; 20 | dir[2] = EAST; 21 | dir[3] = WEST; 22 | std::srand(time(0)); 23 | generate_maze(h, w); 24 | 25 | } 26 | 27 | void Maze::reset_maze(int h, int w){ 28 | delete_maze(); 29 | height = h; 30 | width= w; 31 | grid = new uchar*[height]; 32 | for (int i = 0; i < height; i++){ 33 | grid[i] = new uchar[width]; 34 | for (int j = 0; j < width; j++){ 35 | grid[i][j] = 1; 36 | } 37 | } 38 | } 39 | 40 | void Maze::generate_maze(int h, int w){ 41 | reset_maze(h, w); 42 | visit(0,0); 43 | } 44 | 45 | void Maze::delete_maze(){ 46 | if (grid != nullptr){ 47 | for (int i = 0; i < height; i++){ 48 | delete[] grid[i]; 49 | } 50 | delete[] grid; 51 | } 52 | } 53 | 54 | void Maze::shuffle_dir(){ 55 | for (int i = 0; i < 4; i++){ 56 | int r = std::rand() & 3; 57 | int aux = dir[r]; 58 | dir[r] = dir[i]; 59 | dir[i] = aux; 60 | } 61 | } 62 | bool Maze::inRange(int i, int j){ 63 | return ((i >= 0) && (i< height) && (j >= 0) && (j< width)); 64 | } 65 | 66 | void Maze::visit(int i, int j){ 67 | 68 | int dx = 0; 69 | int dy = 0; 70 | int i_next = 0; 71 | int j_next = 0; 72 | grid[i][j] = 0; 73 | shuffle_dir(); 74 | //std::cout << dir[0] << " " << dir[1] << " " << dir[2] << " "<< dir[3] << std::endl; 75 | for(int k = 0; k < 4; k++){ 76 | //std::cout << dir[k] << std::endl; 77 | if (dir[k] == NORTH){ 78 | dy = -1; 79 | dx = 0; 80 | } 81 | else if (dir[k] == SOUTH){ 82 | dy = 1; 83 | dx = 0; 84 | } 85 | else if (dir[k] == EAST){ 86 | dy = 0; 87 | dx = 1; 88 | } 89 | else if (dir[k] == WEST){ 90 | dy = 0; 91 | dx = -1; 92 | } 93 | i_next = i + (dy<<1); 94 | j_next = j + (dx<<1); 95 | if (inRange(i_next, j_next) && grid[i_next][j_next] == 1){ 96 | grid[i_next - dy][j_next - dx] = 0; 97 | visit(i_next, j_next); 98 | 99 | } 100 | } 101 | } 102 | 103 | void Maze::print(){ 104 | char LIMIT = '='; 105 | std::cout << " Maze ( "<< height << " x " << width << " ) " << std::endl; 106 | std::cout << " "; 107 | for (int j = 0; j < width; j++){ 108 | std::cout << LIMIT; 109 | } 110 | std::cout << " "; 111 | std::cout << std::endl; 112 | for (int i = 0; i < height; i++){ 113 | std::cout << "|"; 114 | for (int j = 0; j < width; j++){ 115 | if (grid[i][j] == 0) { 116 | std::cout << EMPTY; 117 | } 118 | else { 119 | std::cout << WALL; 120 | } 121 | } 122 | std::cout << "|"; 123 | std::cout << std::endl; 124 | } 125 | std::cout << " "; 126 | for (int j = 0; j < width; j++){ 127 | std::cout << LIMIT; 128 | } 129 | std::cout << " "; 130 | std::cout << std::endl; 131 | } 132 | 133 | } 134 | -------------------------------------------------------------------------------- /laberinto/test/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "maze/maze.hpp" 3 | int main(int nargs, char** vargs){ 4 | maze::Maze laberinto(21,21); 5 | laberinto.print(); 6 | 7 | 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /misc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.10) 3 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 4 | # set the project name 5 | 6 | project(misc 7 | VERSION 1.0 8 | LANGUAGES CXX) 9 | 10 | set(CMAKE_CXX_STANDARD 11) 11 | set(CMAKE_CXX_STANDARD_REQUIRED True) 12 | 13 | #add_subdirectory(src) 14 | #add_subdirectory(tests) 15 | 16 | add_executable(test tests/main.cpp src/misc.cpp) 17 | target_include_directories(test PUBLIC ${PROJECT_SOURCE_DIR}/include) 18 | 19 | 20 | -------------------------------------------------------------------------------- /misc/include/misc/misc.hpp: -------------------------------------------------------------------------------- 1 | #ifndef MISC_HPP 2 | #define MISC_HPP 3 | 4 | namespace misc{ 5 | bool isPrime(int n); 6 | void printArray(int* A, int n); 7 | void getMSS(int* A, int n, int *imss_out, int* jmss_out, int* mss_out); 8 | void getMSS_v2(int* A, int n, int *imss_out, int* jmss_out, int* mss_out); 9 | void getMSS_v3(int* A, int n, int *imss_out, int* jmss_out, int* mss_out); 10 | } 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /misc/src/misc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | namespace misc{ 5 | 6 | bool isPrime(int n){ 7 | bool ans = true; 8 | for (int i = 2; i <= static_cast(std::sqrt(n) + 0.5); i++){ 9 | if (n % i == 0){ 10 | ans = false; 11 | } 12 | } 13 | return ans; 14 | } 15 | 16 | void printArray(int* A, int n){ 17 | for (int i = 0; i < n; i++){ 18 | std::cout< mss){ 35 | mss = thisSum; 36 | imss = i; 37 | jmss = j; 38 | } 39 | } 40 | } 41 | *imss_out = imss; 42 | *jmss_out = jmss; 43 | *mss_out = mss; 44 | } 45 | 46 | void getMSS_v2(int* A, int n, int *imss_out, int* jmss_out, int* mss_out){ 47 | int mss = -1; 48 | int imss = 0; 49 | int jmss = 0; 50 | int thisSum = 0; 51 | for(int i = 0; i < n; i++){ 52 | thisSum = 0; 53 | for(int j = i; j < n; j++){ 54 | thisSum += A[j]; 55 | if (thisSum > mss){ 56 | mss = thisSum; 57 | imss = i; 58 | jmss = j; 59 | } 60 | } 61 | } 62 | *imss_out = imss; 63 | *jmss_out = jmss; 64 | *mss_out = mss; 65 | } 66 | 67 | 68 | void getMSS_v3(int* A, int n, int *imss_out, int* jmss_out, int* mss_out){ 69 | int mss = -1; 70 | int imss = 0; 71 | int jmss = 0; 72 | int thisSum = 0; 73 | int start_i = 0; 74 | for (int i = start_i; i < n ; i++){ 75 | thisSum += A[i]; 76 | if (thisSum > mss){ 77 | mss = thisSum; 78 | jmss = i; 79 | imss = start_i; 80 | } 81 | if (thisSum < 0) { 82 | start_i = i + 1; 83 | thisSum = 0; 84 | } 85 | } 86 | *imss_out = imss; 87 | *jmss_out = jmss; 88 | *mss_out = mss; 89 | } 90 | 91 | 92 | } 93 | 94 | 95 | -------------------------------------------------------------------------------- /misc/tests/main.cpp: -------------------------------------------------------------------------------- 1 | #include "misc/misc.hpp" 2 | #include 3 | 4 | int main(int nargs, char** vargs){ 5 | // int n = 7; 6 | // if (misc::isPrime(n)) { 7 | // std::cout<< n << " es primo. " << std::endl; 8 | // } 9 | // else 10 | // { 11 | // std::cout<< n << " NO es primo." << std::endl; 12 | // } 13 | // MSS 14 | int A[]={-2, 11, -1, 3, -3, -2}; 15 | //int A[]={-2, -1, -1, 3, -3, -2}; 16 | int i =0; 17 | int j = 0; 18 | int mss = 0; 19 | int n = 6; 20 | misc::printArray(A, n); 21 | misc::getMSS(A, n, &i, &j, &mss); 22 | std::cout<<" MSS(n2) i: " << i << " j: " << j << " val: "< 7 | 8 | namespace eda { 9 | 10 | 11 | Node::Node(): data('\0'), ptrNext(nullptr) { 12 | } 13 | 14 | Node::Node(char val, Node* next): data(val), ptrNext(next) { 15 | 16 | } 17 | 18 | void Node::setData(char _data){ 19 | data = _data; 20 | } 21 | 22 | void Node::setNext(Node* next){ 23 | ptrNext = next; 24 | } 25 | 26 | char Node::getData(){ 27 | return data; 28 | } 29 | Node* Node::getNext(){ 30 | return ptrNext; 31 | } 32 | 33 | void Node::print(){ 34 | std::cout << data ; 35 | } 36 | 37 | Node::~Node() { 38 | 39 | } 40 | 41 | } /* namespace eda */ 42 | -------------------------------------------------------------------------------- /parenthesis/src/queue.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * queue.cpp 3 | * 4 | * Created on: Aug 18, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/queue.hpp" 9 | 10 | namespace eda { 11 | 12 | Queue::Queue():head(nullptr), tail(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | void Queue::push(char val){ 18 | Node* node = new Node(val); 19 | push(node); 20 | } 21 | 22 | void Queue::push(Node* node){ 23 | if (tail == nullptr){ 24 | head = node; 25 | tail = node; 26 | } 27 | else{ 28 | tail->setNext(node); 29 | tail = node; 30 | } 31 | } 32 | 33 | void Queue::pop(){ 34 | if (!isEmpty()){ 35 | Node* ptr = head; 36 | head = head->getNext(); 37 | delete ptr; 38 | if (head == nullptr){ 39 | tail = nullptr; 40 | } 41 | } 42 | } 43 | 44 | Node* Queue::top(){ 45 | return head; 46 | } 47 | 48 | bool Queue::isEmpty(){ 49 | return (head == nullptr); 50 | } 51 | 52 | void Queue::clear(){ 53 | while (!isEmpty()){ 54 | pop(); 55 | } 56 | } 57 | 58 | Queue::~Queue() { 59 | clear(); 60 | } 61 | 62 | } /* namespace eda */ 63 | -------------------------------------------------------------------------------- /parenthesis/src/stack.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * stack.cpp 3 | * 4 | * Created on: Aug 18, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "adts/stack.hpp" 9 | 10 | namespace eda { 11 | 12 | Stack::Stack():head(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | 18 | void Stack::push(Node* node){ 19 | if (head == nullptr){ 20 | head = node; 21 | } 22 | else{ 23 | node->setNext(head); 24 | head = node; 25 | } 26 | } 27 | 28 | void Stack::pop(){ 29 | if (!isEmpty()){ 30 | Node* ptr = head; 31 | head = head->getNext(); 32 | delete ptr; 33 | } 34 | } 35 | 36 | Node* Stack::top(){ 37 | return head; 38 | } 39 | 40 | bool Stack::isEmpty(){ 41 | return (head == nullptr); 42 | } 43 | 44 | void Stack::clear(){ 45 | while (!isEmpty()){ 46 | pop(); 47 | } 48 | } 49 | 50 | Stack::~Stack() { 51 | clear(); 52 | } 53 | 54 | } /* namespace eda */ 55 | -------------------------------------------------------------------------------- /parenthesis/test/test_parenthesis.cpp: -------------------------------------------------------------------------------- 1 | #include "adts/node.hpp" 2 | #include "adts/queue.hpp" 3 | #include "adts/stack.hpp" 4 | #include 5 | #include 6 | 7 | using namespace eda; 8 | 9 | bool validateParenthesis(const std::string &input, int* pos){ 10 | bool error = false; 11 | int i = 0; 12 | Stack stack; 13 | while (!error && i < input.length()){ 14 | if (input[i] == '(') { 15 | stack.push(new Node(input[i])); 16 | } 17 | if (input[i] == ')') { 18 | if (stack.isEmpty()){ 19 | error = true; 20 | } 21 | else{ 22 | stack.pop(); 23 | } 24 | } 25 | i = i + 1; 26 | } 27 | if (!stack.isEmpty()){ 28 | error = true; 29 | } 30 | *pos = i - 1 ; 31 | return !error; 32 | } 33 | 34 | 35 | int main(int nargs, char** vargs){ 36 | std::string input; 37 | int pos=0; 38 | std::cout<<"Ingresa expresión: "; 39 | std::getline(std::cin, input); 40 | bool status = validateParenthesis(input, &pos); 41 | if (status){ 42 | std::cout<< " Expresión Correcta " << std::endl; 43 | } 44 | else{ 45 | std::cout<< " Expresión Inválida" << std::endl; 46 | std::cout<< "Pos error: " << pos << std::endl; 47 | } 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /pointers/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void funcion(int* &p, int n){ 4 | p = new int[n]; 5 | for (int i = 0; i 10 | 11 | 12 | namespace trees { 13 | 14 | RB::RB():root(nullptr) { 15 | // TODO Auto-generated constructor stub 16 | } 17 | 18 | void RB::balance(RBNode* node){ 19 | //TODO 20 | } 21 | 22 | RotationType RB::getRotationType(RBNode* node){ 23 | /*this will run if node is unbalanced*/ 24 | RotationType rType = RotationType::case_1; 25 | //TODO 26 | return rType; 27 | } 28 | 29 | // void RB::do_case_1(RBNode* node){ 30 | // //TODO 31 | // } 32 | 33 | 34 | 35 | void RB::insert(int val, RBNode* node){ 36 | 37 | if (val < node->getData()){ 38 | if (node->getLeft() == nullptr){ 39 | node->setLeft(new RBNode(val, node)); 40 | } 41 | else{ 42 | insert(val, node->getLeft()); 43 | } 44 | } 45 | else{ 46 | if (node->getRight() == nullptr){ 47 | node->setRight(new RBNode(val, node)); 48 | } 49 | else{ 50 | insert(val, node->getRight()); 51 | } 52 | } 53 | 54 | //TODO 55 | //add code to balance according to the Red-Black rotations 56 | 57 | 58 | 59 | } 60 | 61 | void RB::insert(int val){ 62 | if (root == nullptr){ 63 | root = new RBNode(val); 64 | } 65 | else{ 66 | insert(val, root); 67 | } 68 | } 69 | 70 | RBNode* RB::find(int val, RBNode* node){ 71 | RBNode* ans = nullptr; 72 | if (node->getData() == val){ 73 | ans = node; 74 | } 75 | else{ 76 | if (val < node->getData()){ 77 | find(val, node->getLeft()); 78 | } 79 | else{ 80 | find(val, node->getRight()); 81 | } 82 | } 83 | return ans; 84 | } 85 | 86 | RBNode* RB::find(int val){ 87 | RBNode* ans = nullptr; 88 | ans = find(val, root); 89 | return ans; 90 | } 91 | 92 | void RB::traverse(RBNode* node, int label){ 93 | if (node != nullptr){ 94 | for (int i = 0; i < label; i++){ 95 | std::cout << "*" << std::flush; 96 | } 97 | char T = node->isLeft()?'L':'R'; 98 | std::cout << node->getData() << " " << T <getLeft(), label + 1); 100 | traverse(node->getRight(), label + 1); 101 | } 102 | } 103 | 104 | void RB::traverse(){ 105 | traverse(root, 1); 106 | } 107 | 108 | 109 | RB::~RB() { 110 | delete root; 111 | } 112 | 113 | } /* namespace trees */ 114 | -------------------------------------------------------------------------------- /rb_tree/src/rbNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * rbNode.cpp 3 | * 4 | * Created on: Sep 2, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/rbNode.hpp" 9 | #include 10 | #include 11 | 12 | namespace trees { 13 | 14 | RBNode::RBNode(): 15 | data(-1), ptrLeft(nullptr), ptrRight(nullptr), 16 | parent(nullptr), color(NodeColor::RED), type(NodeType::LEFT) { 17 | // TODO Auto-generated constructor stub 18 | } 19 | 20 | RBNode::RBNode(int val, RBNode* _parent): 21 | data(val), ptrLeft(nullptr), ptrRight(nullptr), 22 | parent(_parent), color(NodeColor::RED), type(NodeType::LEFT){ 23 | } 24 | 25 | void RBNode::setLeft(RBNode* node){ 26 | ptrLeft = node; 27 | if (ptrLeft!= nullptr){ 28 | ptrLeft->setParent(this); 29 | ptrLeft->setType(NodeType::LEFT); 30 | } 31 | 32 | } 33 | 34 | void RBNode::setRight(RBNode* node){ 35 | ptrRight = node; 36 | if (ptrRight!= nullptr){ 37 | ptrRight->setParent(this); 38 | ptrRight->setType(NodeType::RIGHT); 39 | } 40 | } 41 | 42 | void RBNode::setColor(NodeColor c){ 43 | color = c; 44 | } 45 | 46 | void RBNode::setType(NodeType t){ 47 | type = t; 48 | } 49 | 50 | bool RBNode::isLeft(){ 51 | return (type == NodeType::LEFT); 52 | } 53 | 54 | bool RBNode::isRight(){ 55 | return (type == NodeType::RIGHT); 56 | } 57 | 58 | void RBNode::setData(int val){ 59 | data = val; 60 | } 61 | 62 | void RBNode::setParent(RBNode* node){ 63 | parent = node; 64 | } 65 | 66 | RBNode* RBNode::getLeft(){ 67 | return ptrLeft; 68 | } 69 | 70 | RBNode* RBNode::getRight(){ 71 | return ptrRight; 72 | } 73 | 74 | int RBNode::getData(){ 75 | return data; 76 | } 77 | 78 | RBNode* RBNode::getParent(){ 79 | return parent; 80 | } 81 | 82 | 83 | char RBNode::getColor(){ 84 | return color==NodeColor::RED?'R':'B'; 85 | } 86 | 87 | char RBNode::getType(){ 88 | return isLeft()?'L':'R'; 89 | } 90 | 91 | 92 | RBNode::~RBNode() { 93 | if (ptrLeft != nullptr){ 94 | delete ptrLeft; 95 | } 96 | if (ptrRight != nullptr){ 97 | delete ptrRight; 98 | } 99 | } 100 | 101 | } /* namespace trees */ 102 | -------------------------------------------------------------------------------- /rb_tree/test.cpp: -------------------------------------------------------------------------------- 1 | #include "trees/rb.hpp" 2 | #include 3 | #include 4 | #include 5 | 6 | int* readKeysFromFile(std::string filename, int* n_keys){ 7 | std::ifstream fin(filename, std::ios::binary); 8 | char* val = new char[4]; 9 | int n = 0; 10 | fin.read(val, 4); 11 | while (!fin.eof()){ 12 | n = n + 1; 13 | fin.read(val, 4); 14 | } 15 | fin.close(); 16 | fin.open(filename, std::ios::binary); 17 | int* keys = new int[n]; 18 | for(int i=0; i < n; i++){ 19 | fin.read(val, 4); 20 | keys[i] = *reinterpret_cast(val); 21 | } 22 | fin.close(); 23 | *n_keys = n; 24 | delete[] val; 25 | return keys; 26 | } 27 | 28 | int main(int nargs, char** vargs){ 29 | int n_data = 0; 30 | int* data = readKeysFromFile("keys_sorted.bin", &n_data); 31 | trees::RB rbtree; 32 | for(int i=0; i 2 | #include "sort/sort.hpp" 3 | #include "sort/utils.hpp" 4 | 5 | namespace sort{ 6 | 7 | void selectionSort(float* A, int n){ 8 | int smallest = 0; 9 | int i = 0; 10 | int j = 0; 11 | for (i = 0; i < n - 1; i++){ 12 | smallest = i; 13 | for (j = i + 1; j < n; j++){ 14 | if (A[j] < A[smallest]){ 15 | smallest = j; 16 | } 17 | } 18 | swap(A,i,smallest); 19 | } 20 | } 21 | 22 | int split_qs(float* A, int i, int j){ 23 | /*** 24 | * split for quicksort 25 | * i,j are the endpoints 26 | */ 27 | int p = getRandomInt(i, j); 28 | 29 | while (i < j) { 30 | 31 | while ( i < p && A[i] <= A[p]){ 32 | i = i + 1; 33 | } 34 | 35 | while ( j > p && A[j] >= A[p]){ 36 | j = j - 1; 37 | } 38 | 39 | swap(A, i, j); 40 | 41 | if (i == p){ 42 | p = j; 43 | } 44 | else if (j == p){ 45 | p = i; 46 | } 47 | } 48 | return p; 49 | } 50 | 51 | void quickSort(float* A, int i, int j){ 52 | if (i < j){ 53 | int k = split_qs(A, i, j); 54 | quickSort(A, i, k-1); 55 | quickSort(A, k + 1, j); 56 | } 57 | } 58 | 59 | void quickSort(float* A, int n){ 60 | quickSort(A, 0, n - 1); 61 | } 62 | 63 | int k_smallest(float* A, int i, int j, int k){ 64 | int p = split_qs(A, i, j); 65 | int val = 0; 66 | if (k == p){ 67 | val = A[p]; 68 | } 69 | else if (k < p){ 70 | val = k_smallest(A, i, p-1, k); 71 | } 72 | else{ 73 | val = k_smallest(A, p+1, j, k); 74 | } 75 | return val; 76 | } 77 | 78 | int k_smallest(float* A, int n, int k){ 79 | return k_smallest(A, 0, n-1, k); 80 | } 81 | 82 | } 83 | 84 | 85 | 86 | -------------------------------------------------------------------------------- /sort/src/utils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include "sort/utils.hpp" 8 | 9 | namespace sort{ 10 | 11 | float* createArray(int n){ 12 | return new float[n]; 13 | } 14 | 15 | float* createRandomArray(int n){ 16 | float* A = createArray(n); 17 | for (int i = 0; i < n; i++){ 18 | A[i] = std::rand() / static_cast(RAND_MAX); 19 | } 20 | return A; 21 | } 22 | 23 | float* createRandomIntArray(int n, int minVal, int maxVal){ 24 | float* A = createArray(n); 25 | for (int i = 0; i < n; i++){ 26 | A[i] = getRandomInt(minVal, maxVal); 27 | } 28 | return A; 29 | } 30 | 31 | void deleteArray(float* A){ 32 | delete[] A; 33 | } 34 | 35 | void printArray(float* A, int n){ 36 | for (int i = 0; i < n; i++){ 37 | std::cout<(RAND_MAX); 56 | return static_cast(a * (max - min) + min + 0.5); 57 | } 58 | 59 | int* linspace(int max, int n_parts){ 60 | int* V = new int[n_parts]; 61 | int part_size = max / n_parts; 62 | for(int i = 1; i <= n_parts; i++){ 63 | V[i - 1] = part_size * i; 64 | } 65 | return V; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /sort/tests/test.cpp: -------------------------------------------------------------------------------- 1 | #include "sort/sort.hpp" 2 | #include "sort/utils.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | long getElapsedTime(std::chrono::time_point t1, 8 | std::chrono::time_point t2){ 9 | auto int_ms = std::chrono::duration_cast(t2 - t1); 10 | return static_cast(int_ms.count()); 11 | } 12 | 13 | void testSort(int max_size, int n_sizes){ 14 | int* sizes = sort::linspace(max_size, n_sizes); 15 | float* A = nullptr; 16 | int n = 0; 17 | for (int i = 0; i < n_sizes; i++){ 18 | n = sizes[i]; 19 | A = sort::createRandomIntArray(n, 0, 100); 20 | auto start = std::chrono::high_resolution_clock::now(); 21 | //change the sort method here 22 | sort::quickSort(A, n); 23 | // 24 | auto end = std::chrono::high_resolution_clock::now(); 25 | long elapsed = getElapsedTime(start, end); 26 | sort::deleteArray(A); 27 | std::cout << " [" << n << "," << elapsed << "]" << std::endl; 28 | } 29 | delete[] sizes; 30 | } 31 | 32 | 33 | //int main(int nargs, char** nvargs){ 34 | // int n_sizes = 10; 35 | // int max_size = 100000; 36 | // testSort(max_size, n_sizes); 37 | // return 0; 38 | //} 39 | 40 | 41 | int main(int nargs, char** args){ 42 | 43 | std::srand(std::time(nullptr)); 44 | int n = 10; 45 | float* A = sort::createRandomIntArray(n, 0, 100); 46 | sort::printArray(A, n); 47 | std::cout << sort::k_smallest(A, n, 2); 48 | // auto start = std::chrono::high_resolution_clock::now(); 49 | // sort::quickSort(A, n); 50 | // sort::printArray(A, n); 51 | // sort::deleteArray(A); 52 | // auto end = std::chrono::high_resolution_clock::now(); 53 | // std::cout<<" Elapsed : " << getElapsedTime(start, end) << std::endl; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /trees/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | cmake_minimum_required(VERSION 3.10) 3 | set(CMAKE_CXX_COMPILER "/usr/bin/g++") 4 | # set the project name 5 | 6 | project(misc 7 | VERSION 1.0 8 | LANGUAGES CXX) 9 | 10 | set(CMAKE_CXX_STANDARD 11) 11 | set(CMAKE_CXX_STANDARD_REQUIRED True) 12 | 13 | add_executable(test_tree tests/test.cpp src/treeNode.cpp src/treeListNode.cpp src/treeList.cpp src/tree.cpp) 14 | target_include_directories(test_tree PUBLIC ${PROJECT_SOURCE_DIR}/include) 15 | 16 | 17 | 18 | # add the executable -------------------------------------------------------------------------------- /trees/include/trees/tree.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * tree.hpp 3 | * 4 | * Created on: Aug 31, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef TREE_HPP_ 9 | #define TREE_HPP_ 10 | 11 | #include "trees/treeNode.hpp" 12 | 13 | 14 | namespace trees { 15 | 16 | class Tree { 17 | private: 18 | TreeNode* root; 19 | public: 20 | Tree(); 21 | void setRoot(TreeNode* node); 22 | void insert(TreeNode* node, TreeNode* parent); 23 | void insert(int child, int parent); 24 | TreeNode* find_rec(int val, TreeNode* node); 25 | TreeNode* find(int val); 26 | void traverse_rec(TreeNode* node, int level); 27 | void traverse(); 28 | virtual ~Tree(); 29 | }; 30 | 31 | } /* namespace trees */ 32 | 33 | #endif /* TREE_HPP_ */ 34 | -------------------------------------------------------------------------------- /trees/include/trees/treeList.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * childrenList.hpp 3 | * 4 | * Created on: Aug 31, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef CHILDRENLIST_HPP_ 9 | #define CHILDRENLIST_HPP_ 10 | 11 | #include "trees/treeListNode.hpp" 12 | 13 | namespace trees { 14 | 15 | class TreeNode; 16 | class TreeListNode; 17 | class TreeList { 18 | private: 19 | TreeListNode* head; 20 | public: 21 | TreeList(); 22 | TreeListNode* getHead(); 23 | void insertFirst(TreeNode* treeNode); 24 | void removeFirst(); 25 | void remove(int val); 26 | void removeAll(); 27 | bool isEmpty(); 28 | TreeNode* find(int val); 29 | void print(); 30 | virtual ~TreeList(); 31 | }; 32 | 33 | } /* namespace trees */ 34 | 35 | #endif /* CHILDRENLIST_HPP_ */ 36 | -------------------------------------------------------------------------------- /trees/include/trees/treeListNode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * childNode.hpp 3 | * 4 | * Created on: Aug 30, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef CHILDNODE_HPP_ 9 | #define CHILDNODE_HPP_ 10 | 11 | #include "trees/treeNode.hpp" 12 | 13 | namespace trees { 14 | class TreeNode; 15 | class TreeListNode { 16 | private: 17 | TreeNode* data; 18 | TreeListNode* ptrNext; 19 | 20 | public: 21 | TreeListNode(); 22 | TreeListNode(TreeNode* _data, TreeListNode* next = nullptr); 23 | void setData(TreeNode* _data); 24 | void setNext(TreeListNode* _next); 25 | TreeNode* getData(); 26 | TreeListNode* getNext(); 27 | virtual ~TreeListNode(); 28 | }; 29 | 30 | } /* namespace trees */ 31 | 32 | #endif /* CHILDNODE_HPP_ */ 33 | -------------------------------------------------------------------------------- /trees/include/trees/treeNode.hpp: -------------------------------------------------------------------------------- 1 | /* 2 | * treeNode.hpp 3 | * 4 | * Created on: Aug 30, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #ifndef TREENODE_HPP_ 9 | #define TREENODE_HPP_ 10 | 11 | #include "trees/treeList.hpp" 12 | 13 | namespace trees { 14 | 15 | class TreeList; 16 | class TreeNode { 17 | private: 18 | TreeNode* parent; 19 | int data; //data can be of any type 20 | TreeList* children; 21 | public: 22 | TreeNode(); 23 | TreeNode(int val); 24 | void setParent(TreeNode* node); 25 | void setData(int val); 26 | void setChildren(TreeList* list); 27 | TreeNode* getParent(); 28 | int getData(); 29 | TreeList* getChildren(); 30 | virtual ~TreeNode(); 31 | }; 32 | 33 | } /* namespace trees */ 34 | 35 | #endif /* TREENODE_HPP_ */ 36 | -------------------------------------------------------------------------------- /trees/src/tree.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * tree.cpp 3 | * 4 | * Created on: Aug 31, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/tree.hpp" 9 | #include 10 | 11 | namespace trees { 12 | 13 | Tree::Tree(): root(nullptr) { 14 | 15 | } 16 | 17 | void Tree::setRoot(TreeNode* node){ 18 | if (root == nullptr){ 19 | root = node; 20 | } 21 | } 22 | void Tree::insert(TreeNode* child, TreeNode* parent){ 23 | if (parent != nullptr){ 24 | parent->getChildren()->insertFirst(child); 25 | } 26 | } 27 | 28 | void Tree::insert(int val, int val_parent){ 29 | TreeNode* parent = find(val_parent); 30 | if (parent != nullptr){ 31 | TreeNode* child = new TreeNode(val); 32 | insert(child, parent); 33 | std::cout << "insertado " << val << " in " << val_parent << " at " << parent << std::endl; 34 | } 35 | } 36 | 37 | TreeNode* Tree::find_rec(int val, TreeNode* node){ 38 | TreeNode* ans = nullptr; 39 | if (node != nullptr){ 40 | if (node->getData() == val){ 41 | ans = node; 42 | } 43 | else{ // search in children 44 | TreeList* childrenList = node->getChildren(); 45 | TreeListNode* ptr = childrenList->getHead(); 46 | while (ptr!=nullptr && ans == nullptr){ 47 | ans = find_rec(val, ptr->getData()); 48 | ptr = ptr->getNext(); 49 | } 50 | } 51 | } 52 | return ans; 53 | } 54 | 55 | TreeNode* Tree::find(int val){ 56 | TreeNode* ans = find_rec(val, root); 57 | return ans; 58 | } 59 | 60 | 61 | void Tree::traverse_rec(TreeNode* node, int level){ 62 | if (node != nullptr){ 63 | std::cout << std::string(level*2, '-'); 64 | std::cout<getData() << " at level " << level <getChildren(); 66 | TreeListNode* ptr = childrenList->getHead(); 67 | while (ptr!=nullptr){ 68 | traverse_rec(ptr->getData(), level + 1); 69 | ptr = ptr->getNext(); 70 | } 71 | } 72 | } 73 | 74 | void Tree::traverse(){ 75 | traverse_rec(root, 1); 76 | } 77 | 78 | Tree::~Tree() { 79 | delete root; 80 | } 81 | 82 | } /* namespace trees */ 83 | -------------------------------------------------------------------------------- /trees/src/treeList.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * childrenList.cpp 3 | * 4 | * Created on: Aug 31, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/treeList.hpp" 9 | #include 10 | namespace trees { 11 | 12 | TreeList::TreeList(): head(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | } 15 | 16 | void TreeList::insertFirst(TreeNode* treeNode){ 17 | TreeListNode* node = new TreeListNode(treeNode); 18 | if (head == nullptr) { 19 | head = node; 20 | } 21 | else{ 22 | node->setNext(head); 23 | head = node; 24 | } 25 | } 26 | 27 | TreeListNode* TreeList::getHead(){ 28 | return head; 29 | } 30 | 31 | void TreeList::removeFirst(){ 32 | TreeListNode* ptr = head; 33 | if (head != nullptr){ 34 | head = head->getNext(); 35 | delete ptr; 36 | } 37 | } 38 | 39 | 40 | void TreeList::remove(int val){ 41 | TreeListNode* ptr = head; 42 | TreeListNode* ptr_prev = nullptr; 43 | while (ptr != nullptr){ 44 | if ((ptr->getData())->getData() == val){ 45 | //erase node 46 | if (ptr_prev == nullptr) { 47 | head = ptr->getNext(); 48 | delete ptr; 49 | ptr = head; 50 | } 51 | else{ 52 | ptr_prev->setNext(ptr->getNext()); 53 | delete ptr; 54 | ptr = ptr_prev->getNext(); 55 | } 56 | } 57 | else{ 58 | ptr_prev = ptr; 59 | ptr = ptr->getNext(); 60 | } 61 | } 62 | } 63 | 64 | void TreeList::removeAll(){ 65 | while (head != nullptr){ 66 | removeFirst(); 67 | } 68 | } 69 | 70 | TreeNode* TreeList::find(int val){ 71 | TreeListNode* ptr = head; 72 | while ((ptr != nullptr) && ((ptr->getData())->getData() != val)){ 73 | ptr = ptr->getNext(); 74 | } 75 | return ptr->getData(); 76 | } 77 | 78 | bool TreeList::isEmpty(){ 79 | return (head == nullptr); 80 | } 81 | 82 | void TreeList::print(){ 83 | TreeListNode* ptr = head; 84 | while (ptr != nullptr){ 85 | std::cout << ptr->getData()->getData() << " - " < getNext(); 87 | } 88 | } 89 | 90 | TreeList::~TreeList() { 91 | removeAll(); 92 | } 93 | 94 | 95 | 96 | 97 | 98 | } /* namespace trees */ 99 | 100 | 101 | -------------------------------------------------------------------------------- /trees/src/treeListNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * childNode.cpp 3 | * 4 | * Created on: Aug 30, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/treeListNode.hpp" 9 | 10 | namespace trees { 11 | 12 | TreeListNode::TreeListNode(): data(), ptrNext(nullptr) { 13 | // TODO Auto-generated constructor stub 14 | 15 | } 16 | 17 | TreeListNode::TreeListNode(TreeNode* _data, 18 | TreeListNode* next) : data(_data), ptrNext(next){ 19 | 20 | } 21 | void TreeListNode::setData(TreeNode* _data){ 22 | data = _data; 23 | } 24 | void TreeListNode::setNext(TreeListNode* _next){ 25 | ptrNext = _next; 26 | } 27 | TreeNode* TreeListNode::getData(){ 28 | return data; 29 | } 30 | TreeListNode* TreeListNode::getNext(){ 31 | return ptrNext; 32 | } 33 | 34 | TreeListNode::~TreeListNode() { 35 | //Actions when a TreeListNode is deleted 36 | delete data; 37 | } 38 | 39 | } /* namespace trees */ 40 | -------------------------------------------------------------------------------- /trees/src/treeNode.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * treeNode.cpp 3 | * 4 | * Created on: Aug 30, 2022 5 | * Author: jsaavedr 6 | */ 7 | 8 | #include "trees/treeNode.hpp" 9 | 10 | namespace trees { 11 | 12 | TreeNode::TreeNode(): parent(nullptr), data(-1), children(new TreeList()) { 13 | } 14 | 15 | TreeNode::TreeNode(int val): parent(nullptr), data(val), children(new TreeList()) { 16 | } 17 | 18 | void TreeNode::setParent(TreeNode* node){ 19 | parent = node; 20 | } 21 | void TreeNode::setData(int val){ 22 | data = val; 23 | } 24 | void TreeNode::setChildren(TreeList* list){ 25 | children = list; 26 | } 27 | TreeNode* TreeNode::getParent(){ 28 | return parent; 29 | } 30 | int TreeNode::getData(){ 31 | return data; 32 | } 33 | TreeList* TreeNode::getChildren(){ 34 | return children; 35 | } 36 | 37 | TreeNode::~TreeNode() { 38 | //action when a treeNode is deleted 39 | //delete the descendants only 40 | if (children != nullptr){ 41 | delete children; 42 | } 43 | } 44 | 45 | } /* namespace trees */ 46 | -------------------------------------------------------------------------------- /trees/tests/test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "trees/tree.hpp" 3 | #include 4 | 5 | int main(int nargs, char** vargs){ 6 | trees::Tree tree; 7 | tree.setRoot(new trees::TreeNode(10)); 8 | tree.insert(5,10); 9 | tree.insert(6,5); 10 | tree.insert(7,10); 11 | tree.insert(17,7); 12 | tree.insert(71,7); 13 | tree.insert(41,7); 14 | tree.traverse(); 15 | std::cout<<"Mostrar los hijos de 10" << std::endl; 16 | trees::TreeNode* node = tree.find(10); 17 | if (node != nullptr){ 18 | node->getChildren()->print(); 19 | } 20 | return 0; 21 | } 22 | --------------------------------------------------------------------------------