cis22c/07-heaps/Heap.cpp
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

95 lines
2.5 KiB
C++

/* *~*~*
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 less 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 less than the left child
if (right != -1 && heapAry[right] < heapAry[left]) maxChildIndex = right;
else maxChildIndex = left;
}
// base case, heap property satisfied (children greater 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;
}
void Heap::_printIndented(int index, int level, void (*visit)(int item, int level)) {
if (_findRightChild(index) != -1)
_printIndented(_findRightChild(index), level + 1, visit);
visit(heapAry[index], level);
if (_findLeftChild(index) != -1)
_printIndented(_findLeftChild(index), level + 1, visit);
}