I want to build a priority queue from scratch that points to a struct in C++. I know this can easily be done using the STL library by priority_queue <struct_name> pq
. However, I am interested to build this on my own using an array. My initial approach was to build a Min heap for integers and eventually modify the code such that the array points to a struct. To give you more context, here is the code I have written until now.
#include<iostream>
#define MAX_SIZE 100
using namespace std;
struct cell{
int x, y;
int depth;
// Constructor
cell(int x, int y, int depth):
x(x), y(y), depth(depth) {}
};
class minHeap{
private:
// Current size of heap
int size;
// Max size of heap
int capacity;
// Storing elements as an array
struct cell* heap = new struct cell[MAX_SIZE];
// Returns the parent index
int parent(int i){
return (i-1)/2;
}
// Returns left child
int leftChild(int i){
return 2*i + 1;
}
// returns right child
int rightChild(int i){
return 2*i + 2;
}
public:
// Constructor
minHeap(int capacity){
size = 0;
this->capacity = capacity;
}
void insert(struct cell node){
if(size == capacity){
cout<<"Heap is FULL"<<endl;
return;
}
// Increase the amount of elements in the heap
size++;
// Insert the element at the end of heap
heap[size-1] = node;
// Maintain heap invariance
int i = size - 1;
while(i!=0 && greater(heap[parent(i)], heap[i])){
struct cell temp = heap[i];
heap[i] = heap[parent(i)];
heap[parent(i)] = temp;
i = parent(i);
}
}
void heapify(int i){
int left = leftChild(i);
int right = rightChild(i);
int smallest = i;
// Find the smallest element of the three
if((left < size) && greater(heap[left], heap[smallest])){
smallest = left;
}
if((right < size) && (greater(heap[right], heap[smallest]))){
smallest = right;
}
// If the smallest is left or right, keep heapifying
if(smallest != i){
struct cell temp = heap[i];
heap[i] = heap[smallest];
heap[smallest] = temp;
heapify(smallest);
}
}
int extractMin(){
// Check is heap is empty
if(size == 0){
cout<<"Heap is empty"<<endl;
return -1;
}
else if(size == 1){
size--;
return heap[0].depth;
}
else{
struct cell root = heap[0];
heap[0] = heap[size-1];
size--;
heapify(0);
return root.depth;
}
}
void printHeap(){
int power = 0;
int value = 0;
for(int i=0; i<size; i++){
if(i == value){
cout<<endl;
power += 1;
value += (1<<power);
}
cout<<heap[i].depth<<" ";
}
cout<<endl;
}
};
bool greater(struct cell cell1, struct cell cell2){
if(cell1.depth == cell2.depth){
if(cell1.x != cell2.depth){
return (cell1.x < cell2.x);
}
else return cell1.y < cell2.y;
}
return cell1.depth < cell2.depth;
}
int main(){
minHeap m1(15);
return 0;
}
However, I run into some errors after this implementation. I have listed them below:
Heap.cpp:24:46: error: no matching function for call to 'cell::cell()'
struct cell* heap = new struct cell[MAX_SIZE];
^
Heap.cpp:11:2: note: candidate: cell::cell(int, int, int)
cell(int x, int y, int depth):
^~~~
Heap.cpp:11:2: note: candidate expects 3 arguments, 0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(const cell&)
struct cell{
^~~~
Heap.cpp:6:8: note: candidate expects 1 argument, 0 provided
Heap.cpp:6:8: note: candidate: constexpr cell::cell(cell&&)
Heap.cpp:6:8: note: candidate expects 1 argument, 0 provided
Heap.cpp: In member function 'void minHeap::insert(cell)':
Heap.cpp:59:24: error: missing template arguments before '(' token
while(i!=0 && greater(heap[parent(i)], heap[i])){
^
Heap.cpp: In member function 'void minHeap::heapify(int)':
Heap.cpp:73:30: error: missing template arguments before '(' token
if((left < size) && greater(heap[left], heap[smallest])){
^
Heap.cpp:76:32: error: missing template arguments before '(' token
if((right < size) && (greater(heap[right], heap[smallest]))){
I am relatively new to C++ so I'm not sure if I'm giving all of the information necessary. Please let me know if I can provide any additional details.
Because cell has a constructor with arguments but doesn't have a default (no arguments) constructor listed, you get that first error.
You either need to create a default constructor or make the one you have take a default value on its argument.
Fix that and see how many of your other errors disappear.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.