cis22c/07-heaps/Heap.h
Iurii Tatishchev 0789614ef1
10.9 Lab: Heap - Build min/max-heaps (of integers)
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.
2024-05-28 19:21:43 -07:00

57 lines
1.2 KiB
C++

/* *~*~*
Specification file for the Heap class: min- or max-heap of integers
Written By: Iurii Tatishchev
Changed by: Iurii Tatishchev
IDE: CLion
*~**/
#ifndef HEAP_H_
#define HEAP_H_
class Heap {
private:
int *heapAry;
int heapSize;
int count;
void _reHeapUp(int lastndx, int (compareFunc)(int, int));
void _reHeapDown(int rootndx, int (compareFunc)(int, int));
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, int (compareFunc)(int, int));
bool deleteHeap(int &itemOut, int (compareFunc)(int, int));
};
#endif