Problem with priority queue using heap

Hello. I am trying to build a program that will print the convex hull of n points, but in order to do this i was told to implement a priority queue using heap. This should store the coordinates x and y of one point and the key is suppossed to be the distance between two points. The first problem that I have is trying to implement the priority queue using heap, because we briefly talked in class about it and i have no example. I tried to google it, this is what I have now but of course, it's not working.

binary_heap.hpp :
#ifndef PRIORITY_QUEUE_H
#define PRIORITY_QUEUE_H
#include <string>

class priority_queue_overflow{}; //daca prin insert se incearca marirea dimensiunii lui A, se arunca exceptia priority_queue_overflow()
class priority_queue_underflow{}; //daca extract_min se apeleaza pe un heap gol, atunci se arunca exceptia priority_queue_overflow()

class priority_queue{
private:

class arrayH {
public:
int x;
int y;
double key;
};

arrayH* A; //vectorul/array-ul folosit pentru a stoca heap-ul
int heapsize; //dimensiunea curenta a heap-ului
int size; //dimensiunea curenta a vectorului: nu se schimba

void heapify(int k);

public:

priority_queue(int n); //don't forget to allocate the array of size n+1 as we don't use slot zero
~priority_queue(); //delete the array

bool empty(); //true/false daca heap-ul este gol sau nu
void insert(int x, int y, double priority); //adauga punctele x si y in heap, cu cheia de prioritate aferenta

};
#endif /* PRIORITY_QUEUE */


cpp file
#include <iostream>
#include <istream>
#include <ostream>
#include <sstream>
#include <map>
#include <algorithm>
#include <stdio.h>
#include <conio.h>
using namespace std;

#include "../PQHeap/binary_heap.hpp"

/* inline functions */
inline int left(int i) { return i << 1; }

inline int right(int i) { return i << 1 | 1; }

inline int parent(int i) { return i >> 1; }


/* constructor */
priority_queue::priority_queue(int n)
{
heapsize = 0;
size = n;
A = new arrayH[n+1];
}


/* destructor */
priority_queue::~priority_queue() //sterge vectorul
{
delete[] A;
}


/* heapify * finds max of three things */
void priority_queue::heapify(int k)
{
arrayH smallest = A[k];
int pos = k;

//only treat child as object if it's inside heap
if (left(k) <= heapsize && A[left(k)].key < A[pos].key)
{
smallest = A[left(k)];
pos = left(k);
}

if (right(k) <= heapsize && A[right(k)].key < A[pos].key)
{
smallest = A[right(k)];
pos = right(k);
}

// after both if's exectued: smallest is pair with smallest key and
// pos is index of that array in A[]

// only need to do something if pos is NOT the same position
// that we were passed in originally
if (pos != k) {

// swap items
std::swap(A[k], A[pos]);

// go recursive to trickle down...
heapify(pos);
}
}


/* empty */
bool priority_queue::empty()
{
return (heapsize == 0);
}


/* insert */
void priority_queue::insert(int x, int y, double priority) //add s to the heap with the given priority as its key
{
if (heapsize == size) {
throw priority_queue_overflow();
}

// facem loc pentru insert
++heapsize;
// Adaugam coordonata x a punctului
A[heapsize].x = x;
//Adaugam coordonata y a punctului
A[heapsize].y = y;
// desemnam cheia de prioritate membrului
A[heapsize].key = priority;

// loop through and trickle up as needed
int i(heapsize);
while (i > 1 && A[parent(i)].key > A[i].key) {
std::swap(A[parent(i)], A[i]);
i = parent(i);
}
}
int main()
{

}
Topic archived. No new replies allowed.