cis22c/07-heaps/Heap.h
Iurii Tatishchev cc06e5fbdb
10.8 Lab: Heap - Display Heap as an Indented List
Change the previous lab to display a min-heap as an indented list (level numbers included). In order to do this you will have to add two new member functions to the heap class

- void _printIndented(int index, void visit(int, int)); // A
- void printIndented(void visit(int, int)); // B

Note: One function would be sufficient(A). However, to make the call simpler, a wrapper function (B) has been added (to "hide" the root of the heap).

Another solution is to add level as a parameter to _printIndented:

- void _printIndented(int index, int level, void visit(int, int));
2024-05-28 18:59:23 -07:00

60 lines
1.3 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);
// other functions added
void printIndented(void visit(int item, int level)) { _printIndented(0, 0, visit); };
void _printIndented(int index, int level, void visit(int item, int level));
};
#endif