From 312c151a4735c7eb73b1d6b55856ca7da8e239de Mon Sep 17 00:00:00 2001 From: Thiago Luigi Date: Sat, 3 Sep 2022 21:23:55 -0300 Subject: [PATCH 1/3] added minheap sorting --- SortingAlgorithms/heap_sort.cpp | 60 +++++++++++++++++++++++++++------ 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/SortingAlgorithms/heap_sort.cpp b/SortingAlgorithms/heap_sort.cpp index d1b0ef8a..ae57070d 100644 --- a/SortingAlgorithms/heap_sort.cpp +++ b/SortingAlgorithms/heap_sort.cpp @@ -20,35 +20,75 @@ void maxheapify(int arr[], int size, int i) } } +void minheapfy(int arr[], int size, int i) +{ + int min = i; + int left = 2*i + 1; + int right = 2*i + 2; + if(left < size && arr[left] < arr[min]) + min = left; -void heapsort(int arr[], int size) + if(right < size && arr[right] < arr[min]) + min = right; + + if(min!=i) + { + swap(arr[i], arr[min]); + minheapfy(arr, size, min); + } + +} + +void heapsort(int arr[], int size, int c) { - int i; - - for(i=size/2 - 1; i>=0 ; i--) - maxheapify(arr, size, i); + if(c==0) + { + int i; + + for(i=size/2 - 1; i>=0 ; i--) + minheapfy(arr, size, i); - for(i=size-1; i>=0; i--) + for(i=size-1; i>=0; i--) + { + swap(arr[0],arr[i]); + minheapfy(arr, i , 0); + } + } + else { - swap(arr[0],arr[i]); - maxheapify(arr, i , 0); + int i; + + for(i=size/2 - 1; i>=0 ; i--) + maxheapify(arr, size, i); + + for(i=size-1; i>=0; i--) + { + swap(arr[0],arr[i]); + maxheapify(arr, i , 0); + } } } int main() { int n; + cout<<"Type the size of the array: "<>n; int a[n]; int i; for(i=0;i>a[i]; - heapsort(a,n); + int c; + cout<<"Choose the type of sorting: minheap[0] or maxheap[1]: "<>c; + heapsort(a,n,c); for(i=0;i Date: Sat, 3 Sep 2022 22:09:26 -0300 Subject: [PATCH 2/3] Added 2-3-4 tree --- SearchingAlgorithms/2-3-4_Tree.cpp | 342 +++++++++++++++++++++++++++++ 1 file changed, 342 insertions(+) create mode 100644 SearchingAlgorithms/2-3-4_Tree.cpp diff --git a/SearchingAlgorithms/2-3-4_Tree.cpp b/SearchingAlgorithms/2-3-4_Tree.cpp new file mode 100644 index 00000000..f13ee582 --- /dev/null +++ b/SearchingAlgorithms/2-3-4_Tree.cpp @@ -0,0 +1,342 @@ +#include + +using namespace std; + +// constants for verification +const int MAXITEMS = 3; +const int MINITEMS = 1; +const int MAXCHILDREN = 4; + +// middle element position +const int HALF = 1; + +// stored data type +typedef int Data; + +class node { +friend class bt234; +private: + bool leaf; + int num; // number of items stored in the node + Data items[MAXITEMS]; + node* children[MAXCHILDREN]; +public: + node(); + ~node(); +}; + +node::node() { + num = 0; + leaf = false; + for (int i = 0; i < MAXCHILDREN; i++) { + children[i] = NULL; + } +} + +node::~node() { + for (int i = 0; i < num+1; i++) { + delete children[i]; + } +} + +class bt234 { +private: + node* root; + void transverseInOrderAux(node* current, int level); + // helper functions to find smallest and largest value of a subtree with parent in subroot + Data* minAux(node* subRoot); + Data* maxAux(node* subRoot); + // helper function for recursive search + Data* searchAux(node* subRoot, int key); + // auxiliary functions for inserting a Data d in the node aNode + node* insertAux(node* aNode, Data aItem, Data& promotedItem); + node* divideNode(node* aNode, Data& promotedItem); + void insertsInNotFullLeafNode(node* aNode, Data aItem); + void insertsInIntermediateNotFullNode(node* aNode, node* newNode, Data promotedItem); +public: + bt234() { root = NULL; } + ~bt234() { delete root; } + void insert(Data aItem); + // remove an item with a given key - NOT IMPLEMENTED YET + void remove(int key); + // fetch an item in the tree from the key + Data* search(int key); + // lowest and highest value of the tree + Data* min(); + Data* max(); + // traversal print + void transverseInOrder(); +}; + +void bt234::insert(Data aItem) { + // if tree is empty, allocate leaf node to root and insert object at start position + if (root == NULL) { + root = new node; + root->leaf = true; + root->items[0] = aItem; + root->num = 1; + } else { // already has something in the root + // to receive child item that was split + Data promotedItem; + // node generated in case of split + node* newNode; + // call recursive helper method + newNode = insertAux(root, aItem, promotedItem); + // checks if there was a split at the root + if (newNode) { + // if newNode is not null, there was division + // create new root pointing with old root and newNode as children + cout << "*** Root Division ***" << endl; + node* oldRoot = root; + root = new node; + root->items[0] = promotedItem; + root->num = 1; + root->children[0] = oldRoot; + root->children[1] = newNode; + } + } +} + +node* bt234::insertAux(node* aNode, Data aItem, Data& promotedItem) { + // If aNode is a leaf, find the place to insert new item + if (aNode->leaf) { + // checking that aNode is not full + if (aNode->num < MAXITEMS) { + // is not full, just insert + insertsInNotFullLeafNode(aNode, aItem); + return NULL; + } + else { + // aNode is full, need to split + node* newNode; + newNode = divideNode(aNode, promotedItem); + // checks who will receive the item, whether aNode or newNode + if (aItem <= aNode->items[HALF]) { + // item stays in aNode + insertsInNotFullLeafNode(aNode, aItem); + } + else { + // item stays in newNode + insertsInNotFullLeafNode(newNode, aItem); + } + return newNode; + } + } + else { // node is not leaf + // Find child who will receive new item + int i = aNode->num-1; + while (i >= 0 && aNode->items[i] > aItem) { + i--; + } + node* nohAux; + nohAux = insertAux(aNode->children[i+1], aItem, promotedItem); + // checks if there was no overflow in the child + if (nohAux) { // if newNode is not null, there was a division + // Before inserting promoted item + // checks if it should not split the current node. + // To do so, store promoted item in auxiliary variable + Data itemAux = promotedItem; + if (aNode->num < MAXITEMS) { + // aNode is not full, just fix the child's overflow + insertsInIntermediateNotFullNode(aNode, nohAux, itemAux); + return NULL; + } + else { + // aNode is full, divide before fixing child overflow + node* newNode; + newNode = divideNode(aNode, promotedItem); + // checks who will receive new node and promoted item, whether aNode or newNode + if (itemAux <= aNode->items[HALF]) { + // node and item stays in aNode + insertsInIntermediateNotFullNode(aNode, nohAux, itemAux); + } + else { + // node and item stays in newNode + insertsInIntermediateNotFullNode(newNode, nohAux, itemAux); + } + return newNode; + } + } // if newNode is null, no need to do anything + } + return NULL; // just to avoid a warning +} + +node* bt234::divideNode(node* aNode, Data& promotedItem) { + cout << "** Node Division **" << endl; + promotedItem = aNode->items[HALF]; + node* newNode = new node; + newNode->leaf = aNode->leaf; + newNode->items[0] = aNode->items[HALF+1]; + // now each node has only half the elements + newNode->num = 1; + aNode->num = 1; + // if node is not leaf, split children + if (not aNode->leaf) { + for (int i = 0; i < HALF+1; i++) { + newNode->children[i] = aNode->children[HALF+1+i]; + } + } + return newNode; +} + +void bt234::insertsInNotFullLeafNode(node* aNode, Data aItem) { + int pos = (aNode->num - 1); + while ((pos >= 0) and (aNode->items[pos] > aItem)) { + // moving to insert + aNode->items[pos+1] = aNode->items[pos]; + pos--; + } + // inserting + aNode->items[pos+1] = aItem; + aNode->num++; +} + +void bt234::insertsInIntermediateNotFullNode(node* aNode, node* newNode, Data promotedItem) { + int pos = (aNode->num - 1); + while ((pos >= 0) and (aNode->items[pos] > promotedItem)) { + // Move to insert + aNode->items[pos+1] = aNode->items[pos]; + aNode->children[pos+2] = aNode->children[pos+1]; + pos--; + } + // Inserts new item into found spot + aNode->items[pos+1] = promotedItem; + // inserts new node + aNode->children[pos+2] = newNode; + aNode->num++; +} + +void bt234::transverseInOrder() { + transverseInOrderAux(root,0); + cout<< endl; +} + +void bt234::transverseInOrderAux(node* aNode, int level) { + int i; + for (i = 0; i < aNode->num; i++) { + // if node isnt a leaf, print children i data + // before printing item i + if (not aNode->leaf) { + transverseInOrderAux(aNode->children[i], level+1); + } + cout << aNode->items[i] << '/' << level << ' '; + } + + // Print last children data + if (not aNode->leaf) { + transverseInOrderAux(aNode->children[i], level+1); + } +} + +Data* bt234::min() { + if (root == NULL) { + cerr << "Empty Tree!" << endl; + return NULL; + } else { + Data* smallestItem = minAux(root); + return smallestItem; + } +} + +Data* bt234::minAux(node* subRoot) { + if (subRoot->leaf) { + // returns first element + return &(subRoot->items[0]); + } else { + // enters first child (leftmost item) + return minAux(subRoot->children[0]); + } +} + +Data* bt234::max() { + if (root == NULL) { + cerr << "Empty Tree!" << endl; + return NULL; + } else { + Data* bigestItem = maxAux(root); + return bigestItem; + } +} + +Data* bt234::maxAux(node* subRoot) { + int pos = subRoot->num - 1; + if (subRoot->leaf) { + // returns last element + return &(subRoot->items[pos]); + } else { + // enters last child (rightmost child) + return maxAux(subRoot->children[pos+1]); + } +} + +Data* bt234::search(int key) { + if (root == NULL) { + cerr << "Empty Tree!" << endl; + return NULL; + } else { + Data* searchedItem = searchAux(root,key); + return searchedItem; + } +} + +Data* bt234::searchAux(node* subRoot, int key) { + int i = 0; + // loops through subRoot until it finds an item with a key greater than or equal to the searched one + while ((i < subRoot->num) and (subRoot->items[i] <= key)) + i++; + // returns to last position, undoing the last increment + i--; + // if it is the same, the service is finished + if (subRoot->items[i] == key) { + return &(subRoot->items[i]); + } + else { + // if node is leaf, then it does not have the element sought + if (subRoot->leaf) { + cout << "Element not found!" << endl; + return NULL; + } + // is not a leaf, descends in the child to the left of the item with a key greater than the one sought + else { + return searchAux(subRoot->children[i+1],key); + } + } +} + + +int main() { + bt234* B = new bt234; + int n; + + cin >> n; + + while (n >= 0) { + cout << "=> inserting " << n << endl; + Data d = n; + B->insert(d); + B->transverseInOrder(); + cin >> n; + } + + Data* min; + min = B->min(); + if (min) + cout << "#min: " << *min << endl; + + Data* max; + max = B->max(); + if (max) + cout << "#max: " << *max << endl; + + cin >> n; + Data* search; + search = B->search(n); + cout << "#search: "; + if (search) + cout << *search << endl; + else + cout << -1 << endl; + + delete B; + return 0; +} \ No newline at end of file From 22b509c033b710e7b55ecaff267e858d587922b8 Mon Sep 17 00:00:00 2001 From: MatheusMatiasLima <40282042+MatheusMatiasLima@users.noreply.github.com> Date: Sat, 3 Sep 2022 22:19:11 -0300 Subject: [PATCH 3/3] Add List --- SortingAlgorithms/list.cpp | 196 +++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 SortingAlgorithms/list.cpp diff --git a/SortingAlgorithms/list.cpp b/SortingAlgorithms/list.cpp new file mode 100644 index 00000000..2951b1ea --- /dev/null +++ b/SortingAlgorithms/list.cpp @@ -0,0 +1,196 @@ +#include + +using namespace std; + +typedef int Dado; + +class noh { + friend class list; + private: + const Dado dado; + noh* next; + public: + noh(Dado d = 0); +}; + +noh::noh(Dado d) : dado(d) { + next = NULL; +} + +class list { + private: + noh* first; + noh* last; + int size; + public: + list(); + ~list(); + void insert(Dado dado); + void insert(Dado dado, int pos); + void remove(int position); + int find(Dado value); + void print(); + inline bool empty(); +}; + +// build the empty list +list::list() { + size = 0; + first = NULL; + last = NULL; +} + +list::~list() { + noh* iter = first; + noh* next = NULL; + while (iter != NULL) { + next = iter->next; + delete iter; + iter = next; + } +} + +// scrolls through the list printing the items +void list::print() { + noh* aux = first; + while (aux != NULL) { + cout << aux->dado << " "; + aux = aux->next; + } + cout << endl; +} + +// insert item at the end of the list. +void list::insert(Dado dado) { + noh* newItem = new noh (dado); + if (empty()) { + first = newItem; + last = newItem; + } + else { + last->next = newItem; + last = newItem; + } + size++; +} + +// insert an item in the chosen position +void list::insert(Dado dado, int position) { + if (position <= size) { + noh* newItem = new noh (dado); + if (empty()) { + first = newItem; + last = newItem; + } + else if (position == 0) { + newItem->next = first; + first = newItem; + } + else if (position == size) { + last->next = newItem; + last = newItem; + } + else { + noh* aux = first; + for (int i=0; i < position-1; i++) { + aux = aux->next; + } + newItem->next = aux->next; + aux->next = newItem; + } + size++; + } +} + +// remove an element at position +void list::remove(int position) { + if ((position < size) and (position >= 0)) { + if (position == 0) { + noh* aux = first; + first = first->next; + delete aux; + } + else { + noh* aux = first->next; + noh* ant = first; + int posAux = 1; + while ((aux != NULL) and (posAux < position)) { + ant = aux; + aux = aux->next; + posAux++; + } + if (aux == last) { + ant->next = NULL; + last = ant; + delete aux; + } + else { + ant->next = aux->next; + delete aux; + } + } + size--; + if (size == 0) { + first = NULL; + last = NULL; + } + } else { + cout << "ERRO" << endl; + } +} + +// searches for a value and returns the position +// returns -1 if not found +int list::find(Dado value) { + int position = 0; + noh* aux = first; + while (aux != NULL) { + if (aux->dado == value) { + return position; + } + else { + aux = aux->next; + position++; + } + } + return -1; +} + +// checks if the list is empty +inline bool list::empty() { + return first ? false : true; +} + +int main() { + list myList; + char opcao; + int value, pos; + cin >> opcao; + while (opcao != 'Q') { + switch(opcao) { + case 'I': + cin >> value; + myList.insert(value); + break; + case 'W': + cin >> value; + cin >> pos; + myList.insert(value, pos); + break; + case 'P': + cin >> value; + cout << myList.find(value) << endl; + break; + case 'R': + cin >> pos; + myList.remove(pos); + break; + case 'V': + cout << myList.empty() << endl; + break; + } + cin >> opcao; + } + myList.print(); + return 0; +} \ No newline at end of file