Generalize one of the previous labs to build a min-heap or a max-heap using the same heap functions. In main() pass either compareMin or compareMax to the insertHeap function: - minHeap.insertHeap(num, compareMin); - maxHeap.insertHeap(num, compareMax); You also have to update the following functions of the Heap class: - bool insertHeap(int itemIn); - bool deleteHeap(int &itemOut); - void _reHeapUp(int lastndx); - void _reHeapDown(int rootndx); This program will: - read integers from the keyboard and insert them into a min-heap and a max-heap - display the integers as they are deleted from the min-heap. - display the integers as they are deleted from the max-heap.
88 lines
2.6 KiB
C++
88 lines
2.6 KiB
C++
/* *~*~*
|
|
Implementation file for the Heap class: min- or 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, int (compareFunc)(int, int)) {
|
|
|
|
// base case, newElement is heap's root
|
|
if (lastndx == 0) return;
|
|
|
|
int parentIndex = _findParent(lastndx);
|
|
// base case, newElement satisfies heap property
|
|
// min heap - child not less than parent
|
|
// max heap - child not greater than parent
|
|
if (compareFunc(heapAry[lastndx], heapAry[parentIndex]) != -1) return;
|
|
|
|
// swap and continue recursing
|
|
std::swap(heapAry[lastndx], heapAry[parentIndex]);
|
|
_reHeapUp(parentIndex, compareFunc);
|
|
}
|
|
|
|
/* *~*~*
|
|
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 (compareFunc)(int, int)) {
|
|
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
|
|
// min heap - less than the left child
|
|
// max heap - greater than the left child
|
|
if (right != -1 && compareFunc(heapAry[right], heapAry[left]) == -1) maxChildIndex = right;
|
|
else maxChildIndex = left;
|
|
}
|
|
|
|
// base case, heap property satisfied
|
|
// min heap - children greater than parent
|
|
// max heap - children less than parent
|
|
if (maxChildIndex == -1 || compareFunc(heapAry[rootdex], heapAry[maxChildIndex]) == -1) return;
|
|
|
|
// swap and continue recursing
|
|
std::swap(heapAry[rootdex], heapAry[maxChildIndex]);
|
|
_reHeapDown(maxChildIndex, compareFunc);
|
|
}
|
|
|
|
/* *~*~*
|
|
The public member function insertHeap inserts a new item into a heap.
|
|
It calls _reheapUp.
|
|
*~**/
|
|
bool Heap::insertHeap(int newItem, int (compareFunc)(int, int)) {
|
|
if (isFull()) return false;
|
|
|
|
heapAry[count] = newItem;
|
|
_reHeapUp(count, compareFunc);
|
|
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, int (compareFunc)(int, int)) {
|
|
if (isEmpty()) return false;
|
|
|
|
returnItem = heapAry[0];
|
|
heapAry[0] = heapAry[count - 1];
|
|
count--;
|
|
_reHeapDown(0, compareFunc);
|
|
|
|
return true;
|
|
}
|