diff --git a/theory/data_structure/binary_trees.cpp b/theory/data_structure/binary_trees.cpp new file mode 100644 index 0000000..4ef7480 --- /dev/null +++ b/theory/data_structure/binary_trees.cpp @@ -0,0 +1,94 @@ +#include +#include +#include +#include + +// Implementation of binary search tree +class Node { + + public: + int value; + int height; + Node *left; + Node *right; + + // constructor + Node(int value) { + this->value = value; + this->left = nullptr; + this->right = nullptr; + this->height = 0; + } +}; + +// Creating new nodes, the first'll be the root +Node *newNode(int n) { + return new Node(n); +} + +// insert nodes +void insert(Node *&root, int n) { + + // if the root is empty we create a new tree, n being root + if(root == nullptr) { + newNode(n); + return; + } + + // else, check if n is greater than or lesser than actual node + Node *tmp = root; + + // check the left case + if(n < tmp->value) { + if(tmp->left == nullptr) { + tmp->left = newNode(n); + return; + } else + insert(tmp->left,n); + } else if(n > tmp->value) // check the right case + if(tmp->right == nullptr) { + tmp->right = newNode(n); + return; + } else + insert(tmp->right,n); + +} + +// printing tree values in pre-order method +void print(Node *root) { + + // base case + if(root == nullptr) + return; + + // recursion calls + std::cout << root->value << " "; + print(root->left); + print(root->right); + +} + +// Balancing Binary tree + + + + + + + +int main(void){ + + Node *tree = newNode(12); + insert(tree,9); + insert(tree,7); + insert(tree,10); + insert(tree,13); + insert(tree,14); + + print(tree); + + +} + + + diff --git a/theory/data_structure/extract_digit.cpp b/theory/data_structure/extract_digit.cpp new file mode 100644 index 0000000..9960bc8 --- /dev/null +++ b/theory/data_structure/extract_digit.cpp @@ -0,0 +1,18 @@ +#include + +// i is the digit that want +// the order is right to left +int extract_digit(int n, int i) { + + if (i == 1) + return n%10; + + return extract_digit(n/10,i-1); + +} + +int main(void){ + + std::cout << extract_digit(1223456789,2); + +} \ No newline at end of file diff --git a/theory/data_structure/linked_list.cpp b/theory/data_structure/linked_list.cpp new file mode 100644 index 0000000..373b3b0 --- /dev/null +++ b/theory/data_structure/linked_list.cpp @@ -0,0 +1,160 @@ +#include +#include +#include + +// Singled linked list +class Node { + public: + int data; + int size = 0; + Node *next; + + // methods + void head(Node *head) { + if(head != nullptr) + std::cout << head->data << std::endl; + else + std::cout << "NULL" << std::endl; + } + + void tail(Node *head) { + + if(head == nullptr) { + std::cout << "NULL" << std::endl; + return; + } + + Node *tmp = head; + while(tmp->next != nullptr) + tmp = tmp->next; + + std::cout << tmp->data << std::endl; + + } + + int getSize(Node *head) { + Node *tmp = head; + while(tmp->next != nullptr) { + head->size++; + tmp = tmp->next; + } + return head->size; + } + + + // constructor + Node(int data) { + this->data = data; + next = nullptr; + } +}; + +// Creating a new node +// initialy, adding to the first position, but this function +// is called to add at the final +void addFinal(Node *&head, int n) { + + Node *newNode = new Node(n); + Node *tmp = head; + while(tmp->next != nullptr) + tmp = tmp->next; + + tmp->next = newNode; + +} + +// Adding elements at the first position +void addFirst(Node *&head, int n) { + + if(head == nullptr) { + addFinal(head,n); + return; + } + + Node *newNode = new Node(n); + Node *tmp = head; + head = newNode; + head->next = tmp; + +} + +// Adding element at an i position +void addAt(Node *&head, int n, int i) { + + if(head == NULL) + return; + + Node *tmp = head; + int index = 0; + while(index < i-1) { + tmp = tmp->next; + index++; + } + + Node *tmp2 = tmp->next; + Node *newNode = new Node(n); + tmp->next = newNode; + newNode->next = tmp2; + +} + +// Function to print list +void printList(Node *head) { + + Node *tmp = head; + while(tmp != nullptr) { + std::cout << tmp->data << "->"; + tmp = tmp->next; + } + std::cout << "NULL" << std::endl; + +} + + +int main(){ + + // Statically lists + Node n1(1); + Node n2(2); + n1.next = &n2; + + // Dinamically lists + Node *n3 = new Node(3); + Node *n4 = new Node(4); + n3->next = n4; + + //Creating a linked list with n nodes + Node *node = new Node(10); + addFinal(node,12); + addFinal(node,122); + addFinal(node,1112); + addFinal(node,3412); + addFinal(node,2); + + printList(node); + + std::cout << "Atual head: "; + node->head(node); + + std::cout << "Atual tail: "; + node->tail(node); + + addFirst(node,20); + addAt(node,1,2); + + printList(node); + std::cout << node->getSize(node) << std::endl; + +} + + + + + + + + + + + +