#pragma once
#include <iostream>
struct HeapStruct;
typedef struct HeapStruct* PriorityQueue;
PriorityQueue Initialize(int MaxElements);
void Destroy(PriorityQueue h);
void MakeEmpty(PriorityQueue h);
void Insert(int x, PriorityQueue h);
int DeleteMin(PriorityQueue h);
int Findmin(PriorityQueue h);
int IsEmpty(PriorityQueue h);
int IsFull(PriorityQueue h);
struct HeapStruct {
int Capacity;
int Size;
int* Elements;
};
#include "Myheap.h"
PriorityQueue Initialize(int MaxElements)
{
PriorityQueue H = new HeapStruct;
H->Elements = new int[MaxElements];
H->Size = 0;
for (int i = 0; i < MaxElements; i++)
{
H->Elements[i] = -1;
}
H->Capacity = NULL;
return H;
}
void Destroy(PriorityQueue h)
{
}
void MakeEmpty(PriorityQueue h)
{
}
void Insert(int x, PriorityQueue h)
{
int i = ++h->Size;
if (IsEmpty(h))
{
--h->Size;
return;
}
while (true)
{
if (h->Elements[i / 2 - 1] != -1 && h->Elements[i/2-1] > x) {
h->Elements[i] = h->Elements[i / 2 - 1];
i = i / 2 - 1;
}
else
{
h->Elements[i] = x;
break;
}
}
}
int DeleteMin(PriorityQueue h) {
if (h->Size == 0) {
return -1;
}
int minElement = h->Elements[1];
int lastElement = h->Elements[h->Size--];
int i = 1, child;
// Percolate down
for (; i * 2 <= h->Size; i = child) {
child = i * 2;
// Select the smaller child
if (child != h->Size && h->Elements[child + 1] < h->Elements[child]) {
child++;
}
// Check if the last element can be placed here
if (lastElement > h->Elements[child]) {
h->Elements[i] = h->Elements[child];
} else {
break;
}
}
h->Elements[i] = lastElement;
return minElement;
}
int Findmin(PriorityQueue h)
{
return 0;
}
int IsEmpty(PriorityQueue h)
{
return h->Size == 0;
}
int IsFull(PriorityQueue h)
{
return 0;
}