10.6 Lab: Heap - Build a max-heap (of integers)

This program will read integers from the keyboard, insert them into a max-heap, and display them as they are deleted from the heap. Most of the code is given:

- Heap.h
- Heap.cpp
- main.cpp

Your task is to finish defining four function in Heap.cpp:

- insertHeap
- deleteHeap
- _reHeapUp
- _reHeapDown
This commit is contained in:
Iurii Tatishchev 2024-05-28 18:33:44 -07:00
parent 276575ca62
commit 0ca7ce28fd
Signed by: CaZzzer
GPG Key ID: 9A156B7DA6398968
11 changed files with 224 additions and 0 deletions

8
07-heaps/.idea/.gitignore generated vendored Normal file
View File

@ -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

1
07-heaps/.idea/.name generated Normal file
View File

@ -0,0 +1 @@
07_heaps

2
07-heaps/.idea/07-heaps.iml generated Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

7
07-heaps/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakePythonSetting">
<option name="pythonIntegrationState" value="YES" />
</component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
07-heaps/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/07-heaps.iml" filepath="$PROJECT_DIR$/.idea/07-heaps.iml" />
</modules>
</component>
</project>

6
07-heaps/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

7
07-heaps/CMakeLists.txt Normal file
View File

@ -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)

85
07-heaps/Heap.cpp Normal file
View File

@ -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 <utility>
#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;
}

54
07-heaps/Heap.h Normal file
View File

@ -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

40
07-heaps/main.cpp Normal file
View File

@ -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 <iostream>
#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;
}