#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Heap{
int *array;
int count;
int capacity;
int heap_type;
};
struct Heap *createHeap(int capacity, int heap_type){
struct Heap *h=new Heap;
if(h==NULL){
court<<"Memory Error"<<endl;
return;
}
h->heap_type=heap_type;
h->count=0;
h->capacity=capacity;
h->array=new int[h->capacity];
if (h->array==nullptr){
cout<<"Memory Error"<<endl;
return nullptr;
}
return h;
}
int parent(struct Heap *h, int i){
if(i<=0 || i>=h->count)
return -1;
return (i-1)/2;
}
int leftChild(struct Heap *h, int i){
int left=2*i+1;
if(left>=h->count)
return -1;
return left;
}
int rightChild(struct Heap *h, int i){
int right=2*i+2;
if(right>=h->count)
return -1;
return right;
}
int getMaximum(struct Heap *h){
if(h->count==0)
return -1;
return h->array[0];
}
void heapify(struct Heap *h, int i){
int largest;
int l= leftChild(h, i);
int r= rightChild(h, i);
if (l!=-1 && h->array[l]>h->array[i])
largest=l;
else
largest=i;
if (r!=-1 && h->array[r]>h->array[largest])
largest=r;
if (largest!=i){
int temp=h->array[i];
h->array[i]=h->array[largest];
h->array[largest]=temp;
heapify(h,largest);
}
}
int deleteMax(struct Heap *h){
int data;
if (h->count==0)
return -1;
data=h->array[0];
h->array[0]=h->array[h->count-1];
h->count--;
heapify(h,0);
return data;
}
void resizeHeap(struct Heap *h){
int *array_Old=h->array;
h->array=new int[h->capacity*2];
if (h->array==nullptr){
cout<<"Memory Error"<<endl;
return ;
}
for (int i = 0; i < h->capacity; ++i)
h->array[i]=array_Old[i];
h->capacity*=2;
delete [] array_Old;
}
int insert(struct Heap* h, int data){
if(h->count==h->capacity)
resizeHeap(h);
h->count++;
int i=h->count-1;
while(i>=0 && data>h->array[(i-1)/2]){
h->array[i]=h->array[(i-1)/2];
i=i-1/2;
}
h->array[i]=data;
}
void destroyHeap(struct Heap *h){
if(h==nullptr)
return ;
delete [] h->array;
free(h);
h==NULL;
}
void buildHeap(struct Heap *h, const int arr[], int n){
cout<<"Build Heap"<<endl;
if (h==nullptr)
return ;
while(n>h->capacity)
resizeHeap(h);
for (int i = 0; i < n; ++i)
h->array[i]=arr[i];
h->count=n;
for (int i = (n-1)/2; i >=0 ; ++i)
heapify(h,i);
}
int main() {
int capacity=10;
struct Heap *heap= createHeap(capacity,1);
int array[]={1,0,2,7,5,6,3,8,9,4};
int n= sizeof(array)/ sizeof(array[0]);
buildHeap(heap,array,n);
for (int i = 0; i < capacity; ++i) {
cout<<heap->array[i]<<" ";
}
return 0;
}
What I have tried:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct Heap{
int *array;
int count;
int capacity;
int heap_type;
};
struct Heap *createHeap(int capacity, int heap_type){
struct Heap *h=new Heap;
h->heap_type=heap_type;
h->count=0;
h->capacity=capacity;
h->array=new int[h->capacity];
if (h->array==nullptr){
cout<<"Memory Error"<<endl;
return nullptr;
}
return h;
}
int parent(struct Heap *h, int i){
if(i<=0 || i>=h->count)
return -1;
return (i-1)/2;
}
int leftChild(struct Heap *h, int i){
int left=2*i+1;
if(left>=h->count)
return -1;
return left;
}
int rightChild(struct Heap *h, int i){
int right=2*i+2;
if(right>=h->count)
return -1;
return right;
}
int getMaximum(struct Heap *h){
if(h->count==0)
return -1;
return h->array[0];
}
void heapify(struct Heap *h, int i){
int largest;
int l= leftChild(h, i);
int r= rightChild(h, i);
if (l!=-1 && h->array[l]>h->array[i])
largest=l;
else
largest=i;
if (r!=-1 && h->array[r]>h->array[largest])
largest=r;
if (largest!=i){
int temp=h->array[i];
h->array[i]=h->array[largest];
h->array[largest]=temp;
heapify(h,largest);
}
}
int deleteMax(struct Heap *h){
int data;
if (h->count==0)
return -1;
data=h->array[0];
h->array[0]=h->array[h->count-1];
h->count--;
heapify(h,0);
return data;
}
void resizeHeap(struct Heap *h){
int *array_Old=h->array;
h->array=new int[h->capacity*2];
if (h->array==nullptr){
cout<<"Memory Error"<<endl;
return ;
}
for (int i = 0; i < h->capacity; ++i)
h->array[i]=array_Old[i];
h->capacity*=2;
delete [] array_Old;
}
int insert(struct Heap* h, int data){
if(h->count==h->capacity)
resizeHeap(h);
h->count++;
int i=h->count-1;
while(i>=0 && data>h->array[(i-1)/2]){
h->array[i]=h->array[(i-1)/2];
i=i-1/2;
}
h->array[i]=data;
}
void destroyHeap(struct Heap *h){
if(h==nullptr)
return ;
delete [] h->array;
free(h);
h==NULL;
}
void buildHeap(struct Heap *h, const int arr[], int n){
cout<<"Build Heap"<<endl;
if (h==nullptr)
return ;
while(n>h->capacity)
resizeHeap(h);
for (int i = 0; i < n; ++i)
h->array[i]=arr[i];
h->count=n;
for (int i = (n-1)/2; i >=0 ; ++i)
heapify(h,i);
}
int main() {
int capacity=10;
struct Heap *heap= createHeap(capacity,1);
int array[]={1,0,2,7,5,6,3,8,9,4};
int n= sizeof(array)/ sizeof(array[0]);
buildHeap(heap,array,n);
for (int i = 0; i < capacity; ++i) {
cout<<heap->array[i]<<" ";
}
return 0;
}