i am trying to implement an ADT of maxheap in c. i am having trouble with the heapify function.
this is my h file
typedef struct t_MaxHeap* MaxHeap;
typedef struct t_Node* Node;
typedef void *element;
typedef element (*copyFunc)(element);
typedef void (*PrintFunc)(element);
typedef void (*freeFunc)(element);
typedef element(*compareFunc)(element,element);
typedef element (*containFunc)(element);
this is my struct (which is located in the c file)
#include "MaxHeap.h"
struct t_Node
{
element value;
};
struct t_MaxHeap{
int size;
int capcity; //const
char *name;
copyFunc copyFunction;
freeFunc freeFunction;
PrintFunc printFunction;
containFunc containFunction;
compareFunc compareFunction;
Node* array;
};
im creating the heap with no problem.. then i defined a swap function which will be used in the heapify..
void swap (MaxHeap new_MaxHeap,Node n1,Node n2 )
{
Node temp= n1;
n1=n2;
n2=temp;
}
now for the heapify function i have problem with the heapify function and im not sure how to fix it.
void heap_maxify(MaxHeap new_MaxHeap,element i)
{
element largest=i;
element a=new_MaxHeap->array[2*i];
element b=new_MaxHeap->array[a+1];
element b;
b=a+1;
if(a <=new_MaxHeap->size && new_MaxHeap->compareFunction(new_MaxHeap->array[largest],new_MaxHeap->array[a])==1)//1 means a is bigger then largest
{
largest=a;
}
if(b <=new_MaxHeap->size && new_MaxHeap->compareFunction(new_MaxHeap->array[largest],new_MaxHeap->array[b])==1) //1 means b is bigger then largest
{
largest=b;
}
if (largest !=i){
swap(new_MaxHeap,i,largest);
}
}
i think my two if statement are good.. the problem is with setting a as the element which is in index 2i and b which is in index 2i+1 from the currrent element