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
55 lines
1.1 KiB
C++
55 lines
1.1 KiB
C++
/* *~*~*
|
|
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
|