Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

struct Heap{
    int *array;
    int count; // number of elements in  heap
    int capacity;  // size of the heap
    int heap_type;  //max(1)  or min(0);
};

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; // number of elements in  heap
    int capacity;  // size of the heap
    int heap_type;  //max(1)  or min(0);
};

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;
}
Posted
Updated 28-Sep-22 1:39am
v2
Comments
OriginalGriff 28-Sep-22 6:03am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

I wonder how did you find out such a behavior: there are syntax errors in your code, like in the following line
Quote:
court<<"Memory Error"<<endl;
so it doesn't even compile.

Anyway, consider the code below
Quote:
for (int i = (n-1)/2; i >=0 ; ++i)
heapify(h,i);

(If n>1 ) How could such a loop possibly terminate ?
 
Share this answer
 
v2
You have the following in buildHeap:
C++
for (int i = (n-1)/2; i >=0 ; ++i)
    heapify(h,i);

which will create an infinite loop. It should be:
C++
for (int i = (n-1)/2; i >=0 ; --i) // i should be decremented
    heapify(h,i);


Note, there are a number of other issues raised by the compiler that need to be corrected.
 
Share this answer
 
Comments
Rishabh Vishwakarma 2022 28-Sep-22 15:40pm    
Thank you sir!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900