diff --git a/07-heaps/.idea/.gitignore b/07-heaps/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/07-heaps/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/07-heaps/.idea/.name b/07-heaps/.idea/.name new file mode 100644 index 0000000..e87788e --- /dev/null +++ b/07-heaps/.idea/.name @@ -0,0 +1 @@ +07_heaps \ No newline at end of file diff --git a/07-heaps/.idea/07-heaps.iml b/07-heaps/.idea/07-heaps.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/07-heaps/.idea/07-heaps.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/07-heaps/.idea/inspectionProfiles/Project_Default.xml b/07-heaps/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/07-heaps/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/07-heaps/.idea/misc.xml b/07-heaps/.idea/misc.xml new file mode 100644 index 0000000..0b76fe5 --- /dev/null +++ b/07-heaps/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/07-heaps/.idea/modules.xml b/07-heaps/.idea/modules.xml new file mode 100644 index 0000000..4620096 --- /dev/null +++ b/07-heaps/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/07-heaps/.idea/vcs.xml b/07-heaps/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/07-heaps/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/07-heaps/CMakeLists.txt b/07-heaps/CMakeLists.txt new file mode 100644 index 0000000..2074bf2 --- /dev/null +++ b/07-heaps/CMakeLists.txt @@ -0,0 +1,7 @@ +cmake_minimum_required(VERSION 3.28) +project(07_heaps) + +set(CMAKE_CXX_STANDARD 20) + +add_executable(07_heaps main.cpp + Heap.cpp) diff --git a/07-heaps/Heap.cpp b/07-heaps/Heap.cpp new file mode 100644 index 0000000..a95809c --- /dev/null +++ b/07-heaps/Heap.cpp @@ -0,0 +1,85 @@ +/* *~*~* +Implementation file for the Heap class: max-heap of integers +Written By: Iurii Tatishchev +Changed by: Iurii Tatishchev +IDE: CLion +*~**/ + +#include +#include "Heap.h" + +/* *~*~* + The private member function _reHeapUp rearranges the heap after insert by moving the + last item up to the correct location in the heap + *~**/ +void Heap::_reHeapUp(int lastndx) { + + // base case, newElement is heap's root + if (lastndx == 0) return; + + int parentIndex = _findParent(lastndx); + // base case, newElement satisfies heap property (child not greater than parent) + if (heapAry[lastndx] <= heapAry[parentIndex]) return; + + // swap and continue recursing + std::swap(heapAry[lastndx], heapAry[parentIndex]); + _reHeapUp(parentIndex); +} + +/* *~*~* + The private member function _reHeapDown rearranges the heap after delete by moving the + data in the root down to the correct location in the heap + *~**/ +void Heap::_reHeapDown(int rootdex) { + int maxChildIndex = -1; + + int left = _findLeftChild(rootdex); + int right = _findRightChild(rootdex); + + // if there is a left child + if (left != -1) { + // if there is a right child that is greater than the left child + if (right != -1 && heapAry[right] > heapAry[left]) maxChildIndex = right; + else maxChildIndex = left; + } + + // base case, heap property satisfied (children less than parent) + if (maxChildIndex == -1 || heapAry[rootdex] > heapAry[maxChildIndex]) return; + + // swap and continue recursing + std::swap(heapAry[rootdex], heapAry[maxChildIndex]); + _reHeapDown(maxChildIndex); +} + +/* *~*~* + The public member function insertHeap inserts a new item into a heap. + It calls _reheapUp. + *~**/ +bool Heap::insertHeap(int newItem) { + if (isFull()) { + return false; + } + + heapAry[count] = newItem; + _reHeapUp(count); + count++; + + return true; +} + +/* *~*~* + The public member function deleteHeap deletes the root of the heap and + passes back the root's data. It calls _reheapDown. + *~**/ +bool Heap::deleteHeap(int &returnItem) { + if (isEmpty()) { + return false; + } + + returnItem = heapAry[0]; + heapAry[0] = heapAry[count - 1]; + count--; + _reHeapDown(0); + + return true; +} diff --git a/07-heaps/Heap.h b/07-heaps/Heap.h new file mode 100644 index 0000000..7060546 --- /dev/null +++ b/07-heaps/Heap.h @@ -0,0 +1,54 @@ +/* *~*~* +Specification file for the Heap class: max-heap of integers +Written By: Iurii Tatishchev +IDE: CLion +*~**/ + +#ifndef HEAP_H_ +#define HEAP_H_ + +class Heap { +private: + int *heapAry; + int heapSize; + int count; + + void _reHeapUp(int lastndx); + + void _reHeapDown(int rootndx); + + int _findParent(int index) { return (index <= 0) ? (-1) : (index - 1) / 2; } + + int _findLeftChild(int index) { return (2 * index + 1 >= count) ? (-1) : (2 * index + 1); } + + int _findRightChild(int index) { return (2 * index + 2 >= count) ? (-1) : (2 * index + 2); } + +public: + Heap() { + count = 0; + heapSize = 128; + heapAry = new int[heapSize]; + } + + Heap(int n) { + count = 0; + heapSize = n; + heapAry = new int[heapSize]; + } + + ~Heap() { delete[] heapAry; } + + int getCount() const { return count; } + + int getSize() const { return heapSize; } + + bool isEmpty() const { return count == 0; } + + bool isFull() const { return count == heapSize; } + + bool insertHeap(int itemIn); + + bool deleteHeap(int &itemOut); +}; + +#endif diff --git a/07-heaps/main.cpp b/07-heaps/main.cpp new file mode 100644 index 0000000..dce6428 --- /dev/null +++ b/07-heaps/main.cpp @@ -0,0 +1,40 @@ +/* + This program will read integers from the keyboard, + insert them into a max-heap, and display them as + they are deleted from the heap. +*/ + +#include +#include "Heap.h" + +using namespace std; + +int main() { + + Heap heap; + + + // build heap + int num; + cout << "Enter integers [0 - to stop]" << endl; + cin >> num; + while (num != 0) { + heap.insertHeap(num); + cin >> num; + } + + cout << "Heap capacity: " << heap.getSize() << endl; + cout << "Heap actual number of elements: " << heap.getCount() << endl; + + // print items as they are deleted from the heap (sorted) + if (heap.isEmpty()) { + cout << "N/A"; + } + while (!heap.isEmpty()) { + heap.deleteHeap(num); + cout << num << " "; + } + cout << endl; + + return 0; +}