BinaryHeap.h:
#include <iostream>
#include <string>
#define MAX_HEAP_SIZE 101
class BinaryMinHeap {
public:
BinaryMinHeap(int *heapArray);
BinaryMinHeap(int size);
~BinaryMinHeap() {
delete []heapArray;
}
void insert(int value);
void removeMin();
int getMin();
void displayHeapArray();
private:
int *heapArray;
int heapSize;
int heapArraySize;
void siftUp(int nodeIndex);
void siftDown(int nodeIndex);
int getLeftChildIndex(int nodeIndex) {
return 2*nodeIndex+1;
}
int getRightChildIndex(int nodeIndex) {
return 2*nodeIndex+2;
}
int getParentIndex(int nodeIndex) {
return (nodeIndex-1)/2;
}
};
BinaryHeap.cpp:
#include "BinaryHeap.h"
#include <stdio.h>
BinaryMinHeap::BinaryMinHeap(int size) {
heapArray = new int[size];
heapSize = 0;
heapArraySize = size;
}
void BinaryMinHeap::insert(int value) {
if(heapSize == heapArraySize) {
throw std::string("Insertion Error: The heap is full!");
} else {
heapSize++;
heapArray[heapSize-1] = value;
siftUp(heapSize-1);
}
}
/*
* NON-Recursive Version
*/
void BinaryMinHeap::siftUp(int nodeIndex) {
int i = nodeIndex;
int parent = getParentIndex(nodeIndex);
while(parent >= 0) {
if(heapArray[parent] > heapArray[i]) {
int tmp = heapArray[parent];
heapArray[parent] = heapArray[i];
heapArray[i] = tmp;
i = parent;
parent = getParentIndex(parent);
} else {
break;
}
}
// fprintf(stderr, "parent = %d\n", parent);
}
void BinaryMinHeap::removeMin() {
if(heapSize == 0) {
throw std::string("Remove Error: The heap is empty!");
} else {
heapArray[0] = heapArray[heapSize - 1];
heapSize--;
siftDown(0);
}
}
void BinaryMinHeap::siftDown(int nodeIndex) {
int leftIndex = getLeftChildIndex(nodeIndex);
int rightIndex = getRightChildIndex(nodeIndex);
int minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;
while(minIndex <= heapSize) {
if(heapArray[minIndex] < heapArray[nodeIndex]) {
int tmp = heapArray[minIndex];
heapArray[minIndex] = heapArray[nodeIndex];
heapArray[nodeIndex] = tmp;
nodeIndex = minIndex;
leftIndex = getLeftChildIndex(nodeIndex);
rightIndex = getRightChildIndex(nodeIndex);
minIndex = (heapArray[leftIndex]>heapArray[rightIndex])?rightIndex:leftIndex;
} else {
break;
}
}
}
void BinaryMinHeap::displayHeapArray() {
if(heapSize == 0) {
throw std::string("Empty Heap!");
} else {
std::cout << "\n--------------------\n";
std::cout << "heap size is:\t" << heapSize << std::endl;
for(int i = 0; i < heapSize; i++) {
std::cout << heapArray[i] << "\t";
}
std::cout << "\n--------------------\n";
}
}
int BinaryMinHeap::getMin() {
return heapArray[0];
}
Main.cpp:
#include "BinaryHeap.h"
using namespace std;
int main() {
BinaryMinHeap minHeap(10);
minHeap.insert(10);
minHeap.displayHeapArray();
minHeap.insert(23);
minHeap.displayHeapArray();
minHeap.insert(11);
minHeap.displayHeapArray();
minHeap.insert(4);
minHeap.displayHeapArray();
minHeap.insert(3);
minHeap.displayHeapArray();
minHeap.removeMin();
minHeap.displayHeapArray();
minHeap.removeMin();
minHeap.displayHeapArray();
minHeap.removeMin();
minHeap.displayHeapArray();
minHeap.removeMin();
minHeap.displayHeapArray();
minHeap.removeMin();
try {
minHeap.displayHeapArray();
} catch(std::string e) {
cout << e << endl;
}
return 0;
}