Write Binary Heap program in C++. And Insert E,H,I,D,G,F,A,B,C.Then, use reheapdown algorithm and print out the result.
Answer
/*
* Heap.H File
*/
#ifndef HEAP_H
#define HEAP_H
#include <iostream>
#include <climits>
using namespace std;
void swap(char *x, char *y);
class Heap
{
char *hPtr; // pointer to array of elements in heap
int maxCap; // maximum capacity
int size; // Current size
public:
Heap(int capacity);
int Parent(int i)
{
return (i-1)/2;
}
/* Since it is binary heap hence only left and right Child*/
int LeftChild(int i)
{
return (2*i + 1);
}
int RightChild(int i)
{
return (2*i + 2);
}
void Insert(char key);
void HeapDown(int i, char new_val);
void printHeap();
char GetMax()
{
return hPtr[0];
}
};
#endif
/* Heap.h ends here */
/*
* Heap.cpp file
*/
#include “Heap.h”
void swap(char *x, char *y)
{
char temp = *x;
*x = *y;
*y =